if(tile[i].status == "blue") when tile[i] doesn't exist - any way to just return false, or avoid an error?

I’m playing around with a random map generator. I have a load of tiles in a table, each named tile[i]. They’re all either blue or green. For each green tile, I’m trying to check if any of the surrounding tiles are blue. If they are, I turn that green tile yellow (for sand).

The code for this is already quite long (I have to check the 8 tiles surrounding). The problem is, for tiles on the edge (first or last row, first or last column), some of the tiles around them that I’m trying to check don’t exist. 

Before I check the tiles surrounding, I could check whether the grass tile is one of these, and then change the check accordingly. But this would give me about 10x more lines of code (separate checks for row=0, column=0; row=max, column=max; row=0, column=max; row=max, column=0; just row=0; just row=max; just column=0; just column=max). And wouldn’t be very fun. There could be ways around it, like just making sure none of the border tiles are green, but I don’t want to resort to those as this kinda thing might come up in the future.

tl;dr: for all tiles, I’m checking if the tile to the left of it (or right, etc) is water. but for some of those tiles, that tile doesn’t exist so I get an error.

Any ideas or “sophisticated” workarounds? Would be great if the == just returned false instead of an error. thanks!

(now that I think of it, I might have to check for border tiles anyway, as otherwise a tile to the far left would just end up checking a tile to the far right, one row above, when I ask it to check tile[i-1]. still, an answer would be helpful)

if( tile[i] and tile[i].status == “blue”)

Lua will perform the first test and since we are using an “and” connector, if tile[i] doesn’t exist, it will stop testing the rest of the line and execute the else clause if there is one.

Rob

Brilliant, thanks!

(with my world generator, I actually ended up creating 3 different “world types”, randomly selected, depending on how they worked around this problem. one made all border tiles water (island world), one prevented any border tiles that were grass from being turned into sand (cliff world), and one made any border tiles that were grass into sand (something inbetween))

Welcome to the wonderful world of Procedural Generation. I’d suggest doing some light reading at http://pcg.wikidot.com, which is a great resource for 2D map generation. 

I also forked a pathfinding library, and modified it to work with Corona SDK out of the box. It might come in handy:

https://github.com/pancinteractive/Jumper

I was at my son’s Simulation and Game Design Final Project presentation. His team and one other use procedural generation in their games. His was a 3rd person dungeon game. The other team did a top-down game. Their model just generated random levels and they had to use A* pathfinding for the AI to figure out how to move the bad guys. My son’s team took a different approach, they had pre-designed rooms and use procedural generation to pick the rooms and random and make sure there was a path from the beginning to the end which helped them on the AI bits.

Rob

if( tile[i] and tile[i].status == “blue”)

Lua will perform the first test and since we are using an “and” connector, if tile[i] doesn’t exist, it will stop testing the rest of the line and execute the else clause if there is one.

Rob

Brilliant, thanks!

(with my world generator, I actually ended up creating 3 different “world types”, randomly selected, depending on how they worked around this problem. one made all border tiles water (island world), one prevented any border tiles that were grass from being turned into sand (cliff world), and one made any border tiles that were grass into sand (something inbetween))

Welcome to the wonderful world of Procedural Generation. I’d suggest doing some light reading at http://pcg.wikidot.com, which is a great resource for 2D map generation. 

I also forked a pathfinding library, and modified it to work with Corona SDK out of the box. It might come in handy:

https://github.com/pancinteractive/Jumper

I was at my son’s Simulation and Game Design Final Project presentation. His team and one other use procedural generation in their games. His was a 3rd person dungeon game. The other team did a top-down game. Their model just generated random levels and they had to use A* pathfinding for the AI to figure out how to move the bad guys. My son’s team took a different approach, they had pre-designed rooms and use procedural generation to pick the rooms and random and make sure there was a path from the beginning to the end which helped them on the AI bits.

Rob