JavaScript questions that don't deserve their own thread

● ARCHIVED · READ-ONLY
Started by Shaz 3413 posts Page 165 of 171 View original ↗
  1. Hi everyone, anybody have experience on porting VX project to MV or MZ?
    I’m trying to remake an old VX project into MV, I have some complicated thing.
    When I’m reading script in this old project, a script has many settings about switches.
    For example,

    // disable key input with this swich
    DONT_MOVE_SWITCH 54
    // when pitfalls are activated
    PITFALL_ACITIVATED 55
    // when deactivated pitfalls
    PITFALL_DEACTIVATED 56
    (some lines goes on after)

    If I want to rewrite this script as plugin, can some switches be set in one plugin?
  2. I don’t think this deserves its own thread, but I’m looking for help creating a JS snippet.

    Basically, I’m looking to create a passive state that when equipped prevents the user’s state turns from ticking down and the expiring states.

    For example, if they have an attack buff states that’s expires in three turns, the countdown wouldn’t change at all when the passive is equipped.
  3. When you say you have a passive, did you mean yanflys / visustellas passive plugin or anothers? Cause you could make a turn end effect for it to increase the durations of all of their buffs by 1 instead of making a plugin to not tick the durations down if they have the state.
  4. Robro33 said:
    When you say you have a passive, did you mean yanflys / visustellas passive plugin or anothers? Cause you could make a turn end effect for it to increase the durations of all of their buffs by 1 instead of making a plugin to not tick the durations down if they have the state.
    I am using visustella plugins, but essentially the passive is just going to be another state without and turn duration.
  5. ello again c:

    how do i make it so that i change the cost of certain skills using variables? like, maybe if "variable 20" had a value of 1, it would multiply the mp (or tp as it's what i use more) cost by 2x from its original.

    asking because i'm trying to invent a different skill mechanic for one of my characters where they can charge up their skill to become stronger in exchange for being more expensive to use (and after using, will return back to its base level)
  6. I replied to a question and the solution seemed to work, but then I started confusing myself. The solution was like this (abbreviated):

    const userStates = user.states();
    const userStateTurns = user._stateTurns;
    user.clearStates();
    userStates.forEach(s => {target.addState(s.id); target.setStateTurns(s.id, userStateTurns[s.id])});

    I've understood before that if you assign an array/object to a variable, it's just a reference. But that "clearStates" part empties the original arrays. Ok, that states() uses 'map', which if I understood it correctly creates a new array, so that's why I can use the data in the end. But why can I also use the _stateTurns data (that to my understanding the const userStateTurns references) after it's been cleared?
  7. DecastarDaDumy said:
    how do i make it so that i change the cost of certain skills using variables? like, maybe if "variable 20" had a value of 1, it would multiply the mp (or tp as it's what i use more) cost by 2x from its original.
    you can use a plugin such as yanfly's skill core that allows you to alter skill costs via notetags and have the cost be twice than normal if the variable's value is equal to 1

    rpgLord69 said:
    But that "clearStates" part empties the original arrays. Ok, that states() uses 'map', which if I understood it correctly creates a new array, so that's why I can use the data in the end. But why can I also use the _stateTurns data (that to my understanding the const userStateTurns references) after it's been cleared?
    as ive understood it, its because clearStates doesnt really "empty" the array. If it did something to mutate what those properties pointed to like popping every element in the _states array then itd be the same, but its a reassignment and changes the reference entirely.
    Because it reassigns both _states and _stateTurns, those two now reference an empty array and object in new sections of memory, but the variables youve made by then still point to the old locations _states and _stateTurns used to, which still refer to the old values.
  8. Ok, I feel kind of silly... when using the Kadokawa SlotMachine plugin I managed to edit the payouts a few months ago and now I wanted to change it again and I cannot for the life of me figure out where I went to do that...I don't see any lines in the JS file that look like something I've messed around with. I remember it being fairly straightforward when I found it but I don't know where the section I have to change is now...
  9. Thats one of the reasons why making a plugin to hold edits for plugins is often recommended over directly editing the plugin. Try getting a fresh copy of the plugin and compare the new and old versions until you find where they differ
  10. Robro33 said:
    Thats one of the reasons why making a plugin to hold edits for plugins is often recommended over directly editing the plugin. Try getting a fresh copy of the plugin and compare the new and old versions until you find where they differ
    It's not a particularly large file... I just didn't see anything that...EDIT: nvm, I found it I'm blind...it's under the "odds" section I was just looking for something mentioning payouts/win/coin or something...xD silly me.
  11. Hi, everyone. I am trying to see if there is a more efficient way of doing this. I hope am I giving sufficient information below.

    First, I am using a script to create 81 arrays that are all the same. The array variables are 1401 - 1481. In the following script, variable 1386 starts off with a value of 1401 (for the first array). This seems to work fine and seems pretty efficient.


    for (let i = 0; i < 81; i++) {

    $gameVariables.setValue($gameVariables.value(1386), [1,2,3,4,5,6,7,8,9]);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +1));

    }


    Next, I'm getting a random number from 1 - 9 and using a script to remove it from the first array. Variable 1301 is the value of the random number. This also seems fine and pretty efficient.


    const index = $gameVariables.value(1401).indexOf($gameVariables.value(1301));

    const x = $gameVariables.value(1401).splice(index, 1);


    Now, here is one big script that is removing the value of variable 1387 (which is the same as variable 1301) from a bunch of other arrays. The array variables are 1402 - 1412, 1419 - 1421, 1428, 1437, 1446, 1455, 1464, and 1473. So, I am using 3 separate for loops. This script works and removes the value from all necessary arrays. I'm just wondering if there is a more efficient way to do this part.


    for (let i = 0; i < 11; i++) {

    let index = $gameVariables.value($gameVariables.value(1386)).indexOf($gameVariables.value($gameVariables.value(1387)));

    let x = $gameVariables.value($gameVariables.value(1386)).splice(index, 1);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +1));

    } //This is for arrays 1402 - 1412

    $gameVariables.setValue(1386, 1419); //This sets the variable for the array to 1419

    for (let z = 0; z < 3; z++) {

    index = $gameVariables.value($gameVariables.value(1386)).indexOf($gameVariables.value($gameVariables.value(1387)));

    x = $gameVariables.value($gameVariables.value(1386)).splice(index, 1);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +1));

    } //This is for arrays 1419 - 1421

    $gameVariables.setValue(1386, 1428); //This sets the variable for the array to 1428

    for (let y = 0; y < 6; y++) {

    index = $gameVariables.value($gameVariables.value(1386)).indexOf($gameVariables.value($gameVariables.value(1387)));

    x = $gameVariables.value($gameVariables.value(1386)).splice(index, 1);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +9));

    } //This is for arrays 1428, 1437, 1446, 1455, 1464, and 1473


    Any advice or suggestions are greatly appreciated!
  12. Code:
    for (let i = 0; i < 81; i++) {
    
    $gameVariables.setValue($gameVariables.value(1386), [1,2,3,4,5,6,7,8,9]);
    
    ($gameVariables.setValue(1386, $gameVariables.value(1386) +1));
    
    }

    I’ll just comment on the first piece. What you got works well, and good job figuring it out!

    But you can be even more efficient by taking advantage of variable i which changes each time the loop runs
    Code:
    for (let i = 0; i < 81; i++) {
    
    $gameVariables.setValue($gameVariables.value(1386 + i), [1,2,3,4,5,6,7,8,9]);
    
    }
  13. @TESTOSTERONE That option completely slipped passed me. Thanks!
  14. ZombieKidzRule said:
    for (let i = 0; i < 11; i++) {

    let index = $gameVariables.value($gameVariables.value(1386)).indexOf($gameVariables.value($gameVariables.value(1387)));

    let x = $gameVariables.value($gameVariables.value(1386)).splice(index, 1);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +1));

    } //This is for arrays 1402 - 1412

    $gameVariables.setValue(1386, 1419); //This sets the variable for the array to 1419

    for (let z = 0; z < 3; z++) {

    index = $gameVariables.value($gameVariables.value(1386)).indexOf($gameVariables.value($gameVariables.value(1387)));

    x = $gameVariables.value($gameVariables.value(1386)).splice(index, 1);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +1));

    } //This is for arrays 1419 - 1421

    $gameVariables.setValue(1386, 1428); //This sets the variable for the array to 1428

    for (let y = 0; y < 6; y++) {

    index = $gameVariables.value($gameVariables.value(1386)).indexOf($gameVariables.value($gameVariables.value(1387)));

    x = $gameVariables.value($gameVariables.value(1386)).splice(index, 1);

    ($gameVariables.setValue(1386, $gameVariables.value(1386) +9));

    } //This is for arrays 1428, 1437, 1446, 1455, 1464, and 1473
    I wouldn't mess with code that works, but I like to live life on the edge and make up things that I don't understand after a few months... haha jk.

    You don't need to set the variable x if you are not going to use it in your script call or immediately. Also, leaving out let/const (or even var) when you are inside the scope of the script call makes it a global variable. Your first let index and let x is inside a for loop so it is only applicable within that scope, making all other declarations global (type in x or index in your console/dev tool and it will show you a value that is available globally).

    I like using map for something like this:

    Edit: (this also means you won't need variable 1386, but what I will do is make the array variable 1386):

    JavaScript:
    /* Shorthand to reference data in game variables -
       use v[#] to get/set data */
    const v = $gameVariables._data;
    /* Map the specific array to these data conditions -
       puts all the data into a very specific array
       For reference, it contains 1402-1412, 1419-1421,
       1428, 1437, 1446, 1455, 1454, and 1473 */
    v[1386] = [...Array( 21 )].map( ( _, i ) =>
        i < 11 ? 1402 + i :
        i < 14 ? 1408 + i :
        1428 + ( i - 15 ) * 9
    );
    /* For-of loop to iterate through variable 1386 and
       specifically splice the values from the
       arrays inside those variables */
    for ( const id of v[1386] ) {
        v[id].splice( v[id].indexOf( v[v[1387]] ), 1 );
    }
    I haven't checked this code thoroughly, but it should work... at least in my head at the moment haha.
  15. @bass2yang Thanks for this! I don't understand what everything is doing, but I will do some additional research of those portions and do some experiments.

    Thanks for the note about global variables if you don't use let, var, or const. I was thinking that since it was in the same script, the subsequent uses were referring back to what was already declared and the whole script was block scope. But I forgot about the for loop scope. I'm still learning about scope in scripts.

    I haven't read anything about map yet so I will check that out more.

    Thanks again!
  16. ZombieKidzRule said:
    @bass2yang Thanks for this! I don't understand what everything is doing, but I will do some additional research of those portions and do some experiments.

    Thanks for the note about global variables if you don't use let, var, or const. I was thinking that since it was in the same script, the subsequent uses were referring back to what was already declared and the whole script was block scope. But I forgot about the for loop scope. I'm still learning about scope in scripts.

    I haven't read anything about map yet so I will check that out more.

    Thanks again!
    Coming back to this, you could do it all in one for loop as well :

    JavaScript:
    const v = $gameVariables._data;
    for ( let i = 0; i < 21; i++ ) {
        const id = i < 11 ? 1402 + i :
                   i < 14 ? 1408 + i :
                   1428 + ( i - 15 ) * 9;
        v[id].splice( v[id].indexOf( v[v[1387]] ), 1 );
    }

    This way, you are only dealing with one loop instead of creating an array, mapping it, and then looping through the values. We can just assign a variable inside that loop.

    Go with what is most readable to you. You aren't dealing with a huge array or loop so you won't experience a crazy performance hit. We're already using an eval() with script calls so that is the true performance hit haha.
  17. @bass2yang Thanks for this! This is definitely easier for me to understand what is going on. I will experiment with this a little later and see what happens.
  18. bass2yang said:
    Coming back to this, you could do it all in one for loop as well :

    JavaScript:
    const v = $gameVariables._data;
    for ( let i = 0; i < 21; i++ ) {
        const id = i < 11 ? 1402 + i :
                   i < 14 ? 1408 + i :
                   1428 + ( i - 15 ) * 9;
        v[id].splice( v[id].indexOf( v[v[1387]] ), 1 );
    }

    This way, you are only dealing with one loop instead of creating an array, mapping it, and then looping through the values. We can just assign a variable inside that loop.

    Go with what is most readable to you. You aren't dealing with a huge array or loop so you won't experience a crazy performance hit. We're already using an eval() with script calls so that is the true performance hit haha.
    @bass2yang This works perfectly and is EXACTLY what I wanted to ask for, but wasn't sure how to explain it in words! Thank you so much for taking the time to share this!

    I read through it and was about 90% sure of what it was doing in each step so I had Google AI Mode break down each line to explain it and to confirm I understood what was going on. So, I know how to modify it to change which arrays are being accessed each time I need to do this. I am doing this for each Sudoku puzzle square as the puzzle is being generated.

    This reduces my previous script significantly and is something I can definitely utilize in the future beyond this project.

    Thanks again for helping me learn something new!

    EDIT: It appears I spoke to soon. I just noticed a small issue with Variable #1419. I sent you a direct message hoping you can help me figure out what I am doing wrong.
  19. Hey, so I set up a common event that has this requirement as a conditional $gameParty.aliveMembers().filter(member => !member.isRestricted()).length >= 2. I wanted to also stipulate that the party leader has to be one of those actors who isn't restricted in order for the common event to run. How would I go about doing that?
  20. K_Bowman said:
    Hey, so I set up a common event that has this requirement as a conditional $gameParty.aliveMembers().filter(member => !member.isRestricted()).length >= 2. I wanted to also stipulate that the party leader has to be one of those actors who isn't restricted in order for the common event to run. How would I go about doing that?
    you could try this:
    Code:
    $gameParty.leader() && $gameParty.aliveMembers().filter(member => !member.isRestricted()).length >= 2

    which include the game party leader (restricted or not) but if it must NOT restricted,
    than I don't know, maybe: $!gameParty.leader().isRestricted()?