I have multiple widgets using the same variables..

I have multiple widgets using the same variables..

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

Moderator: Moderators

Post Reply
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

I have multiple widgets using the same variables..

Post by smoth »

I have some coloring I am doing to the ui and a few other common values that I store in a config... problem is for each widget to use them I would need to put it in widget global(wg) so how can I ensure that it the var is in wg before the widget loads? as in how do I set the load precedence?

Also is this a good way to do it or would one of you recommend a different approach?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: I have multiple widgets using the same variables..

Post by knorke »

The simplest approach is to set the layer of the widget that sets up the WG variables to something extremely low, so that it initialized before the other widgets.
this, and maybe make the WG-loading widget an unsynced gadget so it can not be accidently disabled. (if unsynced gadget can write to WG but I think yes)
(or check in the other widgets if WG.bla ~= nil)
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: I have multiple widgets using the same variables..

Post by smoth »

Code: Select all

if gadgetHandler:IsSyncedCode() then
	--SYNCED
	function gadget:Initialize()
		BuildColorSets()
		GG.colors = colorSets
		_G.colors = colorSets
	end
	
else
	--UNSYNCED
	function gadget:Initialize()
		WG.colors = SYNCED.colors
	end	
end
tried in gadget..

attempt to index global 'WG' (a nil value)
result
SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: I have multiple widgets using the same variables..

Post by SirMaverick »

smoth wrote:
attempt to index global 'WG' (a nil value)
result
Use Script.LuaUI to send data to widgets.

Afaik you cannot access WG from gadgets. Even if you could, widgets are loaded after gadgets. So it wouldn't work in gadget:Initialize.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: I have multiple widgets using the same variables..

Post by knorke »

hm too bad, was not sure.
Guess then every widget should have something like
if not WG.myWidgetSettingStuff then ... end
in case the framework widget is disabled/failed.
I think in early CA some chili widgets did not have that and so clicking around in F11 could cause error spam.

btw what happend to Niobiums post? (the one i quoted)
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: I have multiple widgets using the same variables..

Post by smoth »

Maybe he deleted it? I figured you were quoting yourself.

I'll just have to make the widget load either as a widget or pass it peacemeal via Script.luaui lik chickens does but I would have to do it for each widget :/.

Kinda lame
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: I have multiple widgets using the same variables..

Post by Niobium »

Yeah the post was mine, I deleted it to do some actual testing of what I said, then when I came back to repost it knorke had quoted it, so that was good enough.

In regards to the widget problem, ideally users wouldn't be going into the widget list and disabling essential widgets, but there is another way: load the config outside of a callin.

Code: Select all

function widget:GetInfo()
    ...
end

WG.Config = {cat = 'meow'}
When the widget handler loads this widget from file (Which it always does - As it has no information about it otherwise) it executes the script, which sets up the WG variable. After loading the widget the handler has access to its name/callins/enabled state etc and may choose to disable it, but the variables have already been set.
Post Reply

Return to “Lua Scripts”