JavaScript questions that don't deserve their own thread

● ARCHIVED · READ-ONLY
Started by Shaz 3413 posts Page 116 of 171 View original ↗
  1. @TheAM-Dol - As an alternate solution to @GmOcean, simply set your range in the for loop to what you're looking at in the first place. Then no ifs necessary.

    Code:
    for (var i=30; i<=59; i++)
        ['A', 'B', 'C', 'D'].forEach(letter => $gameSelfSwitches([$gameMap.mapId(), i, letter], false));
  2. ATT_Turan said:
    Yanfly's Move Route plugin has a Move To Coordinates command, along with Move Toward Location and Move Toward Player.

    You can do what you're asking with that.
    I've been using Galv Move Route Extras step toward commands over Yanfly's Move Route since my game uses pixel movement and Yanfly's grid movement just makes events move in zigzags since it sticks to the grid. I'm stuck on the part where I need to calculate distance between the event's coordinate and each coordinate in the player position array, pick the smallest value and move there. I can probably get something working eventually since I also use Krons_AI_base to get distance between two events but it'll take a day or two.
  3. I don't know what I'm doing wrong, but this is what I threw together. If I call this function it lags the game for a few seconds and does nothing. It has dependencies on Restart's CrossEngine.js and uses a modified version of Restart's goTowardsPlayer function to goTowardsCoordinates instead that works in isolation but not as a part of this function.
    JavaScript:
    Game_CharacterBase.prototype.followExactPath = function(){
    var playerCoordinates = [];
    
    //If player is moving push their coordinates to an array
    if($gamePlayer._inMotion){
      if (playerCoordinates.length === 20) {
        playerCoordinates.shift();
      }
      playerCoordinates.push([$gamePlayer.x, $gamePlayer.y]);
      $gameMap._interpreter.wait(5); //Wait 5 frames before adding the next coordinate
    }
    
    //Find the closest coordinates to the current event in the playerCoordinates array
    var closestX;
    var closestY;
    var closestDistance = Infinity;
    
    for (var i = 0; i < playerCoordinates.length; i++) {
        var x = playerCoordinates[i][0];
        var y = playerCoordinates[i][1];
        var distance = Math.sqrt((x - this.character(0).x) ** 2 + (y - this.character(0).y) ** 2);
        if (distance < closestDistance) {
          closestX = x;
          closestY = y;
          closestDistance = distance;
          playerCoordinates.splice(0, i);
        }
     }
    
    //Make current event go to the closest coordinates
    this.character(0).goTowardsCoordinates(closestX, closestY);
    //When it reaches the coordinates make it go to the next position in the array
    if(this.character(0).x === closestX && this.character(0).y === closestY){
      closestX = playerCoordinates[1][0];
      closestY = playerCoordinates[1][1];
      this.character(0).goTowardsCoordinates(closestX, closestY);
      playerCoordinates.shift();
    }
    }
  4. AquaEcho said:
    I don't know what I'm doing wrong, but this is what I threw together.
    This is definitely more complex than a single post that doesn't deserve its own thread.

    That being said, a few things look weird to me.

    - You only push one set of (the player's current) coordinates into playerCoordinates, but then you iterate through the entire list of its coordinates.

    - I don't see what the point of the interpreter wait in there is.

    - You only push any coordinates if the player is moving, but you never have any default initialized values or make checks later on for the coordinate list to have length. It seems to me if you call this when the player isn't moving the entire rest of your code will glitch out with undefined values.

    - What is this.character(0) ? Is it added from the other plugins you reference?

    You should definitely post your own thread detailing exactly what you want to accomplish, link to the plugins you're referencing, and show how you're calling this in-game.
  5. I am trying to solve the problem posed in this thread (pics taken from there) where a following event runs into the wall if you get too far ahead of them by making the event follow the exact path the player took (and also get it to work with pixel movement since I use it).

    pathing 01.png
    pathing 02.png
    ATT_Turan said:
    You should definitely post your own thread detailing exactly what you want to accomplish, link to the plugins you're referencing, and show how you're calling this in-game.
    I'll work on this script and make a new thread. I'll put the playerCoordinates array part in its own function since it should run independently of the rest of the script; I wanted it to act more like a do while script that logs the player position every five frames but it made the game freeze when I tried that.

    ATT_Turan said:
    - What is this.character(0) ? Is it added from the other plugins you reference?
    It's a Game_Interpreter function. Shaz's explanation: Now, Game_Interpreter has a neat function called character(x) and if you pass -1 to that function, it returns the player object. If you pass a number > 0, it returns the event object of the event with that id. And if you pass 0, it returns the event object that this command was run from. So this.character(0) is the same as saying $gameMap.event(this._eventId).
  6. AquaEcho said:
    It's a Game_Interpreter function. Shaz's explanation:
    That's not correct syntax, then. Your function is declared as a Game_CharacterBase method, so this will always refer to a Game_CharacterBase object.
  7. ATT_Turan said:
    That's not correct syntax, then. Your function is declared as a Game_CharacterBase method, so this will always refer to a Game_CharacterBase object.
    Thanks for all your help. I finally got everything working and would like to publicly release it here for others to use but I don't know how to write this loop part into javascript for a callable function or create a global array without using a control variable.CreatePlayerPath.png
  8. Hey all.

    I have this command working when I type it in the MZ console :

    $gameVariables.value(454).splice(2, 1);

    But using it in a script event command won't work, any idea why?
  9. Splice works only on array prototypes. Not on integers and not on strings. So unless your variable 454 is an array, it should not work.
  10. Poryg said:
    Splice works only on array prototypes. Not on integers and not on strings. So unless your variable 454 is an array, it should not work.
    Could it not work because my variable is detected as an Object rather than an Array?
  11. Narch said:
    Could it not work because my variable is detected as an Object rather than an Array?
    No.

    I put this code into the console:
    Screenshot 2023-02-20 110323.jpg

    Then duplicated it in a Script box in an event:
    Screenshot 2023-02-20 110726.jpg

    and the output of the end result of the array was the same. If it's not working the same way for you, you have something wrong in the code in your script box or you have something else modifying the variable so it isn't starting at the value/settings you expect.
  12. Afternoon all,

    Looking for a way to check if the same weapontype is in use while dual wielding.


    Leaning towards

    actor.equips()[0] && actor.equips()[1] === (parseInt(match[1]));


    But feel like I'm overlooking something obvious. Any tips? (For context I'm setting up equipment requirements via notetag, hence the match)


    Ideally I want to be able to require certain armor to be usable only when the Actor has x weapon type equipped in main and offhand
  13. Puppet Knight said:
    Afternoon all,

    Looking for a way to check if the same weapontype is in use while dual wielding.


    Leaning towards

    actor.equips()[0] && actor.equips()[1] === (parseInt(match[1]));


    But feel like I'm overlooking something obvious. Any tips? (For context I'm setting up equipment requirements via notetag, hence the match)


    Ideally I want to be able to require certain armor to be usable only when the Actor has x weapon type equipped in main and offhand

    So, are you trying to check if both weapons are of a specified type? You're currently checking if the first equipped item exists and if the second one is equal to an int (which will always be false, since items are not ints). I'm assuming you'd like to do something like this:

    actor.equips()[0]._itemId == parseInt(match[1]) && actor.equips()[1]._itemId == parseInt(match[1])

    Which should work assuming that you do have the variables actor and match defined in this context, the the second item is indeed a weapon and not armour, and that MZ didn't change anything that would make my MV knowledge not applicable here.
  14. Mac15001900 said:
    Which should work assuming that you do have the variables actor and match defined in this context, the the second item is indeed a weapon and not armour, and that MZ didn't change anything that would make my MV knowledge not applicable here.
    Should work but not still ^_^'. I'll keep playing around with it. For context this is the plugin I'm using


    I think Im on the right track with actor.equips()[0] == actor.equips()[1];

    since at the very least the same item has to be equipped. still would help to expand it to same wType
  15. Puppet Knight said:
    I think Im on the right track with actor.equips()[0] == actor.equips()[1];
    That's unlikely to work, since you're comparing two objects. This would very much depend on how the engine handles it, but most likely these are two different objects with the same properties. Try just comparing _itemId directly.

    And generally start by trying out simpler things. I'm assuming you're trying to add a new case to that big switch statement in the plugin? Try to make the condition a simple true, then just false - make sure that works as expected. Then try just checking the id of one weapon, and work your way up from there.

    You can also use console.log(actor.equips()) next to that check to see what this list looks like, which should help you find out which properties you'll need.
  16. Mac15001900 said:
    And generally start by trying out simpler things. I'm assuming you're trying to add a new case to that big switch statement in the plugin? Try to make the condition a simple true, then just false - make sure that works as expected. Then try just checking the id of one weapon, and work your way up from there
    I've built out by weapon type in my own updates to the plugin as:


    case "wType": if (result) result = actor.isWtypeEquipped(parseInt(match[1])); break;


    Along with a couple of other updates based on armour type, skill level (to tie int TAA Spiders plugin) and even Class Level when using dual classes

    actor.equips()[0] == actor.equips()[1];

    makes it so having the same weapon in both slots satisfies the requirement.

    I'm struggling with how to merge the two to designate what Weapon type the note tags looks for that should be in both slots
  17. The player can equip items which add an element to the ordinary attack skill. That is not a problem.

    However, what I would like would be :
    If the user has armour id = 91 equipped, then there is a 50% chance that the target will be afflicted with State 64.

    Similarly if armour id = 92 is equipped, then the state would be State 65.

    I assume that this would be done by a note tag in the skill, but beyond that I am lost. I've tried looking in the script call list, but can only see a call for if a specific actor is equipped.

    I am using VisuStella plugins if that is relevant.

    Can anyone help me out?

    Thanks.
  18. Kes said:
    If the user has armour id = 91 equipped, then there is a 50% chance that the target will be afflicted with State 64.

    Similarly if armour id = 92 is equipped, then the state would be State 65.

    I assume that this would be done by a note tag in the skill...I am using VisuStella plugins if that is relevant.
    I would just give the armors a passive state.

    You can make the state do what you asked by giving it:
    Code:
    <JS Post-Damage as User>
    if (!target.isDead() && Math.random()<.5)
        target.addState(64);
    </JS Post-Damage as User>
  19. @ATT_Turan Thank you for this. Much appreciated.
  20. ATT_Turan said:
    I would just give the armors a passive state.

    You can make the state do what you asked by giving it:
    Code:
    <JS Post-Damage as User>
    if (!target.isDead() && Math.random()<.5)
        target.addState(64);
    </JS Post-Damage as User>
    Unless I'm misunderstanding the help documentation, I think you can actually just put that note tag directly on the armor itself, without the need for a state.