Variable in conditional statement does not update when re-ran

This is the code:

Code:
var partymember = $gameActors.actor( $gameVariables.value(7).toString() );

var num = $gameVariables.value(10);



if (partymember.weapons().includes($dataWeapons[num]))

{this._params = [num];

this.command119();

console.log("hello?");

}

console.log(num)

These are the screenshots:
top.PNG

bottom.PNG


Expected: When no weapon is equipped, jump to label end. When any weapon is equipped, script logs "Hello?" (which means it should be jumping to labels?), the current variable number, and ends at 21
Result: When no weapon is equipped, jumps to label end. When any weapon is equipped, script logs 'Hello?" one time (and does not jump to 1 even when the weapon with id 1 is equipped) and then infinitely increases and logs the variable:
log.PNG

Update: The title is misleading. It does actually update. The issue seems to be that the jump to label in the script isnt working.
Jump to label is loop (back up to loop)
you never use label 1 through 20, so how is those called?

where do you exactly "break the loop" or what is the expected end result
you try to achieve?
im not entirely sure to what end youre doing this, but youve only created an infinite loop.
just cause the labels are named doesnt mean they correspond to the variables. your very first "jump to label" in the sequence will unconditionally send you back to the loop label with no way to break it because there arent any conditions to not do it.
script code and jumps should be used when there's absolutely no way of bending the existing structures to achieve what you need.

...from which the question stems: WHAT do you need, that requires you to not-use the preset blocks?
Code:
var partymember = $gameActors.actor( $gameVariables.value(7).toString() );
Why are you doing this? The actor() method expects a numeric value - the ID of an actor - not a string.

Code:
{this._params = [num];
Here you have the opposite problem - the parameter for jump to label should be a string, the text of the label. That's why it's not jumping to any labels, so your code is always hitting the first proper Jump to Label event command, which is back up to loop.

Aside from the syntactical errors in your code, I'm rather confused about the entire structure of the event.

Why are you constructing this elaborate faux loop with 23 labels when there's an actual Loop event command? Just do a Loop, Control Variables to increase by one, then Conditional Branch to see if it's 21 and Break Loop.

Then there's no need to rely on typing as much code for it to work, and it takes up a fraction of the space in your event.
Expected: When no weapon is equipped, jump to label end. When any weapon is equipped, script logs "Hello?"
It's probably more efficient to focus on exactly what you need rather than checking for 21 possible weapons so I'll try to make something that reflects this statement (without the label stuff since it really isn't needed based on what I am seeing so far).

This script checks if there is anything in weapons for actor 1. If there is, show a console log. If not (making the slot undefined, which means nothing is equipped), go on.
Try this instead:
JavaScript:
◆If:Script:$gameActors.actor(1).weapons()[0]
  ◆Script:console.log('hello');
  ◆
:End
s1.png

From there, if you must get other information, you can just add event commands as necessary.

Feel free to use $gameActors.actor($gameVariables.value(7)) or whatever instead if needed.
And if your goal is to get variable 10, ChipNum, to be the lowest ID of any equipped weapons, that takes one code statement.

Code:
let actor = $gameActors.actor($gameVariables.value(7));
let equipId = $dataWeapons.length;
equipId = !actor.weapons().length ? 0 : actor.weapons().forEach(weapon => {if (weapon && weapon.id < equipId) equipId = weapon.id;});
$gameVariables.setValue(10, equipId);
Jump to label is loop (back up to loop)
you never use label 1 through 20, so how is those called?

where do you exactly "break the loop" or what is the expected end result
you try to achieve?
Code:
this._params = [num];

this.command119();

Variable 10 is num, which is there.

im not entirely sure to what end youre doing this, but youve only created an infinite loop.
just cause the labels are named doesnt mean they correspond to the variables. your very first "jump to label" in the sequence will unconditionally send you back to the loop label with no way to break it because there arent any conditions to not do it.
but it doesnt ever jump to the labels and then continues looping the script even after 21 doesnt jump back

script code and jumps should be used when there's absolutely no way of bending the existing structures to achieve what you need.

...from which the question stems: WHAT do you need, that requires you to not-use the preset blocks?

Why are you doing this? The actor() method expects a numeric value - the ID of an actor - not a string.


Here you have the opposite problem - the parameter for jump to label should be a string, the text of the label. That's why it's not jumping to any labels, so your code is always hitting the first proper Jump to Label event command, which is back up to loop.

I fixed these, but it didn't seem to change much.

Why are you constructing this elaborate faux loop with 23 labels when there's an actual Loop event command? Just do a Loop, Control Variables to increase by one, then Conditional Branch to see if it's 21 and Break Loop.

Then there's no need to rely on typing as much code for it to work, and it takes up a fraction of the space in your event.

There seems to still be a bug where loops do not work, as i had another event that went from not doing anything to working from changing the loop out for labels and jump to.

It's probably more efficient to focus on exactly what you need rather than checking for 21 possible weapons so I'll try to make something that reflects this statement (without the label stuff since it really isn't needed based on what I am seeing so far).

I need to check all the weapons because each one should do a different effect at the start of a battle. I have just not yet put anything in place. I filled out the first few to show what I mean.

table.PNG



Update:
I just realized I didn't check for it being 22 and ending. I am very sorry.
but it doesnt ever jump to the labels and then continues looping the script even after 21 doesnt jump back
And I told you exactly why.

not making the actor id a string gives "TypeError: Cannot read property 'weapons' of null". That part of the script works fine with it being a string.
The ID is expected to be a number. JavaScript's loosey goosey typing may accommodate it, but all IDs in the engine are numbers.

1729398809086.png


1729398085216.png


It throwing an error when you use the correct data type doesn't mean it works fine when you use a string, it means something else is wrong in your variable values or how things are set up going into the event.


putting a ".toString()" next to variable 10 gives "TypeError: Cannot read property 'toString' of undefined"
It's not really helpful to just say errors without showing the exact syntax you used. But that would mean you are probably putting the function call in the wrong place, because $gameVariables.value() can't return undefined.

All I can say is I've both read the code for command119() and implemented this exact procedure, and the param is expected to be a string.

There seems to still be a bug where loops do not work, as i had another event that went from not doing anything to working from changing the loop out for labels and jump to.
There is no stock bug in MV where loops just don't work. I'm not sure why you'd jump to that assumption rather than troubleshooting for logical errors when you've already had errors pointed out here.

I need to check all the weapons because each one should do a different effect at the start of a battle. I have just not yet put anything in place. If it helps explain, imagine that 1 has a script that places a state on the user.
Why wouldn't you just use Yanfly's Auto Passive states and put a passive state on the weapon(s)?

Your whole response really comes off like you're defending your non-functional code. It doesn't work, which you know because you posted a thread saying it doesn't work. But then you sound like you're arguing with the people who are explaining why it doesn't work and how you could make it work.
Your whole response really comes off like you're defending your non-functional code. It doesn't work, which you know because you posted a thread saying it doesn't work. But then you sound like you're arguing with the people who are explaining why it doesn't work and how you could make it work.
i already got the code to work. The thing I was blind to was that it wouldn't actually go to 21 if you didn't have weapon 21. idk why I thought it would. I even optimized it by reorganizing the weapons that have a start effect to all be next to eachother so I don't have to go through the ones that dont.
Why wouldn't you just use Yanfly's Auto Passive states and put a passive state on the weapon(s)?
It's too limiting for what I'm trying to do. I need to have states that do expire and I need to have more than just user applied states. I do have it though for the few that do need a permanent state.

update: I optimized it and completely removed the need for the loop.
optimized.PNG

it gives an error for when the id doesn't have a label, but it still works fine.
It's too limiting for what I'm trying to do. I need to have states that do expire and I need to have more than just user applied states. I do have it though for the few that do need a permanent state.
a passive state doesnt have to be always on, nor does it have to actually be the effect. you can just use the state passive conditions to make the state "expire" by making it only active when the turn count is below x turns, or have the passive state grant the applied state at the start of the battle
a passive state doesnt have to be always on, nor does it have to actually be the effect. you can just use the state passive conditions to make the state "expire" by making it only active when the turn count is below x turns, or have the passive state grant the applied state at the start of the battle
it still does not let me do a lot of what i need to do with this, such as apply a state to all enemies when a party member has something equipped (without the use of a switch), easily allow for states to be ended early with damage, allow for follow up skills, skills that force the actor to preform another random one, etc.
if your procedures always lead up to $party.character(ID).addState(number), you should make that it's own function, generically.
you don't need 20 instructions to handle 20 cases... you need ONE instruction to handle case "whatever that pointer says"
if your procedures always lead up to $party.character(ID).addState(number), you should make that it's own function, generically.
you don't need 20 instructions to handle 20 cases... you need ONE instruction to handle case "whatever that pointer says"
This is very helpful, especially for the longer armor list that mostly use the same code with just the ids swapped out. Thank you!
it still does not let me do a lot of what i need to do with this, such as apply a state to all enemies when a party member has something equipped (without the use of a switch), easily allow for states to be ended early with damage, allow for follow up skills, skills that force the actor to preform another random one, etc.
youll have to explain in detail when and how your examples are supposed to work. just by everything youve described so far, all of those effects are easily doable through passives, conditions, and the yanfly's buffs and states plugin youre already running if youre using the passive plugin.
I need to check all the weapons because each one should do a different effect at the start of a battle. I have just not yet put anything in place. I filled out the first few to show what I mean.
Thank you - this is clearer since what you wrote from the earlier post was only "check for any or no weapon" instead. Please be sure to specify this from the beginning because it changes the approach and solution.

The solution I provided below will avoid any loop or label use which may or may not be what you want. That being said, it is another solution based on how I am interpreting what you are asking for (without using other plugins, which might be the better solution for you if you used those plugins instead).

Since I can't see all your states and whether or not it is for an actor or enemy, I will have to assume the rest of the code.

This is what I would do:
- put all my states into an array in order to coincide with weapons 1 through 21 since your states are ordered differently from the weapon index
- have a conditional branch that decides on whether the actor or the enemy gets the state (in this case, I am designating weapon numbers 6, 10, 18, 19, and 20 to add the state to the enemy instead of the actor)
- choose a random enemy (your code does not specify the index of the troop member so that part of it will not run properly when it gets to this part)
- make sure the code is 12 lines or less due to MV editor limitation
- use one script call
JavaScript:
const arrStates = [31, 29, 30, 2, 4, 13, 5, 9, 18, 11, 23,
                   25, 33, 40, 42, 22, 17, 7, 27, 39, 21],
       arrEnemy = [6, 10, 18, 19, 20], enemy = $gameTroop.members(),
          actor = $gameActors.actor($gameVariables.value(7)),
       weaponId = actor.weapons()[0] ? actor.weapons()[0].id : 0;
if (weaponId) {
    if (arrEnemy.includes(weaponId)) {
        enemy.forEach(e => e.addState(arrStates[weaponId - 1]));
    } else {
        actor.addState(arrStates[weaponId - 1]);
    }
};
I'm assuming that you have already established a value for Game Variable 7.
Hope this helps - otherwise, there are plenty of other solutions.

Edited to make state add to all enemies based on your other conversation.
It's too limiting for what I'm trying to do. I need to have states that do expire and I need to have more than just user applied states.
Passive states can do that, it takes one line of code.
it still does not let me do a lot of what i need to do with this, such as apply a state to all enemies when a party member has something equipped (without the use of a switch)
Yes, it does.

easily allow for states to be ended early with damage
That's a setting on the regular state the passive state would spawn.

allow for follow up skills, skills that force the actor to preform another random one
...yes, it does?

update: I optimized it and completely removed the need for the loop.
I'm not going to try to guess what your knowledge base or training is, but it really appears like you don't super know what you're doing in terms of JavaScript and are just throwing things that you find in different places together until it does something close to what you want.

This is somewhat reinforced by your other thread, where you appear to be trying to mix Ruby and JavaScript syntax together without having any idea that they're different.

Anyway, good luck achieving what you want with your project, but it's clear you don't actually want help with learning anything here, you just want to show off posts where you Frankenstein things together. That's not my jam.
Moved to Learning Javascript. Coding questions should be posted there in the future.

Original thread: https://forums.rpgmakerweb.com/threads/variable-in-conditional-statement-does-not-update-when-re-ran.172693/