Jump to content
Sign in to follow this  
RoryRothon

Global variable - how to?

Recommended Posts

Hi (again), i have setup a loop to add alive players into an array and remove dead.

Want i'm struggling to grasp, is how i can in-fact access variables from further down in my sqf file?

I'm trying to learn SQF, and i am really enjoying it! But this is confusing the hell out of me lol

 

As always, i am extremely grateful of any and all help..

Kind regards

Rory...

 

This is what i have setup:

_loopCheck = [] spawn {

	_displayResults = true;

	while { _displayResults } do {

		_alivePlayers = [];

		{

			if ( alive _x && isPlayer _x ) then { _alivePlayers = _alivePlayers + [_x] };
			if ( !alive _x && isPlayer _x ) then { _alivePlayers = _alivePlayers - [_x]; };

		} forEach allPlayers - switchableUnits;

		_aliveCount = count _alivePlayers;

		sleep 2;

	};

};

 

Share this post


Link to post
Share on other sites

Hi Rory,

 

LOL - glad you figured it out... Globals are easy - just leave the "_" off the beginning - then it's available not only in your current script but any other script you might use... As I assume you've gathered - the script runs in sequential order - so you can't use variables you haven't declared until you get to them... If you write any functions - same deal - I usually place them in the very beginning so they are immediately available...

 

I'm no expert - but - if you use [spawn] I don't think your [sleep] will work - [spawn] is scheduled to run only when there is time available in the game engine - so [sleep]'s don't work - you might want to look at [call] as that's sequential - everything needs to be done in order - and the script processing will wait for [sleep]'s to process before moving on... I'm probably not explaining it 100% - but I think that's the gist... [spawn] is something you fire off when you don't need the result to continue...

 

If you're new to this - hope your editing with something that gives you the ArmA Syntax color coding - something like Notepad ++ with an ArmA script plugin... If you need more info just shout...

http://killzonekid.com/arma-2-arma-3-hybrid-syntax-highlighter-for-notepad/

 

Here's some great background info on variables:

http://killzonekid.com/arma-scripting-tutorials-variables-part-2/

 

Bookmark that site as there is a TON of very useful scripting help...

 

Best of luck...

 

Regards,

Scott

Share this post


Link to post
Share on other sites
40 minutes ago, RoryRothon said:

Nevermind, i have sussed it, blonde moment over......

 

Regards,

Scott

  • Like 1

Share this post


Link to post
Share on other sites
Quote

I'm no expert - but - if you use [spawn] I don't think your [sleep] will work

Maybe this instead of sleep?

 

_timer = time + 2;
waitUntil { time > _timer };

 

Share this post


Link to post
Share on other sites
33 minutes ago, RoryRothon said:

Maybe this instead of sleep?

 


_timer = time + 2;
waitUntil { time > _timer };

 

 

Hi Rory,

 

I think I'm not explaining it well - here is an excerpt from KK's blog:

 

 

Quote

Today I’d like to talk a bit about scheduled and non scheduled environment in Arma.

 

Remember when you were a kid and you were playing your favourite video game and your mum told you to go do something and you were like “leave me alone” like you had a choice? This is how it works in Arma. Non scheduled environment is just that: script calls that need immediate attention, such as event handler calls. What good is event handler if you get notification of an event sometime after it happened and not immediately when it happens? At this point Arma will suspend its regular scheduled execution and give priority to those non scheduled events.

 

This is also the reason why you cannot use sleep or uiSleep or waitUntil commands in non scheduled environment. These scripts have to complete as fast as possible to let Arma return back to scheduled environment. This is also the reason any while loop in non scheduled environment will exit after 10,000 iterations. Using sleep command in a script is a good way to test what environment the script is running, in case you don’t know how it was originated. Non scheduled environment will throw an error. How can you not know? With many scripts calling scripts calling scripts it is easy to lose track of the origin. If you call a script from scheduled environment, called script will also run in scheduled environment and so on. With non scheduled environment all scripts will be non scheduled.

 

Non scheduled environments: CfgFunctions with preInit = 1, all event handler scripts including object init fields, onXXXXXX events (onEachFrame, onMapSingleClick etc.), Arma 3 debug console exec, FSM scripts. Scheduled environments: CfgFunctions with postInit = 1, various event scripts such as initPlayerLocal.sqfinitPlayerServer.sqf etc., and of course the init.sqf. You can start scheduled environment inside non scheduled by using spawn or execVM commands. Because none of them is expected to come back to the script they are executed from, the engine doesn’t wait. If you use call command, then the engine has no choice but to wait for its return. So be wise choosing which command to use when.

The scheduled environment does not guarantee the order or priority of execution of queued scripts. The engine will simply do it when it is feasible and possible. If you have too many non scheduled calls and they also take time to complete, your scheduled scripts can be delayed significantly, we are talking minutes! For the same reason non critical scripts should be run in scheduled environment so that a) they do not get on the way of urgent executions, b) give engine more flexibility for rescheduling when needed.

 

 

Regards,

Scott

Share this post


Link to post
Share on other sites
On ‎7‎/‎5‎/‎2014 at 7:56 AM, sxp2high said:

You can sleep in called code, but it will pause the scope, while spawn will create a new virtual machine (VM).

 


call { sleep 5; };
hint "hello world."; // Hint will be shown after 5 sec.
 

 

 


[] spawn { sleep 5; };
hint "hello world."; // Hint will be shown without delay
 

 

 

Example: I think you would want to use [call] vs [spawn] ???

 

Regards,
Scott

Share this post


Link to post
Share on other sites

Why don't you use the updated allPlayers command?

AllPlayers select {alive _x}  more exactly in your case, everywhere you need that.

 

 

  • Like 2

Share this post


Link to post
Share on other sites
12 hours ago, RoryRothon said:

Hi (again), i have setup a loop to add alive players into an array and remove dead.

I doubt you need to create a loop for this kind of thing as this line

(allPlayers - switchableUnits) select {alive _x && isPlayer _x};

gives the same array as your loop's variable. And to my knowledge even this line:

allPlayers select {alive _x};

 

 

10 hours ago, scottb613 said:

 

Example: I think you would want to use [call] vs [spawn] ???

 

Regards,
Scott

https://community.bistudio.com/wiki/sleep

 

A good rule of thumb is that when you call a code or a function there can't be any sleep or waitUntils as it needs to be completed with haste, and when there needs to be condition checks and waiting, or just waiting, you use spawn or execVM.

 

But there seems to be an exception as well:

"A called function may only use suspension (sleep, uiSleep, waitUntil) if it originates in a scheduled environment. If the called function originates in a non-scheduled environment it will return a generic error."

https://community.bistudio.com/wiki/call

  • 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
Sign in to follow this  

×