Reading global variable in "Unsynced" returns false value

Reading global variable in "Unsynced" returns false value

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Pithikos
Posts: 146
Joined: 26 Dec 2008, 14:26

Reading global variable in "Unsynced" returns false value

Post by Pithikos »

I came across this little problem. Here is a simple test code:

Code: Select all

local number=10

if (gadgetHandler:IsSyncedCode()) then
  number=number+1
else
  Spring.Echo(number)
end
Someone would expect to be printed a list of increasing numbers but it seems that the echo prints just the number 10 as if it was a constant. If you move the "Spring.Echo(number)" to "Synced" then it works normally. Same if you move the "number=number+1" to "Unsynced".
Can someone explain this behaviour?
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: Reading global variable in "Unsynced" returns false value

Post by Kloot »

There are two gadget environments (synced and unsynced).

Variables declared in a gadget's global space actually exist twice, once in each environment.

In synced code ("if (gadgetHandler:IsSyncedCode())") the synced environment is active, in unsynced code the unsynced environment.
User avatar
Pithikos
Posts: 146
Joined: 26 Dec 2008, 14:26

Re: Reading global variable in "Unsynced" returns false value

Post by Pithikos »

Well how is it possible then to change dynamically the value of variable "number" inside the synced block of code and then read the updated value inside the unsynced block of code?
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: Reading global variable in "Unsynced" returns false value

Post by Kloot »

It's not. Use SendToUnsynced or the _G/SYNCED tables.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Reading global variable in "Unsynced" returns false value

Post by zwzsg »

Code: Select all

if (gadgetHandler:IsSyncedCode()) then

	local number = 10

	function gadget:SomeSyncedCallin(...)
		number=number+1
		_G.number=1
	end

else

	function gadget:SomeUnsyncedCallin(...)
		Spring.Echo(SYNCED.number)
	end

end
I believe _G. and SYNCED is not unique to your gadget but shared amongst gadgets, so instead of hoards of variables with dull names it is good practice to encapsulate your data into a well named table (ie, _G.MyGadgetTable={number=number} / SYNCED.MyGadgetTable.number).

Because of recent advances in multithreading Spring, this method of passing data from synced to unsynced through _G. and SYNCED. might soon get deprecated. Check changelog, stickies, and threads about multhithreading for details.

There are other methods than _G. / SYNCED to send data from synced to unsynced. See for instance set_alpha_threshold.lua inside Kernel Panic for a short exemple using SendToUnsynced.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Reading global variable in "Unsynced" returns false value

Post by Argh »

Didn't they agree not to break _G ?
User avatar
Pithikos
Posts: 146
Joined: 26 Dec 2008, 14:26

Re: Reading global variable in "Unsynced" returns false value

Post by Pithikos »

Thanks a lot!! I finally fixed the thing :mrgreen: I also followed your advice and used a table to encapsulate my variables.

Such small understandable examples should be in the wiki! Easy to follow and comprehend in contrast to the "mines" example they have there.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Reading global variable in "Unsynced" returns false value

Post by knorke »

Pithikos wrote:Such small understandable examples should be in the wiki! Easy to follow and comprehend in contrast to the "mines" example they have there.
You mean http://springrts.com/wiki/Lua_sync_to_unsync ?
If so, yea sorry that page was made by me, I know it sucks :?
But I thought some c&p example would be better than nothing...
User avatar
Pithikos
Posts: 146
Joined: 26 Dec 2008, 14:26

Re: Reading global variable in "Unsynced" returns false value

Post by Pithikos »

Well imagine that you are learning a new programming language/API and someone gives you an example of 3 pages long. Would be quite hard to locate the meat of what you are looking for. If you could break that thing to max 10 lines of code it would be gold worthy.

And btw why isn't wiki a real "wiki" in this site? There is big lack of documentation in lua, mods, etc and not allowing the public to add content just dries out knowledge and makes it a pain in the ass to get started with anything. :|

Where's the real opensource spirit?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Reading global variable in "Unsynced" returns false value

Post by knorke »

Well imagine that you are learning a new programming language/API and someone gives you an example of 3 pages long. Would be quite hard to locate the meat of what you are looking for. If you could break that thing to max 10 lines of code it would be gold worthy.
I know what you mean. This is all new for me too ;)
ie I do not know how "SendToUnsynced" works so I just put in the example of quantum as a link.
Would be nice if people with the knowledge would update the wiki, I can only spam my half-correct stuff in it because I hope that it will help at least a bit.
Maybe I will try to think of a shorter example but I this was the thing I was currently working on, so I just put it in. It is also a working examples that you can try out with ie XTA. (also with other mods but then needs changing of the unit names)
(will look like this: http://www.youtube.com/watch?v=eBiWbd3x33Q just without automatic unit moving, took that out to shorten the example)
not allowing the public to add content
everybody can add/edit stuff!
just log in with your forum account.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: Reading global variable in "Unsynced" returns false value

Post by SinbadEV »

knorke wrote:just log in with your forum account.
Yeah, like he said, you need to login to have the edit buttons be readily noticeable.
User avatar
Pithikos
Posts: 146
Joined: 26 Dec 2008, 14:26

Re: Reading global variable in "Unsynced" returns false value

Post by Pithikos »

knorke wrote:
Well imagine that you are learning a new programming language/API and someone gives you an example of 3 pages long. Would be quite hard to locate the meat of what you are looking for. If you could break that thing to max 10 lines of code it would be gold worthy.
I know what you mean. This is all new for me too ;)
ie I do not know how "SendToUnsynced" works so I just put in the example of quantum as a link.
Would be nice if people with the knowledge would update the wiki, I can only spam my half-correct stuff in it because I hope that it will help at least a bit.
Maybe I will try to think of a shorter example but I this was the thing I was currently working on, so I just put it in. It is also a working examples that you can try out with ie XTA. (also with other mods but then needs changing of the unit names)
(will look like this: http://www.youtube.com/watch?v=eBiWbd3x33Q just without automatic unit moving, took that out to shorten the example)
not allowing the public to add content
everybody can add/edit stuff!
just log in with your forum account.
was expecting to see some mines from that gadget :p Anyway I took the initiative and updated the wiki(you will excuse me) with the example given from zwzsg.
Yeah, like he said, you need to login to have the edit buttons be readily noticeable.
Quite hard to notice it when it's at the bottom of the page. Maybe it would be more practical to have an [edit] option in every page so if someone tries to edit a page it redirects to a login page just like in wikipedeia.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Reading global variable in "Unsynced" returns false value

Post by knorke »

Anyway I took the initiative and updated the wiki(you will excuse me) with the example given from zwzsg.
yea no problem ;)
Post Reply

Return to “Lua Scripts”