Go.property not being set? (SOLVED)

#1

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?

0 Likes

To hash or not to hash (SOLVED)
#2

Good question!

Defold likes to know as much about the game as possible, ahead of time, preferably when we build the game. The more we know about your game the more we can optimise and pre-allocate to increase performance. This is why we for instance have a lot of these Max Sprites, Max Tilemaps etc, all defined in game.project.

When it comes to the properties, we also want to know about these, but for a different reason: they are exposed in the editor! If you attach your script to a game object and select the script in the editor you see these values exposed in the Properties panel. This is really useful because it allows a game/level designer to go in and tweak values of individual game objects at design time. They don’t need to know anything about the code itself. And for this to work the properties must have names as strings so that the editor can show them. You can read more about this here: https://defold.com/manuals/script-properties/#defining-a-script-property

2 Likes

#3

Thanks

1 Like