JavaScript questions that don't deserve their own thread

● ARCHIVED · READ-ONLY
Started by Shaz 3413 posts Page 22 of 171 View original ↗
  1. Hello, 


    I trying to make a script and I want it to first find all the events within the map that the player is in.


    However I seem to get an error saying:

    Code:
    TypeError: Cannot read property 'events' of null



    The line of code its having trouble with is very first like of this for loop.


    for (var i = 0; i < $dataMap.events.length-1; i++) {
            var note = $dataMap.events.note;
            var note_args = note.split(" ");
            var note_id = note_args.shift().toLowerCase();
            var piece_num = Number(note_args.shift());
            if ( note_id == "piece") {
                puzzle_pieces[piece_num].push(i);
            }
        }


    This for loop is not inside any function, besides the main (function() {  })();


    Correct me if I'm wrong, but I think the error is coming from the fact that the script is loaded before the map is.


    If this is true, how do I make the script wait?


    If not what is causing this error and how should I fix it?
  2. How do I ensure that things I add to a class are saved and loaded appropriately?

    I'm trying to make a pokemon style EV system and added the following:
     


    Game_BattlerBase.prototype.addedTraining = [0,0,0,0,0,0,0,0];


    I then use plugin commands to manipulate these with regard to a given actor. e.g:


    var protagonist = $gameActors.actor(1);
    protagonist.addedTraining[statNum]+=1;


    When I save and load a different game, the addedTraining array is the same as before. When I leave the game completely and reload any game, the addedTraining array is back to being entirely zeroes.

    Where should I be keeping this data? It used to be that anything associated with an actor was saved along with the actor...
  3. Hey guys.


    I'm trying to get a passive state that absorbs MP from single target spells that hit the actor with the state.


    <Custom Respond Effect>
    console.log(this)
    if (this.isHpEffect() && this.isMagical()) {
    var cost = ($dataSkills[a._actions[0]._item._itemId].mpCost)
    b.gainMp(cost)
    }
    </Custom Respond Effect>


    I have the above working lunatic code in a notetag on the state. The problem is that it also restores MP on a multitarget spell hitting the actor. I've tried using && this.isForOne() in the if statement, but it doesn't change the effect. Same if I use && this.isForAll() instead.


    Anyone know offhand how to check for a single target spell effect?
  4. Hello,


    I'm trying to use the Custom Turn Modifiers from Yanfly's BuffsStatesCore


    The Lunatic Custom Turn Modifiers should allow my to change the turns of a state but somehow i don't get them to work.


    Here's the Code i'm using in a skill


    <Custom State 56 Turn>
    if(target.isStateAffected(56))
    turn = 11;
    else
    turn = 10;
    </Custom State 56 Turn>


    for example.


    If i use the Skill to apply State 56 the turncounter is still always set to the original settings of the state.


    I already tried removing the Turn Condition and adding the state using JS Code. The result stayed the same,


    it's as if the Turn Modifiers aren't used.


    help would be appreciated very much :)
  5. ramza said:
    Hey guys.


    I'm trying to get a passive state that absorbs MP from single target spells that hit the actor with the state.



    <Custom Respond Effect>
    console.log(this)
    if (this.isHpEffect() && this.isMagical()) {
    var cost = ($dataSkills[a._actions[0]._item._itemId].mpCost)
    b.gainMp(cost)
    }
    </Custom Respond Effect>


    I have the above working lunatic code in a notetag on the state. The problem is that it also restores MP on a multitarget spell hitting the actor. I've tried using && this.isForOne() in the if statement, but it doesn't change the effect. Same if I use && this.isForAll() instead.


    Anyone know offhand how to check for a single target spell effect?

    For anyone interested, I managed to solve this myself by checking the scope of the skill being used. Scope 2 is All enemies, and Scope 8 is all allies. I left the other ones in there, in case a multi-hit move meets the other requirements to restore MP:


    <Custom Respond Effect>
    if (this.isHpEffect() && this.isMagical()) {
    var scope = $dataSkills[a._actions[0]._item._itemId].scope
    if (scope != 2 && scope != 8) {
    var cost = ($dataSkills[a._actions[0]._item._itemId].mpCost)
    b.gainMp(cost)
    }
    }
    </Custom Respond Effect>






    I am also trying to make a skill that writes some text on the battlelog window instead of in a text box and am having no luck tracking this function down. I found an old topic about this in rmvxa, but I'm not good enough to convert the information given in that topic to rmmv.


    Anyone know what one would use as an eval to write text to the battlelog window in rm mv?


    Thanks in advance.
  6. ramza said:
    Anyone know what one would use as an eval to write text to the battlelog window in rm mv?

    While I haven't tried using the following bit of code in skill eval notetags, it works at least for states:

    Code:
    var text = 'text goes here';
    var window = SceneManager._scene._logWindow; 
    window._lines.push(text + '<CENTER>');
    window.refresh();
  7. Punamaagi said:
    While I haven't tried using the following bit of code in skill eval notetags, it works at least for states:



    var text = 'text goes here';
    var window = SceneManager._scene._logWindow;
    window._lines.push(text + '<CENTER>');
    window.refresh();

    Works like a charm!


    You'd think one of the dozens of battle/message plugins I have installed would make this easier.
  8. Talonos said:
    How do I ensure that things I add to a class are saved and loaded appropriately?

    I'm trying to make a pokemon style EV system and added the following:
     



    Game_BattlerBase.prototype.addedTraining = [0,0,0,0,0,0,0,0];


    I then use plugin commands to manipulate these with regard to a given actor. e.g:



    var protagonist = $gameActors.actor(1);
    protagonist.addedTraining[statNum]+=1;


    When I save and load a different game, the addedTraining array is the same as before. When I leave the game completely and reload any game, the addedTraining array is back to being entirely zeroes.

    Where should I be keeping this data? It used to be that anything associated with an actor was saved along with the actor...

    I addressed this in IRC, but I'll do this again too. Game_BattlerBase isn't saved or loaded, so anything you add them will only remain in memory unless it's consumed or remodified by a saved object.

    The data that is saved exists in DataManager, specifically the makeSaveContents and extractSaveContents functions that control save and loading, and the createGameObjects function that maps classes to the global variables. You'll notice that, in the latter, $gameActors maps to Game_Actors, which intern exposes each individual array member as Game_Actor. And while you're right that Game_BattlerBase is a superclass of Game_Battler which is a superclass of Game_Actor, Game_Actor is more concerned with the manipulation of the actor objects and the data contained within. If you want the data in Game_Actor, you should add addedTraining as an instance member of Game_Actor and have it affect an instance variable in Game_Actor. Perhaps adding your array directly to the json data as an 'addedTraining' property, and then accessing it (and creating the appropriate methods) in Game_Actor?
  9. Milena said:
    I have a question about blt and numbers. I want to change the texts Lv and the HP MP numbers into an image. For example, if I use sprite, it becomes cluttered and since it gets created everytime, the sprite will just double up in the window. Is there a proper way to make the text LV into an image? How about the numbers?

    Draw the bitmap only when you need it. You can use a boolean variable to mark that you've already done so to avoid doubling up.
  10. DreamX said:
    Draw the bitmap only when you need it. You can use a boolean variable to mark that you've already done so to avoid doubling up.



    It works fine for the level image. How about the numbers? Do you know a way to substitute images to represent the numbers just like the Sprite Damage? Thank you very much.
  11. I'm trying to make a script I can call to instantly reset every 'D' self switch on all events on all maps.


    I have the following snippet that doesn't crash the game when it runs:


    for (var m = 1; m < $dataMapInfos.length; m++){
    for (var s = 1; s < $dataMap.events.length; s++){
    $gameSelfSwitches.setValue([m, s, 'D'], false);}
    }


    The issue with it, is that the second for loop is based on the number of events on the current map, not the map being handled. he $dataMapInfos thing I'm plugging into to get all the maps, doesn't contain any information on the events on the maps. Does anyone know where this info is stored before it loads into $dataMap?
  12. Is it possible to determine which actor made the physical attack (from the normal attack command) and then permanently increase a game variable every time that actor attacks?


    for example.  Actor 1 attacks and in increases the value of v[114] by 1.  If actor 2 attacks, it wont increase the value of v[114] 
  13. Milena said:
    It works fine for the level image. How about the numbers? Do you know a way to substitute images to represent the numbers just like the Sprite Damage? Thank you very much.

    Try this.


    Window_Base.prototype.drawNumber = function (x, y, value) {
    var bitmap = ImageManager.loadSystem('Damage');
    var string = Math.abs(value).toString();
    var digitWidth = bitmap.width / 10;
    var digitHeight = bitmap.height / 5;

    for (var i = 0; i < string.length; i++) {
    var bitmapX = x + (i * digitWidth);
    var n = Number(string);
    var sx = n * digitWidth;
    this.contents.blt(bitmap, sx, 0, digitWidth,
    digitHeight, bitmapX, y);
    }
    };



    ramza said:
    I'm trying to make a script I can call to instantly reset every 'D' self switch on all events on all maps.


    I have the following snippet that doesn't crash the game when it runs:



    for (var m = 1; m < $dataMapInfos.length; m++){
    for (var s = 1; s < $dataMap.events.length; s++){
    $gameSelfSwitches.setValue([m, s, 'D'], false);}
    }


    The issue with it, is that the second for loop is based on the number of events on the current map, not the map being handled. he $dataMapInfos thing I'm plugging into to get all the maps, doesn't contain any information on the events on the maps. Does anyone know where this info is stored before it loads into $dataMap?

    Maps are not loaded unless they are needed. What you can do is set the self switch of every event id from 1 to a very high number. That way you don't need to know the exact event count for every map. Otherwise, you will either need to plug in the exact count for every map id, or you need to load all the maps before hand to get that information, or you can just perform the self switch turning off when each map loads normally.

    lithkast said:
    Is it possible to determine which actor made the physical attack (from the normal attack command) and then permanently increase a game variable every time that actor attacks?


    for example.  Actor 1 attacks and in increases the value of v[114] by 1.  If actor 2 attacks, it wont increase the value of v[114] 

    Try this. Put it into a .js file plugin.


    alias_Game_Actor_performAction = Game_Actor.prototype.performAction;
    Game_Actor.prototype.performAction = function (action) {
    if (action.isForOpponent()) {
    switch (this.actorId()) {
    case 1:
    $gameVariables.setValue(144, $gameVariables.value(144) + 1);
    break;
    }
    }
    alias_Game_Actor_performAction.call(this, action);
    };


    Change the variable id and switch statement as necessary.
  14. awesome thanks!
  15. asksuyo said:
    Hello, 


    I trying to make a script and I want it to first find all the events within the map that the player is in.


    However I seem to get an error saying:

    Code:
    TypeError: Cannot read property 'events' of null



    The line of code its having trouble with is very first like of this for loop.



    for (var i = 0; i < $dataMap.events.length-1; i++) {
            var note = $dataMap.events.note;
            var note_args = note.split(" ");
            var note_id = note_args.shift().toLowerCase();
            var piece_num = Number(note_args.shift());
            if ( note_id == "piece") {
                puzzle_pieces[piece_num].push(i);
            }
        }


    This for loop is not inside any function, besides the main (function() {  })();


    Correct me if I'm wrong, but I think the error is coming from the fact that the script is loaded before the map is.


    If this is true, how do I make the script wait?


    If not what is causing this error and how should I fix it?



    Put it into a function and then call it when you need it.
  16. DreamX said:
    Maps are not loaded unless they are needed. What you can do is set the self switch of every event id from 1 to a very high number. That way you don't need to know the exact event count for every map. Otherwise, you will either need to plug in the exact count for every map id, or you need to load all the maps before hand to get that information, or you can just perform the self switch turning off when each map loads normally.

    Thanks for that info. Right now I'm just telling it to check eventIds 1-999, but I figured that might cause an unnecessary performance hit, if there were only a small number of events with self switches to check in that large number. Testing, it doesn't seem to cause any sort of slowdown or anything though, so I think that's good enough.
  17. Is there a way to reference weapon types in the damage formula?  For example...



     Actor 1 attacks the target.

    The formula checks what weapon type actor 1 has equipped (in this case a sword) and that determines the potency of the attack (lets go with  1-8)


    Essentially.  a. weapontype(1) ? Potency = (Math.random() * 8) + 1)
  18. lithkast said:
    Is there a way to reference weapon types in the damage formula?  For example...



     Actor 1 attacks the target.

    The formula checks what weapon type actor 1 has equipped (in this case a sword) and that determines the potency of the attack (lets go with  1-8)


    Essentially.  a. weapontype(1) ? Potency = (Math.random() * 8) + 1)

    $dataWeapons[$gameActors._data[a.actorId()]._equips[0]._itemId].wTypeId will give you the weapontype of the weapon in slot 0 of the actor making the attack.


    This will probably crash the game if an enemy uses it though, so you'll need some if logic to stop that from happening.
  19. ramza said:
    $dataWeapons[$gameActors._data[a.actorId()]._equips[0]._itemId].wTypeId will give you the weapontype of the weapon in slot 0 of the actor making the attack.


    This will probably crash the game if an enemy uses it though, so you'll need some if logic to stop that from happening.



    Wow, that is a long statement.


    This one is shorter and gives the weapon type id, too. BD


    a.equips()[0].wtypeId


    This won't crash the game if an enemy uses that attack either.
  20. kiriseo said:
    Wow, that is a long statement.


    This one is shorter and gives the weapon type id, too. BD


    a.equips()[0].wtypeId


    This won't crash the game if an enemy uses that attack either.

    This is why we pay you the big bucks :p