?! >:/

Something very weird going on in my GameMaker project…

I have two Object classes: Player and Enemy.

Player
{ Create() {size = 2}
}

Enemy
{ Create() {size = random_range(0.5, Player.size*3)}
}

This code was working fine up until today. Technically, I should be checking to see if Player exists before I try to use Player.size, but I set things up so that Player always exists and was created before any Enemy instances, so it never threw an exception. I understood the risks and tested it and it never had a problem.

Today I created a background image for the room where the game takes place, and added it to the room. All of a sudden now, the Enemy.Create() event is throwing an exception at Player.size because it is an unknown variable.

I scratch my head a bit and try to figure out why…

Was there actually a fortuitous timing condition prior to adding the background that caused the Player instance to be created before the Enemy instances when the room was being constructed? I try removing the background, but the game still throws the exception. I try creating a new room, adding the background to it, then the player, then the enemies, in that order, and it still throws the exception.

OK, let’s be more careful, and check to see if Player exists when in Enemy.Create(), then.

Enemy.Create()
{
If instance_count(Player)>0
{
size = random_range(0.5,(Player.size*3))
}
}

Game STILL throws an exception trying to access a non existent Player.size variable.

OK, did my game project somehow get corrupted? I roll back and test, things are working, then re-implement the background, and the bug is back again.

Does a room with a background image somehow conflict with Player.size? I run the built in variable name checker, no conflicts.

Hmm, what if there’s a name conflict anyway? I try renaming the size variable to foo. Still throws the exception. I try deleting the variable and adding a new variable that performs the same function. No better.

I run the game, Ignore all the thrown exceptions, and then once all the Enemy instances are created the game works. Clear level, next level is fine, no errors.

Alright, last thing: I pull size out of the Player object and make it global. Surely if Enemy isn’t capable of peering in to Player’s public variables (GameMaker doesn’t seem to have a distinction between public and private object properties, at any rate — everything is public for sake of simplicity for newbie programmers) but what the hey, let’s try it. Add the global.Player_size variable, attempt to access it in Enemy.Create(), still throws the exception.

To quote Moss from The IT Crowd, “What the ever-flipping flip?”

Player.size most certainly exists, why can Enemy no longer access it?

Update: To troubleshoot further, I created a new gamemaker project and made it as simple as possible. I created two sprites, two objects, a room, and a background. With the objects, I assigned a variable in the create() event, and tied the value of object0’s variable to the value of object1. Then added both to a room, then ran the game. No error. Then added the background to the room. Again, no error. So, it doesn’t look like this is a bug in GameMaker, then. Something must be messed up with my game project, and it’s up to me to sort through and figure out what it is, then.

Updated: 2012-Feb-20 — 12:39 pm

Leave a Reply