Rook Mk1 46 Posted April 25, 2020 So I want to change a local variable in a script with an add action command. Example _action = { _var = false; _unit addAction ["Change var", {_var = true}]; }; However due to addaction being its own scope, I am unable to change the var in the action code, any ideas how to do so? Share this post Link to post Share on other sites
gc8 970 Posted April 25, 2020 make _var a global variable: myvar = false then myvar = true in the action script Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 25, 2020 1 minute ago, gc8 said: make _var a global variable: myvar = false then myvar = true in the action script No, beacuse it is specific to a unit and it would affect other units which share the same script. Share this post Link to post Share on other sites
gc8 970 Posted April 25, 2020 or if you dont want to use global variable, maybe something like: _unit setVariable ["test", false]; _unit addAction ["Change var", { (_this select 0) setVariable ["test", true]; }]; 1 Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 25, 2020 1 minute ago, gc8 said: or if you dont want to use global variable, maybe something like: _unit setVariable ["test", false]; _unit addAction ["Change var", { (_this select 0) setVariable ["test", true]; }]; Oh cool, i'll give it a shot Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 25, 2020 @gc8 How do you then check for the variable in an if condition? Share this post Link to post Share on other sites
gc8 970 Posted April 25, 2020 4 minutes ago, Rook Mk1 said: @gc8 How do you then check for the variable in an if condition? if( _unit getVariable "test" ) then // Is true? { }; 1 Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 25, 2020 18 minutes ago, gc8 said: if( _unit getVariable "test" ) then // Is true? { }; I keep getting error missing ) Share this post Link to post Share on other sites
gc8 970 Posted April 25, 2020 12 minutes ago, Rook Mk1 said: I keep getting error missing ) can't see anything wrong with that if code I posted. maybe error elsewhere? Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 25, 2020 17 minutes ago, gc8 said: can't see anything wrong with that if code I posted. maybe error elsewhere? Yeah I’ve stopped for now, but I’ll try again later. It may be a bug, because even if I put something else in the condition after it throws up the same error until I do the line again. Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 25, 2020 I cut and pasted everything into a new script which fixed it for some inexplicable reason. 1 Share this post Link to post Share on other sites
ZaellixA 383 Posted April 25, 2020 Keep in mind that copying and pasting from the forums may produce some errors. Last time I checked there was some kind of bug which resulted in code being copied incorrectly. Could be this exact thing, or it may have been fixed. Can't be sure about it, but until someone confirms the problem has been solved I recommend avoiding direct copy-paste from forums. Finally, glad this worked for you :). 2 Share this post Link to post Share on other sites
Harzach 2507 Posted April 25, 2020 47 minutes ago, ZaellixA said: Keep in mind that copying and pasting from the forums may produce some errors Indeed, I see special characters getting pasted as "curly" versions a lot, most often with quote marks. A real PITA, but it's worth it to take the time to type out your copied code. 1 Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 26, 2020 Another question, would it be possible to assign setVariable to an addaction command. For instance instead of _actionID = player addaction "etc"; It could be: player setVariable ["actionID", addAction "etc"]; then i could remove it using: player removeAction actionID or something. Share this post Link to post Share on other sites
Harzach 2507 Posted April 26, 2020 Just make the ID global. Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 26, 2020 9 minutes ago, Harzach said: Just make the ID global. Again I can’t really do that since it’s used by more than one unit. Share this post Link to post Share on other sites
gc8 970 Posted April 26, 2020 It's a client side script since you are using the player variable. Each player has their own global variables so it shouldn't matter 1 Share this post Link to post Share on other sites
gc8 970 Posted April 26, 2020 If you still want to use setVariable, it would look something like this: player setVariable ["actionID", addAction "etc"]; // Rest of addaction code here _id = player getvariable "actionID"; player removeAction _id; 1 1 Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 26, 2020 Are we talking global setVariable or just var = addaction? Share this post Link to post Share on other sites
Rook Mk1 46 Posted April 26, 2020 2 minutes ago, gc8 said: If you still want to use setVariable, it would look something like this: player setVariable ["actionID", addAction "etc"]; // Rest of addaction code here _id = player getvariable "actionID"; player removeAction _id; Okay, cool Share this post Link to post Share on other sites
gc8 970 Posted April 26, 2020 2 minutes ago, Rook Mk1 said: var = addaction? this :) Share this post Link to post Share on other sites
Harzach 2507 Posted April 26, 2020 1 hour ago, Rook Mk1 said: Again I can’t really do that since it’s used by more than one unit. In addition to @gc8's explanation, both addAction and removeAction have local effect, so it wouldn't matter. 2 Share this post Link to post Share on other sites