Getting a random location in your world

Hey.

I’m a little lost. I’m trying to get a random location within the bounds of my current level.

I can ask the tilemap for it’s bounds, but then I need to know the tilesize, which seems weird. What’s the best practice here? Send a message to a level manager script asking for a random location, and have all that information in that? Is there any way to quickly get the size of the world?

What constitutes the world? How would that be decided? The bounds of all game objects?

You mention tile maps and in that case you have the get_bounds function, but you need to hardcode the tile size

I was hoping there be a tilemap.get_bounds equivlant that returned the pixel bounds, rather than the tile bounds.

Thanks though.

If you want to use tiles, they should all be the same size. Otherwise, the tilemaps would be rather screwed up. Once you decide on a tile size that would be appropriate, like 64x64 pixel tiles in Minecraft, you can make a simple function like this:

local tilemap = {}

function get_random_pos()
     tilemap.x, tilemap.y = tilemap.get_bounds() -- (your tilemap)
     local new_pos = {}; new_pos.x = math.random(0,tilemap.x); new_pos.x = math.random(0,tilemap.y)
     return new_pos
end
1 Like