This question follows on from a previous one I had asked a few days ago. Littering my code with free floating constants has always been an absolute anathema to me. So when I needed to extend a game object with some extra properties I initially tried to do the following
Predefined constants file: statics.lua
(Saved in the folder /scripts
local Statics = {}
Statics.PROPERTY_ONE = "propertyone"
Statics.PROPERTY_TWO = "propertytwo"
....
return Statics
I then use these predefined constants in the rest of my Defold code as follows
**Sample defold .script **
local CONSTS = require "scripts.statics"
go.property(CONSTS.PROPERTY_ONE,0)
go.property("propertytwo",0)
function handleImpact(self,message,sender)
--called from the on_message function when a collision is detected
local url = msg.url(nil,message.other_id,"thisscript")
local propOne = go.get(url,CONSTS.PROPERTY_ONE)
local propTwo = go.get(url,CONSTS.PROPERTY_TWO)
end
Now here is the strange thing - propTwo correctly retrieves propertytwo which was set via a vulgar go.property(“propertytwo”,0) statement. On the other hand propOne which was set via - what I felt smarter - go.property(CONSTS.PROPERTY_ONE,0) which is less liable to typos, easier to change globally etc failed. My question in a word - why?