Function “resource.tile_source” doesn't work with a string variable as parameter

i have a strange thing, using resource.tile_source

local tilesource_path = "/data/resources/newyork4x4.tilesource"
-- doesn't work:
-- go.property("new_tilesource", resource.tile_source(tilesource_path) ) -- (1)
-- hardcoded string works:
go.property("new_tilesource", resource.tile_source("/data/resources/newyork4x4.tilesource") ) -- (2)
print("value new_tilesource:", self.new_tilesource) -- (1): nil , (2): correct hash 

the two strings are the same, the only difference is one is stored in a variable and the other is hardcoded string.

Mysterii.
Thank you for helping !

It is hinted at here:

“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.

2 Likes

Thank you for the explaination. So i’m stuck as i cannot do this:

  go.set(self.current_tilemap, "tile_source", resource.tile_source(tilesource_path))

(go.set needs a hash)

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
1 Like

[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