Jump to content
Rook Mk1

Setting variable in addaction

Recommended Posts

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

make _var a global variable: myvar = false then myvar = true in the action script

Share this post


Link to post
Share on other sites
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

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];


}];

 

  • Like 1

Share this post


Link to post
Share on other sites
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

@gc8

 

How do you then check for the variable in an if condition?

Share this post


Link to post
Share on other sites
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?
{

};

 

  • Like 1

Share this post


Link to post
Share on other sites
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
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
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

I cut and pasted everything into a new script which fixed it for some inexplicable reason.

  • Like 1

Share this post


Link to post
Share on other sites

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 :).

  • Like 2

Share this post


Link to post
Share on other sites
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.

  • Like 1

Share this post


Link to post
Share on other sites

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
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

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

  • Like 1

Share this post


Link to post
Share on other sites

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;

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Are we talking global setVariable or just var = addaction?

Share this post


Link to post
Share on other sites
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
2 minutes ago, Rook Mk1 said:

var = addaction?

 

this :)

Share this post


Link to post
Share on other sites
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.

  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×