Page 1 of 1
Reading global variable in "Unsynced" returns false value
Posted: 09 Nov 2010, 13:36
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?
Re: Reading global variable in "Unsynced" returns false value
Posted: 09 Nov 2010, 13:40
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.
Re: Reading global variable in "Unsynced" returns false value
Posted: 09 Nov 2010, 13:47
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?
Re: Reading global variable in "Unsynced" returns false value
Posted: 09 Nov 2010, 14:15
by Kloot
It's not. Use SendToUnsynced or the _G/SYNCED tables.
Re: Reading global variable in "Unsynced" returns false value
Posted: 09 Nov 2010, 18:04
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.
Re: Reading global variable in "Unsynced" returns false value
Posted: 09 Nov 2010, 18:41
by Argh
Didn't they agree not to break _G ?
Re: Reading global variable in "Unsynced" returns false value
Posted: 15 Nov 2010, 20:00
by Pithikos
Thanks a lot!! I finally fixed the thing

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.
Re: Reading global variable in "Unsynced" returns false value
Posted: 16 Nov 2010, 18:00
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...
Re: Reading global variable in "Unsynced" returns false value
Posted: 16 Nov 2010, 18:16
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?
Re: Reading global variable in "Unsynced" returns false value
Posted: 16 Nov 2010, 18:31
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.
Re: Reading global variable in "Unsynced" returns false value
Posted: 16 Nov 2010, 18:52
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.
Re: Reading global variable in "Unsynced" returns false value
Posted: 16 Nov 2010, 20:28
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.
Re: Reading global variable in "Unsynced" returns false value
Posted: 16 Nov 2010, 21:52
by knorke
Anyway I took the initiative and updated the wiki(you will excuse me) with the example given from zwzsg.
yea no problem
