Tilemap.set_tile() results in "Could not get the tile since the supplied tile was out of range" (SOLVED)

Just started with defold. Can’t figure out what I am doing wrong here.

tilemap.set_tile("#grid", “layer1”, 0,0, 1)

I get: ERROR:GAMESYS: Could not get the tile since the supplied tile was out of range.

My tile source is a 120 x 120 image with tile width=60 and tile height=60 i.e.divided into 4 frames, out of which the first 2 frames are coloured red and yellow while the other two are completely transparent.

A look at the documentation:

The position is identified by the tile index starting at
origo with index 1, 1

It looks like you are trying to set tile at (0,0) which might then be out of bounds.

3 Likes

Remember that your tilemap needs to have defined bounds, it can’t be completely empty. Put dummy tiles at least in the corners of the area that you wish to manipulate using set_tile().

4 Likes

Have a 10x10 grid painted on the tilemap. Still the same. Is this error for the tile position or the tile source frame value?

Tried putting values from 1,1 to 2,2 for the tilex,tiley parameter.

Tried putting values from -1,0,1,2,3 for the tile frame value parameter.

No luck yet.

This gives the same error though: tilemap.set_tile("#grid", “layer1”, 1,1, 1)

Read this so I think the cell values have a range from -ve to +ve: https://www.defold.com/ref/tilemap/#tilemap.set_tile:url-layer-x-y-tile--h-flipped---v-flipped-

Solved.

I had painted the -ve (left bottom of origin) tiles in the tilemap, so (-10,-10) was my (0,0)

This worked. Silly really.
tilemap.set_tile("#grid", “layer1”, -10,-10, 1)

Thanks for the replies guys.

3 Likes

tilemap.zip (10.4 KB)

I made you a simple example to study.

4 Likes

Remember that there’s a tilemap.get_bounds() to get the actual bottom left x, y position plus width and height.

3 Likes

I have a question about this - because the current Error statement:

image

Is pretty clear, but it doesn’t show the place when set_tile() or get_tile() was invoked - is it possible to add such info to the error message, preferably with a direct link to the place?

1 Like

We could change it to an actual Lua error in which case you would get file and line number, but that might break things in people’s projects.

But how many places do you call set or get tile? It shouldn’t be that many?

1 Like

In one case I used way too much and had problems debugging, just time consuming :smiley: But yeah, I was just wondering, but if it would break things, it’s unacceptable. It’s better to search for those calls then

Maybe the message could at least show the x/y coordinates which are out of bounds?

It is usually very quick to monkey patch a Lua function and print callstack every time the function is invoked. It is a very quick way to debug when and from where functions are called.

3 Likes

Monkey patch? :monkey_face:
Something like replacing the actual set_tile() with a function with print()?

Yes, like this:

-- save reference to original function
local tilemap_set_tile = tilemap.set_tile

-- replace with our custom function
tilemap.set_tile = function(...)
	-- print stacktrace
	print(debug.traceback())
	-- call original function
	return tilemap_set_tile(...)
end
4 Likes

This makes me realize it is possible to detect where in files things are being print() and pprint() if people have the habit of putting them in places and commenting them instead of deleting them thus making it harder to find…

-- save reference to original function
local local_print = print

-- replace with our custom function
print = function(...)
	-- print stacktrace
	local_print(debug.traceback())
	-- call original function
	local_print(...)
end

print("test")
stack traceback:
	input:7: in function 'print'
	input:12: in main chunk
	[C]: in function 'pcall'
	demo.lua:60: in main chunk
	[C]: in ?
test
4 Likes

Thanks @britzl and @Pkeod, this is really useful! :heart:

2 Likes