Gadget and Widget Cross Communication

Gadget and Widget Cross Communication

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

Moderator: Moderators

User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Gadget and Widget Cross Communication

Post by bobthedinosaur »

So.... Lets say I have a trigger/ condition that changes a local in one gadget. How can I get other gadgets and widgets to see this?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Gadget and Widget Cross Communication

Post by knorke »

there is a tablething GG that all gadgets can read, you need to put your variable in it.

http://answers.springlobby.info/questio ... created-it

http://answers.springlobby.info/questio ... munication

probally good idea to use GG.dinobob.bla instead of just GG.bla to avoid conflicts with other widgets.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

muhahahahahahahaha


YES!
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Gadget and Widget Cross Communication

Post by CarRepairer »

But you also want widgets to see it so GG won't work. You might want to instead use Spring.SetGameRulesParam(...) which can then be ready by widgets as well.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Gadget and Widget Cross Communication

Post by knorke »

isnt there WG? (not sure)
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Gadget and Widget Cross Communication

Post by CarRepairer »

Yes there's WG, but that's only for widgets. Bob wants both to to communicate from gadget to gadget/widget.
a trigger/ condition that changes a local in one gadget. How can I get other gadgets and widgets to see this?
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

how do i do
Spring.SetGameRulesParam(...)
??
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Gadget and Widget Cross Communication

Post by Google_Frog »

This gadget and this widget have gadget -> widget communication.

The important bits*


Gadget synced (anywhere in synced)

Code: Select all

SendToUnsynced("MexEnergyEvent", teamID, allyTeamData.teams, energyWasted, ODenergy,summedMetalProduction,summedOverdriveMetal, teamEnergy[teamID].totalChange, teamIncome)
Gadget unsynced

Code: Select all

function WrapToLuaUI(_,teamID, allies, energyWasted, energyForOverdrive, totalIncome, metalFromOverdrive, EnergyChange, teamIncome)
  if (teamID ~= Spring.GetLocalTeamID()) then return end
  if (Script.LuaUI('MexEnergyEvent')) then
    Script.LuaUI.MexEnergyEvent(teamID, allies, energyWasted, energyForOverdrive, totalIncome, metalFromOverdrive, EnergyChange, teamIncome)
  end
end


function gadget:Initialize()
        gadgetHandler:AddSyncAction('MexEnergyEvent',WrapToLuaUI)
end
Widget:

Code: Select all

function MexEnergyEvent(teamID, allies, energyWasted, energyForOverdrive, totalIncome, metalFromOverdrive, change, teamIncome)
  if (Spring.GetLocalTeamID() == teamID) then 
        WG.energyWasted = energyWasted
        WG.energyForOverdrive = energyForOverdrive
        WG.change = change -- energy change by OD - substract that from income 
        WG.mexIncome = totalIncome-metalFromOverdrive
        WG.metalFromOverdrive = metalFromOverdrive
        WG.teamIncome = teamIncome
        WG.allies = allies
  end
end
*Lua may contain missing localisations and includes
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

okay
what am i doing wrong here
from gadget synced section

Code: Select all

local function NextRound()
	if curround < maxrounds then
		curround = curround + 1
		SendToUnsynced("RoundReset", curround)
		DisplayScore()
		echo( '-- Round ' .. curround .. " --" )
		KillAll()
		KillDead()
		SpawnGear()
		MakeAllWatchersBlind()
		SpawnStarters()
	else
		--Game Over
		gameover = true
		KillLosingAlliances()
		DisplayScore()
	end
end
and widget:

Code: Select all

function RoundReset(curround)	
		if ((curround ~= nil) and (curround > lastround)) then
		lastround = curround
			local x, y, z = Spring.GetTeamStartPosition(Spring.GetMyTeamID())
			local unitArray = Spring.GetTeamUnits(Spring.GetMyTeamID())
			if (unitArray and #unitArray==1) then
			  --Spring.SelectUnitArray(unitArray[1])
			  Spring.SelectUnitArray(unitArray)
			  x, y, z = Spring.GetUnitPosition(unitArray[1])
			end
			if x and y and z then
			  Spring.SetCameraTarget(x, y, z)
			end
		end	
	--end
end
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Gadget and Widget Cross Communication

Post by Google_Frog »

By the look of it you didn't look at the central codeblock of my last post.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

edit:
n/m
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

Lies! This doesn't work.. I'll get echos saying the lauUI script is found, but the local is always nil and never comes out of sync to unsync.

Gadget parts:

Code: Select all

...
local function NextRound()
	if curround < maxrounds then
		curround = curround + 1
		DisplayScore()
		echo( '-- Round ' .. curround .. " --" )
		SendToUnsynced("NewRoundNotice", curround)
		KillAll()
		KillDead()
		SpawnGear()
		MakeAllWatchersBlind()
		SpawnStarters()
	else
		--Game Over
		gameover = true
		KillLosingAlliances()
		DisplayScore()
	end
end
......
----- SYNCED -----
-------------------------------------
else
-------------------------------------
----- UNSYNCED -----


function RoundNotifier(curround)
	Spring.Echo("looking for luaUI w round", curround)
	if (Script.LuaUI('NewRoundNotice')) then
	Spring.Echo("Found luaUI")
	Script.LuaUI.NewRoundNotice(curround)
	end
end
	
function gadget:Initialize()
Spring.Echo("sent to unsync w round", curround)
	gadgetHandler:AddSyncAction('NewRoundNotice',RoundNotifier)
end
Widget:

Code: Select all

function widget:GetInfo()
  return {
    name      = "Commander Zoom Mid Round",
    desc      = "Zooms to Commander after round restarts",
    author    = "quantum and Evil4Zerggin and zwzsg, modded by bobwithbeer",
    date      = "Jul 11, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

function widget:Initialize()
  widgetHandler:RegisterGlobal("NewRoundNotice", NewRoundNotice)
	local lastround = 0
  if (CheckForSpec()) then return false end
end

function CheckForSpec()
  if Spring.GetSpectatingState() or Spring.IsReplay() then
    widgetHandler:RemoveWidget()
    return true
  end
end

function NewRoundNotice(curround)	
	Spring.Echo("Round called", curround)
	if ((curround ~= nil) and (curround > lastround)) then
		local x, y, z = Spring.GetTeamStartPosition(Spring.GetMyTeamID())
		local unitArray = Spring.GetTeamUnits(Spring.GetMyTeamID())
		if (unitArray and #unitArray==1) then
		  --Spring.SelectUnitArray(unitArray[1])
		  Spring.SelectUnitArray(unitArray)
		  x, y, z = Spring.GetUnitPosition(unitArray[1])
		end
		if x and y and z then
		  Spring.SetCameraTarget(x, y, z)
		  lastround = curround
		end
	end	
end

function widget:GameFrame(f)
		--- to double check 
	if f %30 < 1 then
		NewRoundNotice(curround)
	end
end
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Gadget and Widget Cross Communication

Post by Google_Frog »

Idk, get the CA gadget and widget pair and delete things from both that look unimportant until it stops working.
SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: Gadget and Widget Cross Communication

Post by SirMaverick »

in widget: move Initialize() after your NewRoundNotice(). I remember there were problems with ordering of functions, e.g. could be nil if it is defined later.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

Good idea, but it didn't help. Still only getting nil.

okay.. I'm getting this in the log for my echos in the gadget section.

[ 2497] looking for luaUI w round, NewRoundNotice <<--echo in gadget: "Spring.Echo("looking for luaUI w round", curround)"
[ 2497] Found luaUI <<--echo in gadget
[ 2497] Round called, NewRoundNotice <<--- echo in widget " Spring.Echo("Round called", curround)"

does this mean that the gadget is trying to send "NewRoundNotice" as a curround value?

?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Gadget and Widget Cross Communication

Post by jK »

Why the heck didn't you posted the output right next to your widget???
Oh yeah it makes so much fun to search errors w/o getting any hints ...
NEXT time post ALWAYS the (including error) messages when you expect help!

SO your arguments declaration in the widget are wrong. Replace "NewRoundNotice(curround)" with "NewRoundNotice(...)"
and add a "Spring.Echo(...)" to get+see all arguments. Their values may indicate what you are searching for.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

[ 1710] Widget notice called
[ 1710] nil
[ 1729] SCORE: (Alliance 1: 1) (Alliance 0: 0)
[ 1729] -- Round 2 --
[ 1729] looking for luaUI w round, NewRoundNotice
[ 1729] Found luaUI
[ 1729] Widget notice called
[ 1729] NewRoundNotice <--- indicates this is all that is being recieved??
[ 1740] Widget notice called
[ 1740] nil

with widget changed top this:

Code: Select all

function widget:GetInfo()
  return {
    name      = "Commander Zoom Mid Round",
    desc      = "Zooms to Commander after round restarts",
    author    = "quantum and Evil4Zerggin and zwzsg, modded by bobwithbeer",
    date      = "Jul 11, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

function CheckForSpec()
  if Spring.GetSpectatingState() or Spring.IsReplay() then
    widgetHandler:RemoveWidget()
    return true
  end
end

function NewRoundNotice(...)	
	Spring.Echo("Widget notice called")
	Spring.Echo(...)
	if (curround ~= nil) then
		if (curround > lastround) then
			local x, y, z = Spring.GetTeamStartPosition(Spring.GetMyTeamID())
			local unitArray = Spring.GetTeamUnits(Spring.GetMyTeamID())
			if (unitArray and #unitArray==1) then
			  --Spring.SelectUnitArray(unitArray[1])
			  Spring.SelectUnitArray(unitArray)
			  x, y, z = Spring.GetUnitPosition(unitArray[1])
			end
			if x and y and z then
			  Spring.SetCameraTarget(x, y, z)
			  lastround = curround
			end
		end	
	end	
end

function widget:GameFrame(f)
		--- to double check 
	if f %30 < 1 then
		NewRoundNotice(curround)
	end
end

function widget:Initialize()
  widgetHandler:RegisterGlobal("NewRoundNotice", NewRoundNotice)
  local lastround = 0
  if (CheckForSpec()) then return false end
end
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Gadget and Widget Cross Communication

Post by jK »

k the error is earlier in the pipe
bobthedinosaur wrote:

Code: Select all

function RoundNotifier(curround)
	Spring.Echo("looking for luaUI w round", curround)
	if (Script.LuaUI('NewRoundNotice')) then
	Spring.Echo("Found luaUI")
	Script.LuaUI.NewRoundNotice(curround)
	end
end
	
function gadget:Initialize()
Spring.Echo("sent to unsync w round", curround)
	gadgetHandler:AddSyncAction('NewRoundNotice',RoundNotifier)
end
the arguments of RoundNotifier(curround) are wrong, add here the "..." to see all arguments and select the correct ones.


PS: it is even visible in the log
bobthedinosaur wrote:[ 1729] -- Round 2 --
[ 1729] looking for luaUI w round, NewRoundNotice
From the code you wanted to see a number there and not the function name ;).

PPS: k, you even noticed that, just didn't knew how to handle such errors ^^
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

okay. I now know what was wrong. if you notice my code in the unsync portion of the gadget i do not include a _, at the begging of the function and so it was sending the function name instead of the value...

thanks for every ones help and patience!
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Gadget and Widget Cross Communication

Post by bobthedinosaur »

Something that was never really discussed earlier in this old thread. Widget to gadget communication, how would one set that up?

Say I wanted to send the user's playerID along with some other data to a gadget.
Post Reply

Return to “Lua Scripts”