gadget - Set/GetConfigData

gadget - Set/GetConfigData

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

Moderator: Moderators

Post Reply
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

gadget - Set/GetConfigData

Post by qray »

I am trying to save the (adjustable) window position of a koth mutator gadget inside a map. So I wanted to use the Set/GetConfigData functions (haven't found any docu on them, just examples here in the forum).
But the data is not saved/restored. I then added echos to see what happens and they do not seem to be called at all (not echo entries in the infolog). The script itself seems to run without errors.

Do Set/GetConfigData not work in gadgets? Or I am doing sth. wrong? I just added the code below to the unsynced part of the gadget, in which kothposx and kothposy are local variables giving the position of the window.

Where should the data be stored? I didn't find any entry of my data in the mod config file (in luaui/config); which is not very surprising since the function do not seem to be called :wink:

I would appreciate any tips, since I am running out of ideas.

Code: Select all

    
function widget:GetConfigData()
    --Spring.Echo("Widget KotH: Saving config data: kothposx = " .. kothposx .. " / kothposx = " .. kothposy )
    local data = {}
    data.koth={}
    data.koth.kothposx = kothposx
    data.koth.kothposy = kothposy
    return data
end  

function widget:SetConfigData(data)
    --Spring.Echo("Widget KotH: Loading config data ..." )
    if (data.koth ~= nil) then
        if ( data.koth.kothposx ~= nil ) then
            kothposx = data.koth.kothposx
        end
        if ( data.koth.kothposy ~= nil ) then
            kothposy = data.koth.kothposy
        end   
    else 
        --Spring.Echo("Widget KotH: Config data was nil ..." )
    end
end  
EDIT: corrected the posted code, since it contained an error.
Last edited by qray on 21 Mar 2016, 00:29, edited 1 time in total.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: gadget - Set/GetConfigData

Post by Kloot »

Do Set/GetConfigData not work in gadgets?
No, only in widgets.
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: gadget - Set/GetConfigData

Post by qray »

That explains it...

Thanks for the quick reply :-)
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: gadget - Set/GetConfigData

Post by Jools »

You can use Set/GetConfigInt for saving stuff from gadgets, but make sure the names don't overlap with existing names of game parameters. My suggestion is to prefix everything with the prefix "tp_"
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: gadget - Set/GetConfigData

Post by qray »

Thanks for the hint Jools. But if I get it right, Set/GetConfigInt would write into the engine config, which seems a bit hacky to me since it clutters the file. Or is this a commonly used way?

I could also think of separating the display part into a widget and then use the Set/GetConfigData there. Wouldn't this be a cleaner way?
If I get it right the data exchange from unsynced gadget to widget could be done could be done via "Script.LuaUI". Though I haven't found much docu on this and am still unsure how it works exactly...
GG seems also possible for communication, but if I understand it right, this shouldn't be the first choice.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: gadget - Set/GetConfigData

Post by Silentwings »

if I get it right, Set/GetConfigInt would write into the engine config, which seems a bit hacky to me since it clutters the file. Or is this a commonly used way?
You are right, and the general convention is not to do it except for really important things (e.g. CUS).
I could also think of separating the display part into a widget and then use the Set/GetConfigData there. Wouldn't this be a cleaner way?
Yes, also the standard solution.

If I get it right the data exchange from unsynced gadget to widget could be done could be done via "Script.LuaUI". Though I haven't found much docu on this and am still unsure how it works exactly...
http://imolarpg.dyndns.org/trac/balates ... _stats.lua, http://imolarpg.dyndns.org/trac/balates ... damage.lua, for example.

Note that it's for unsynced gadget -> widget comms, and not the other way around!
GG seems also possible for communication, but if I understand it right, this shouldn't be the first choice.
It's a global table accessible gadgets, but not accessible to widgets. Widgets have their own global table, WG.
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: gadget - Set/GetConfigData

Post by qray »

Thanks for the quick and very helpful answer, Silentwings. The example makes the usage clear. :-)
Silentwings wrote:Note that it's for unsynced gadget -> widget comms, and not the other way around!
Since the widget will only display time counters and not set/change anything, this solution fits perfect.
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: gadget - Set/GetConfigData

Post by qray »

For completeness, to make the "Script.LuaUI" thing easily understandable for somebody new -as me- reading this :-)

Add in the unsynced part of the gadget a function that sends the info, here times (the if statement checks if the receiver exists in a widget). In my case this is inside a loop over all teams:

Code: Select all

if ( Script.LuaUI("SendKOTHTimers") ) then
    Script.LuaUI.SendKOTHTimers(team, newTime)
end 
In the initialization of the widget register the receiver:

Code: Select all

widgetHandler:RegisterGlobal('SendKOTHTimers', ReceiveKOTHTimers) 
and add to the widget a function (same name as given in the registration) that does something with the sent data (here: update the teamTimers list with the new times):

Code: Select all

function ReceiveKOTHTimers(te,ti)
    teamTimers[te]=ti
end
NOTE: the receiver function musn't have "widget:" in front of it (took me an hour to find this error :wink:)

and to make it complete and clean, deregister the connection when the widget shuts down:

Code: Select all

function widget:Shutdown()
    widgetHandler:DeregisterGlobal('SendKOTHTimers')
end
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: gadget - Set/GetConfigData

Post by Jools »

Btw, you are aware of the existence of previous KOTH gadgets?
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: gadget - Set/GetConfigData

Post by qray »

Yes, the one I use since some time and extend currently again -based on input from players- was initially from Alchemist. I also saw that some games come with this option (was a while ago that I looked, when I made these two maps which include the gadget. Unsure how it is now).

This script works well, but there's always room for improvement - as the one now, to store the position of the movable hill time display. Done :-)
In the past, I had added the option (as map option) to adjust the needed hill time and that aircrafts can control the zone, or not.

And still on my list is the map option that you can get your commander back after a certain waiting time if you loose it. Since in this gadget you can only gain control of the hill zone with your com, teams who loose their commander have completely lost the game. Probably more fun if they have some chance to get back into the game...
Post Reply

Return to “Lua Scripts”