Chili:Tutorial Dynamic Label Content
Dynamic label updating
Preparation
Having windows with labels on screen is a fun thing. Although you probably want to change the contents of those labels if some event happens ingame. For this tutorial we are going to change a label everytime a unit gets destroyed.
as before we are going to establish a basic structure for our code. However we are going to need 4 scripts now.
- LuaRules\Gadgets\Includes\messages.lua - LuaRules\Gadgets\Includes\utilities.lua - LuaRules\Gadgets\units_destroyed_counter.lua - LuaUI\widgets\gui_chili_unitdestroyedwindow.lua
In the messages.lua we are going to put global constants that will prefix interface messages between the engine rules code and the widget ui code. Utilities.lua will contain handy utitliy functions like tokenizing strings or array debug code. Units_destroyed_counter.lua will be used as engine rule code to keep track of the amount of units destroyed. Gui_chili_unitdestroyedwindow.lua will contain the code to display the amount of units destroyed.
Units_destroyed_counter.lua
We will start this page the same way as we did with hello world.
-- INCLUDES
VFS.Include("LuaRules/Gadgets/Includes/utilities.lua")
VFS.Include("LuaRules/Gadgets/Includes/messagetypes.lua")
-- CONSTANTS
local DEBUG = true
-- MEMBERS
local unitDestroyedCounter = 0
-- SYNCED CONTROLS
local Echo = Spring.Echo
-- SCRIPT FUNCTIONS
-- GADGET CODE
function gadget:GetInfo()
return {
name = "units destroyed counter",
desc = "Keeps track of the destroyed units in game (including unfinished units, that timeout)",
author = "Sunspot",
date = "2011-06-19",
license = "GNU GPL v2",
layer = -50,
enabled = true
}
end
As you see 2 of the scripts mentioned earlier will be included in this script in the Includes section. I'll elaborate on them later but for now just put them there. In the Constants section I created a DEBUG constant, this constant will be your friend in later development. Especially if you have to give support to users (ofcourse it's the users fault !) if the code doesn't work anymore. In the Members section we have unitDestroyedCounter this counter will keep track of the destroyed units thru the entire lifetime of the script. Synced control section speaks for itselfs we cache the Spring.Echo function because we probably gonna use it a lot and we need to think about performance.
Next up we will have to init the gadget and create the method to increase the counter everytime a unit gets destroyed. Not really all that difficult but then raises the question how are we going to notify the widget this counter increased ... cause thats an entire other script. Well the sollution is to let the engine send a message. Here is how.
-- INCLUDES
VFS.Include("LuaRules/Gadgets/Includes/utilities.lua")
VFS.Include("LuaRules/Gadgets/Includes/messagetypes.lua")
-- CONSTANTS
local DEBUG = true
-- MEMBERS
local unitDestroyedCounter = 0
-- SYNCED CONTROLS
local Echo = Spring.Echo
local MessageDispatcher = Spring.SendLuaUIMsg
-- SCRIPT FUNCTIONS
-- GADGET CODE
function gadget:GetInfo()
return {
name = "units destroyed counter",
desc = "Keeps track of the destroyed units in game (including unfinished units, that timeout)",
author = "Sunspot",
date = "2011-06-19",
license = "GNU GPL v2",
layer = -50,
enabled = true
}
end
if (gadgetHandler:IsSyncedCode()) then
function gadget:GameStart()
unitDestroyedCounter = 0
end
function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID)
unitDestroyedCounter = unitDestroyedCounter + 1
if DEBUG then Echo("Sending message type " .. UNITDESTROYEDUPDATE .. " params ", unitDestroyedCounter) end
MessageDispatcher(UNITDESTROYEDUPDATE .. "-" unitDestroyedCounter)
end
end