Page 1 of 1

Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 04:56
by Forboding Angel
Occasionally I have a very real need to pass information form synced to unsynced or even just from widget to widget or gadget to gadget. The problem is that all of the infor I have found on the wiki for this is rather vague.

I understand that you use gadget/widget globals for this I.E. GG or WG, but how are these declared?

Is it simply GG.myVarName and suddenly widgets can pull the data from it? Does it require some sort of a go-between?

ELI5 plox

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 07:46
by gajop
GG - gadget globals
WG - widget globals

These tables are used for sharing data between addons in the same Lua interface. You cannot share between widget<->gadget nor synced_gadget <-> unsynced_gadget.

This is the relevant wiki page (https://springrts.com/wiki/LuaTutorials ... unications) - and it might need extending.

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 08:15
by Forboding Angel
So if I have synced data that I need to sent to send to unsynced, so for this example lets say gadget -> widget, then I would use RulesParams.

This doesn't help me though, not really anyway. If I have a variable set in synced gadgetspace, how do I make it so that a widget can read that variable data? Is it completely impossible? Keep in mind, I'm talking about reading from unsynced only, not writing.

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 08:22
by gajop
Forboding Angel wrote:So if I have synced data that I need to sent to send to unsynced, so for this example lets say gadget -> widget, then I would use RulesParams.
The alternative way of communicating is to use SendToUnsynced and then Script.LuaUI, but there's no guarantee of persistancy, as LuaUI might be reloaded afterwards.
Forboding Angel wrote:If I have a variable set in synced gadgetspace, how do I make it so that a widget can read that variable data? Is it completely impossible? Keep in mind, I'm talking about reading from unsynced only, not writing.
This is how you do it. In the gadget you set the gamerules param, which you should update each time your value changes.

Code: Select all

Spring.SetGameRulesParam("myVariable", myValue)
In the widget (or any Lua interface) you read the game rules param. Note that you won't know *when* it changed/being set, besides checking each frame and comparing to previous values.

Code: Select all

local myVariable = Spring.GetGameRulesParam("myVariable")

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 09:17
by Forboding Angel
Aha! The lightbulb shows a glimmer.

In the particular usage I need right now it would be update every 30 frames or so. I'll give it a whirl and see what I come up with :-)

Also, you never really answered how to use GG and WG. Do you just do GG.myVariableName? In another gadget/widget, how do you use it?
If I set something like GG.myVariableName = 5 in one gadget, can I then go to another gadget and say myCalc = GG.myVariableName + 5 and then myCalc = 10?

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 09:28
by gajop
Forboding Angel wrote:If I set something like GG.myVariableName = 5 in one gadget, can I then go to another gadget and say myCalc = GG.myVariableName + 5 and then myCalc = 10?
Correct. That will work as long as your assumption about the order of execution holds (it'll depend on the layer of the addon as well as what call you're invoking it in). Normally you'll just check if GG.myVariableName ~= nil before doing this calculation.
The best way to think of GG and WG is just as globally accessible tables.

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 09:44
by Silentwings
The best way to think of GG and WG is just as globally accessible tables.
They even are globally accessible tables, within their respective lua states.

RulesParams, SendToUnsycned, etc have more complicated implementations - the engine interacts there. I fully agree that lua comms page needs a rewrite, imo its by far the worst part of the lua docs. Had been meaning to do it one day.

Re: Explicit Instructions for using GG or WG

Posted: 22 Dec 2016, 12:24
by Forboding Angel
Very cool, thank you. Set and Get GameRulesParam is a really simple way of shifting data around, that is extremely helpful. I've tried to figure this out before, but frankly, the wiki docs on it made no sense to me whatsoever.

Honestly, Gajop's post on GameRulesParam should just be c&p to that section, barring better documentation.