Jump to content
Luft08

[Solved] Using Global Variables

Recommended Posts

I wrote a small script to allow troops and vehicles to patrol between marker positions. However to do so I use a global variable and because multiple units call the same script multiple times I believe I have a race condition.  My code:

if (isServer) then {
	_destinationNumber = round(random 50) + 1;
	vehicleStr = _this select 0;
	_current_waypoint = "patrol_marker_"+str(_destinationNumber);
	_marker_pos = getMarkerPos _current_waypoint;
	deleteWaypoint [group vehicleStr, 0];
	_wp = group vehicleStr addWaypoint [_marker_pos, 0];
	_wp setWaypointType "MOVE";
	_wp setWaypointStatements ["true", "[vehicleStr] execVM 'chooseWaypoint.sqf';"];
};

The function is named "chooseWaypoint.sqf" and calls itself in the "On ACT" event of the waypoint. In order to pass the vehicle information I use the global variable "vehicleStr". It occurred to me that since many vehicles and troops are calling this, the global variable will get changed for all vehicles running the script. so I have a couple of questions:

 

1. Is there a way to make the script run in it's own space for each vehicle? 

2. should I move all of the code except the random number out side of the "if (is Server) then {"  statement so that vehicles on non-server clients will get the waypoints? I could make a public variable for the destinationNumber.

 

Sorry about so many beginner questions.

Share this post


Link to post
Share on other sites
6 hours ago, Luft08 said:

vehicleStr = _this select 0;

you don't need a public variable since you pass the object to the script.
 

 

6 hours ago, Luft08 said:

_wp setWaypointStatements ["true", "[vehicleStr] execVM 'chooseWaypoint.sqf';"]

setWayPointStatement passes the group as this so you can just do:
 

_wp setWaypointStatements ["true", "[this] execVM 'chooseWaypoint.sqf';"];

since the parameter passed is a group you will have to change

deleteWaypoint [group vehicleStr, 0];
//to
deleteWaypoint [(_this select 0), 0];

assuming you call it from the group leader's initbox at first call it with

[group this] execVM 'chooseWaypoint.sqf';


 

Share this post


Link to post
Share on other sites

Thanks for your reply. Sorry it took so long to get back to you but the script has a problem and I didn't want to reply before trying everything I could think of doing to fix the problem. Here is the code with changes:

if (isServer) then {
	_destinationNumber = round(random 8) + 1;
	_current_waypoint = "patrol_marker_"+str(_destinationNumber);
	_marker_pos = getMarkerPos _current_waypoint;
	deleteWaypoint [(_this select 0), 0];
	_wp = group vehicleStr addWaypoint [_marker_pos, 0];
	_wp setWaypointType "MOVE";
	_wp setWaypointStatements ["true", "[this] execVM 'chooseWaypoint.sqf';"];
};

It throws an invalid number at line 8. I tried putting both

[group this] execVM "chooseWaypoint.sqf";

as well as 

[this] execVM "chooseWaypoint.sqf";

Again, thanks for your help.

 

P.S.

I also tried changing line 8 to:

_wp setWaypointStatements ["true", "[_this] execVM 'chooseWaypoint.sqf';"];

 

Share this post


Link to post
Share on other sites

Hi!

 

First, you forgot to change that line:

_wp = group vehicleStr addWaypoint [_marker_pos, 0];

with this:

_wp = (_this select 0) addWaypoint [_marker_pos, 0];

 

and Secondly it seems that you have some invisibles characters at line 8 (precisely in the name of your script chooseWaypoint.sqf) so create a new and empty sqf file and retype the code entirely or copy paste this one (retype even the name of the file by yourself) :

if (isServer) then {
	_destinationNumber = round(random 8) + 1;
	_current_waypoint = "patrol_marker_"+str(_destinationNumber);
	_marker_pos = getMarkerPos _current_waypoint;
	deleteWaypoint [(_this select 0), 0];
	_wp = (_this select 0) addWaypoint [_marker_pos, 0];
	_wp setWaypointType "MOVE";
	_wp setWaypointStatements ["true", "[group this] execVM 'chooseWaypoint.sqf';"];
};

If your script isn't found at mission start retype yourself the name of the script in the init field of your squad leader. Don't copy/paste the old one 😉

Share this post


Link to post
Share on other sites

Thank you! It works!  I never would have found the invisible character and have no idea how it got there.

Share this post


Link to post
Share on other sites
On 6/25/2019 at 6:14 PM, Luft08 said:

Thank you! It works!  I never would have found the invisible character and have no idea how it got there.

it's a weird forum bug with copying code from forum...

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, sarogahtyp said:
On 6/25/2019 at 7:14 PM, Luft08 said:

Thank you! It works!  I never would have found the invisible character and have no idea how it got there.

it's a weird forum bug with copying code from forum... 

 

I think that this bug is here to stay !

Spoiler

hqdefault.jpg

Can a Moderator check this ?

Thanks !

  • Haha 1

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

×