Jump to content
  • 0

How to spawn a script from an addaction and then kill it from another (or the same) action item.


RimBlock

Question

Hi,

 

So I am looking for a way to spawn a sqf from an addaction menu item and then allow the player to kill the sqf spawn from either choosing the same addaction item (i.e. it works as a toggle) or another addaction item (on and off actions).

 

How to best handle this ?.

 

Direction, tutorials, scripts greatly appreciated.

 

Thanks

RB

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I will assume that someone way better at coding will answer shortly, but I would suggest this: 

 

- In your script, define a variable that tells it if it is on or not. 

- Provide a case option for when variable is not matching on state

- Provide a case option for when variable is matching on state

- In each case, you put the functionality in (ie, if on, turn it off, if off, turn it on).

Link to comment
Share on other sites

  • 0

You can use public booleans to do this:

 

toggle.sqf

if (mytoggleEnabled) then {
	mytoggleEnabled = false;
	systemChat ("Toggle Disabled");
} else {
	mytoggleEnabled = true;
	systemChat ("Toggle Enabled");
};

spawn.sqf

mytoggleEnabled = false;

if (_certainConditionIsMet) {
s_toggle = player addAction [("<t color=""#ffffff"">" + ("Toggle") + "</t>"),"myfolder\doAwesomeStuff.sqf"];

};
player removeAction s_toggle;
Link to comment
Share on other sites

  • 0

Thanks for taking the time.  My bad, I seem to have been unclear.

 

What I am trying to work out is how to control the spawned process rather than how to build a control structure for a toggle.

 

So...

 

[fn_selfActions.sqf]

if (s_player_awesome_script < 0) then {
   s_player_awesome_script = player addAction ["awesome_script on", "awesome_script_control.sqf", "1", 1, false];
}else
{
   s_player_awesome_script = player addAction ["awesome_script off", "awesome_script_control.sqf", "0", 1, false];
};

s_player_awesome_script is set to -1 in variables.sqf and the above code will call awesome_script_control.sqf with a variable ("on" or "off").

 

[awesome_script_control.sqf]

private ["_action"];

_action = _this 0;

player removeAction s_player_awesome_script;

if (_action == 1) then {
  s_player_awesome_script = 1;
  spawn player awesome_script;
}
else
{
  s_player_awesome_script = -1;
  terminate awesome_script;
};
It is maintaining the link for the spawned process that I am trying to confirm.
 
Do I need to create a variable in [fn_selfActions.sqf] to hold a handle to the spawned script and then pass it down to [awesome_script_control.sqf] which sets it with 
_script_handle = spawn player awesome_script;
and if so, how to pass it back from the [awesome_script_control.sqf] to [fn_selfActions.sqf] so it can be passed back to  [awesome_script_control.sqf] for the 
terminate _script_handle;

The addaction seems, from documentation, to only pass variables one way.

 

If that is the case, any better way than creating a public variable in [fn_selfActions.sqf] which is just set in  [awesome_script_control.sqf] ?.

 

Thanks.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...