Tag: Game Maker

?! >:/

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.

GameMaker 8 bug in TestVariable action?

I ran into a bit of strangeness with my game project today. While testing a feature, I noticed something inconsistent in the way the program behaved. Upon investigation I discovered that the cause of the inconsistent behavior was that I used two different functions to do a comparison of a certain value in two different calculations, and these functions are not equivalent.

In GameMaker 8, there’s an Action called Test Expression, as well as an Action called Test Variable. It turns out that Test Variable isn’t so good when it comes to testing for equality, due to the way GameMaker handles typing of variables. For the most part, there are only two types of variables: strings and real (floating point) numbers. But in GameMaker 8 they also introduced a few new functions that return integer values. It turns out, if you use Test Variable to compare the value of a real number to the value of an integer to see if the two values are equal, Test Variable will never return true. However, if you use the Test Expression action to do the comparison, it will return true.

In most programming languages, it is impossible to directly compare an integer to a floating point number; you have to use type casting to convert one of the values and then do the comparison. Working with floating point math is tricky with computers, because of rounding errrors, where values are often +/-0.00…01 (whatever the precision is) off due to the way floating point math works. Yet, GameMaker does not display the full precision of the floating point number that is stored in a variable — it only shows the first two decimal places, and hides the rest. Thus, to GameMaker’s Test Variable action, the number 3, which might be returned by the irandom(n) function, is not equal to the real value 3.00.

Test Expression does get this right, though, and automatically accounts for the rounding error, and will consider int 3 to be equal to real 3.00(00…01).

Hopefully they’ll fix Test Variable in a future release. I have reported the issue to GameMaker’s bug tracking system.

Developing in GameMaker

One of the first things I did with my gmk project is figure out how to do file I/O so I can write out to an event log so I can see what’s going on in the game. This is highly useful, since it allows me to track every action that happens, every value of every variable if I want to.

Once I figured out how to write a file I/O routine in gamemaker language, I put logging on a bunch of things in the game that I wanted to track so I could see it working and make sure that it was working the way I wanted it to. Mostly this was a check to verify that the way I understood the environment to work was correct.

After I’d written a logging routine on just about everything I’d built so far, I discovered that you can call scripts. So I got excited by the prospect of being able to write a reusable script that would save me from having to write out the same code again and again, like I’d just did. So I built my script, proved it worked, then went and ripped out all that redundant work that I’d just put in. I didn’t mind at all because it meant moving forward that I’ll have a lot easier time with building up code, and I’d discovered it way early in the project.

(I probably could have learned it even earlier had I read the manual cover to cover before starting, but I find it easier to engage with something if I play with it as I learn and read as I play with it whenever I get stuck.)

###

The next interesting thing I did was a feature I wanted to implement early. Trying to do the early stuff the right way is important, but you can’t second-guess yourself so much that you don’t make progress at all because you can’t decide what approach is truly best.

The feature in question has to do with the graphics. I wanted a way to graphically show the relative strength of the enemies to the player. Basically, we have a “strength” variable, and if you compare it to the player’s current strength, an enemy is either weaker, equal, or stronger than the player. I want to make this apparent to the player visually.

On my first development iteration, I simply implemented a separate object for weaker, equal, and stronger enemies, which all inherited from a base enemy class.

On my second development iteration, I figured out that I could have a single enemy class, and for the initial value of the strength variable, I can just assign a random number, and then based on this random number, compare it to the strength value of the player, and assign the appropriate graphic to the enemy. I got this to work pretty easily, and it eliminated the need for having separate objects to represent the weaker, equal, and stronger variant enemies. This might be a mistake, as it might make things more complicated to have a single object doing all three things. But I think it is the right way for now.

On my third development iteration, I needed to make the enemies update their graphics. The player’s strength increases during play, so as the player’s strength changes, the enemy’s relative strength might change from stronger to equal, and finally to weaker. I wanted to figure out a way to do this efficiently, and figured that having an event that fires whenever the player’s strength changes would be a good solution, and the “right way” to do it. I haven’t been able to figure out how to do this in GameMaker yet, however. It’s not an easy thing to do, and probably requires a bit more understanding of GML than I have at present. GameMaker handles instances of objects in a kindof kludgy way, and this quirk in the way the language works makes it harder for me to do what I want to do.

After trying to figure it out for about 4-5 hours, and not getting anywhere, I opted to go for something cruder but simpler, and found that it works just fine, at least for now. Rather than trying to fire an event that checks the relative strength of every existing enemy in the current room whenever the player’s strength changes, I simply created an event for the enemy class which checks its strength relative to the player each and every step of the game engine, and updates the enemy’s graphic. Obviously, this would not scale as well (I’m basically “polling” the objects 30 times/second, instead of simply calling a method as needed, which would be a much more efficient way of doing it.) So this approach may not work out so well if the game gets larger. But at the scale I’m currently working at, it performs just fine. I’ll have to revisit at some point if I have too many objects in play, but I’m not going to worry about that for now.

Game project

So I’m working on my first serious attempt at a GameMaker project.

The thing with working personal projects is balancing how much you talk about your project with how much you work on your project. You can’t be all talk and no action, obviously, and you probably shouldn’t simply spring a new project on an unsuspecting world, either.

I’m not yet sure how to do this right. I don’t want to spend a bunch of time describing something that doesn’t exist yet, especially if I may change my mind about particulars a bunch of times before I decide to release it. On the other hand, not talking about it just drives me crazy. I’m into my project, and it’s interesting and exciting to me, and I just have to talk about it.

The other thing that’s hard about talking about working on personal projects is talking about it with a proper level of authority and confidence. I’m doing things that are new to me, for the first time in many cases. But most of what I’m doing is technically not all that difficult or sophisticated. As much as I want to be excited about figuring out something, I don’t want to make myself look dumb by blogging things that are the equivalent of what “Hey, guess what I just figured out? When you have a plus sign between two quantities, that means they’re added together!” would be to a math blog.

What I do like about this project so far:

  1. The fact that I’m doing something that I’ve wanted to do my whole life.
  2. The fact that I can do it.
  3. The fact that I am doing it.
  4. That the progress thus far has been fairly steady.
  5. My process: planning, building experimentally, testing, documenting. I feel like I’m going about this in just the right way.

In fact, I really like my process. Here’s what I do:

  1. Sit down and think about what I want to do. Brainstorming. Create a list, prioritize it. This becomes my backlog.
  2. Work an item on the list until it’s done to where I’m satisfied with it for now. This involves building and running it to test it, over and over. I try to conceive of my solution first, then build it, starting small and simple, and building off of what I just did. I’m more tentative with things I haven’t mastered yet, but that’s the natural way of things.
  3. Write up what I did in my version history. Update the backlog, to either remove the item from the list, or more likely, update it to reflect the next level of refinement that I want to get to with that item.

So far, so good. I have a ways to go yet before I have something that’s worthy of an alpha release. I might have it out in a few weeks, we’ll see. What I have right now is playable, but not interesting as I’ve yet to implement any enemy behavior. I expect that the enemy behavior will be very interesting to work on, and probably one of the trickier aspects of the game and thus may take me longer than what I’ve done so far.