“Script properties are parsed when building the project. Value expressions are not evaluated. This means that something like go.property("hp", 3+6) will not work while go.property("hp", 9) will.”
This means that you can’t use expressions of any kind or variables for that matter since both script and resource properties are parsed at build time, not at runtime.
May be by accessing tilemaps which are in the scope of the game object , and not stored as a resource. But everything will be loaded at startup, then.
Or may be i could generate code for this part, by parsing /data/resources and generate all combinations as a key, and the call of go.property as value (with hardcoded value of the key).
Then i can do the call to go.set with the property got.
But for this last one, i will have to re-generate each time i add something in the resources.
The resource.tile_source()is only used when declaring the variable.
After that, the property is available on self, like so:
function init(self)
print("init", self.tile_source) -- self.tile_source is a hash, a path hash in particular.
go.set(some_url, "tile_source", self.tile_source)
end
[EDIT : correction of go.propery /go.set confusion]
Thank you for your answer.
I had understood the go.property call parameters. The go.set above is to show what i wanted to do.
Sorry it was not clear.
I should have written « i’m stuck because i cannot use expressions in go.property » (would be very complicated for defold team to allow this, i guess). But if every resources must be hardcoded, that add a step of generation in the flow (could be added in a Makefile, before bob call).
I think i will just generate all the go.property with content of /data/resources.
After the generation, It will look like this:
if my_new_tilesource == "foo" then
go.property("new_tilesource", resource.tile_source("/data/resources/foo.tilesource") )
else if my_new_tilesource == "bar" then
go.property("new_tilesource", resource.tile_source("/data/resources/bar.tilesource") )
else if …
end
and then after all of this, i just use the variable self.tile_source with the go.set.
Only possibility i see.
I’ve tried also:
local switch_prop = {["foo"]= go.property("new_tilesource", resource.tile_source("/data/resources/foo.tilesource"))}
switch_prop["test"]()
But i get severe errors at generation:
ERROR:GAMEOBJECT: Error running script: scripts/game.script:106: unexpected symbol near '}'
WARNING:RESOURCE: Unable to create resource: /scripts/game.scriptc: FORMAT_ERROR
WARNING:RESOURCE: Unable to create resource: /_generated_33d003ce.goc: FORMAT_ERROR
ERROR:GAMEOBJECT: Could not instantiate game object from prototype /_generated_33d003ce.goc.
(the first error is while evaluating the table generation)
So probably not possible. Only a bunch of if - elseif will work, i guess
go.property needs to use as 1st parameter a new variable each time.
Look the sample below.
The first go.property owns forever new_tilesource. self.new_tilesource references forever /main/res1.tilesource
The second one won’t change anything. (and the two same values are inserted in the table).
local flipflop={}
go.property("new_tilesource", resource.tile_source("/main/res1.tilesource") )
table.insert(flipflop,self.new_tilesource)
go.property("new_tilesource", resource.tile_source("/main/res2.tilesource") )
table.insert(flipflop,self.new_tilesource)
No, go.property() declarations actually do not exist at all at runtime. When your script runs there is no go.property() function calls anywhere!
The go.property() declaration only exists while compiling your Lua code. We have a build step which finds every go.property() and we parse the property name and value+type. These properties are saved separately but together with the compiled script file in your game archive.
At runtime your Lua code is loaded and the properties are set on the instance (ie on self).
This is what you should do:
go.property("res1_tilesource", resource.tile_source("/main/res1.tilesource") )
go.property("res2_tilesource", resource.tile_source("/main/res2.tilesource") )
function init(self)
local flipflop={}
table.insert(flipflop, self.res1_tilesource)
table.insert(flipflop, self.res2_tilesource)
end