Changing tile source of tile map doesnt work like i think it should

Hi I’m trying to randomly pick between 5 different tile sources and assign the selected one to a tile map. I have this code bit to change the tile source which works

go.property("tile_selection", resource.tile_source("/main/tiles/tile1.tilesource"))
	go.set("#tilesmap", "tile_source", self.tile_selection)

however when I do this

math.randomseed(os.time())
	local random = math.random(5)
	go.property("tile_selection", resource.tile_source("/main/tiles/tile" .. tostring(random) .. ".tilesource"))
	go.set("#tilesmap", "tile_source", self.tile_selection)

or this

math.randomseed(os.time())
	local random = math.random(5)
	if random == 1 then
		go.property("tile_selection", resource.tile_source("/main/tiles/tile1.tilesource"))
	elseif random == 2 then
		go.property("tile_selection", resource.tile_source("/main/tiles/tile2.tilesource"))
	elseif random == 3 then
		go.property("tile_selection", resource.tile_source("/main/tiles/tile3.tilesource"))
	elseif random == 4 then
		go.property("tile_selection", resource.tile_source("/main/tiles/tile4.tilesource"))
	elseif random == 5 then
		go.property("tile_selection", resource.tile_source("/main/tiles/tile5.tilesource"))
	end
	go.set("#tilesmap", "tile_source", self.tile_selection)

it doesnt work and both of those return this error

ERROR:GAMEOBJECT: Properties can not be of type ‘nil’.
ERROR:SCRIPT: /main/puzzlelogic.script:: the property ‘tile_source’ of ‘#tilesmap’ must be a hash
stack traceback:
[C]:-1: in function set
/main/puzzlelogic.script:
: in function </main/puzzlelogic.script:50>

please explain this

Summary

This text will be hidden

I incorrectly formatted the error at the end of my post, here it is in a block quote cause I have no better idea how to do it

ERROR:GAMEOBJECT: Properties can not be of type ‘nil’.
ERROR:SCRIPT: /main/puzzlelogic.script:65: the property ‘tile_source’ of ‘#tilesmap’ must be a hash
stack traceback:
[C]:-1: in function set
/main/puzzlelogic.script:65: in function </main/puzzlelogic.script:50>

go.property can only be used outside of a callback function.

2 Likes

Correct! @aliensloveit add one go.property() per tilesource at the top of your script file and then get a reference to each of them and put them in a table and pick a random one from that table:

go.property("tiles1", resource.tile_source("/main/tiles/tiles1.tilesource"))
go.property("tiles2", resource.tile_source("/main/tiles/tiles2.tilesource"))
go.property("tiles3", resource.tile_source("/main/tiles/tiles3.tilesource"))

function init(self)
	local tiles = {
		self.tiles1,
		self.tiles2,
		self.tiles3
	}

	local t = tiles[math.random(1, #tiles)]
	go.set("#tilesmap", "tile_source", t)
end
3 Likes

Thank you! This works