"Tell" Tiles at Screen Edge Causing Crash

In my exploration game, there is a point where the player character can become glow in the dark, which allows you to find objects in a dark room. This works in two ways - one by changing the sprite to a different tile when walking on black squares. The other way is to make certain objects appear when the player is near them by telling them to call an event called "glow". I used to following code in the player's Update event handler, which works fine except for one issue which causes a crash.

	if glowinthedark==1 then
		lightupx = event.x
		lightupy = event.y
		lightupy--
		tell lightupx,lightupy to
			call "glow"
		end
		lightdownx = event.x
		lightdowny = event.y
		lightdowny++
		tell lightdownx,lightdowny to
			call "glow"
		end
		lightleftx = event.x
		lightlefty = event.y
		lightleftx--
		tell lightleftx,lightlefty to
			call "glow"
		end
		lightrightx = event.x
		lightrighty = event.y
		lightrightx++
		tell lightrightx,lightrighty to
			call "glow"
		end
	end

This works great except for when the player tries to move to the top or bottom of the screen, which causes the game to crash. I've tested different possibilities and it is definitely this script specifically that's causing the problem.

It doesn't crash if the player goes to the sides of the screen, and if I block off the bottom or top of the screen with wall tiles so the player cannot get within one tile of the top or bottom edge the problem also doesn't occur. I thought it could be something to do with the player trying to "tell" tiles that don't exist, but if that were the case I'd expect it to happen at the horizontal edges as well, and not just the vertical edges.

Does anyone have any idea why this might be happening only when moving to the vertical edges of the screen and not the horizontal edges? And even more helpfully, does anyone know how I could stop this from happening at all?

Many thanks in advance!

1 Like

Sometimes just asking the question helps your mind find an answer!

I added three new lines to the lightupy and lightdowny parts:

		lightupx = event.x
		lightupy = event.y
		lightupy--
				if lightupy<0 then //NEW
			lightupy = 1 //NEW
			end //NEW
		tell lightupx,lightupy to
			call "glow"
		end
		lightdownx = event.x
		lightdowny = event.y
		lightdowny++
				if lightdowny>13 then //NEW
			lightdowny = 13 //NEW
			end //NEW
		tell lightdownx,lightdowny to
			call "glow"
		end

And now, no more crashes :slight_smile:

Glad you've solved it! Just to answer this question:

I believe the horizontal edges wrap around to the row above (to the left) or the row below (to the right). You can think of the room as being one long list of tiles that gets linewrapped. That's why it works going "off" the horizontal edge but not the vertical edge.

2 Likes

Ahh that makes perfect sense!