Beaster’s Dungeon Revisited: Part II

Continued from Part I...

With Eric gone, I eventually re-coded everything, but never could work out how to implement the trap mechanic, so I shelved it and there it sat…until now…

###

I was talking with a friend who was admiring some CGA-16 color palette pixel art, which lead me to showing him the artwork I’d done for Beaster’s Dungeon. He asked if I had any intention of picking the project back up, to which I replied that I might, albeit with a reduced scope. I explained the difficulties I encountered with the trap mechanic, specifically, getting the cursor to snap to a grid.

This lead to a discussion of where I got stuck, and in order to illustrate this, I fired up GMS and started creating a prototype to illustrate the problem. I reached out to my friend Jason (who did the programming for ‘Milk Smugglers’) for suggestions, and he linked a forum post containing the floor/ceiling functions.

In that post was another mysterious function called, move_snap. Here’s what I did for the prototype:

o_cursor Object

///step event

//set coordinates of the cursor to the mouse's x,y position:
x = mouse_x;
y = mouse_y;

//snap the object to nearest grid coordinate 
move_snap (16,16);

Why is this important? Because it allows for precise placement of objects in a map comprised of 16×16 pixel tiles (or any predefined ‘snap’). Around this, additional rules and checks could be incorporated to ensure traps could only be placed where they were meant to be!

At long last, I was finally able to implement the trap mechanic! The only thing left to complete the prototype was enemy path finding… This turned out to be far more complicated than any of the other mechanics combined.

Stay tuned for Part III!

Beaster’s Dungeon Revisited: Part I

Naively Ambitious

Beaster’s Dungeon was to be a 2D platformer where the player would try to stop oncoming hoards of enemies from reaching your treasure vault by collecting resources to build and place elaborate traps.

It was conceived by my friend Eric and myself in late November of 2016. We sketched out a rough outline and slowly refined it into a proper (read: laughably inadequate) design document by early December.

Progress was slow as Eric and I both had very steep learning curves to overcome. I (arguably) had the easier job of creating the artwork and animation, and had plenty of references to lean on for inspiration. Eric, on the other hand, had the dubious honor of trying to cobble together code from various YouTube tutorials and the results were about what you’d expect.

We were getting nowhere fast, and even what [naively] seemed to me to be the simplest features were a puzzle of Gordian complexity for which I decided to apply the Alexandrian solution: cut it all loose and start anew.

Somehow, I stumbled across John Janetka’s Game Programming Course, and by the third lesson, knew enough about GML to solve many of the programming problems Eric was stuck on. Encouraged by a possible way forward, I suggested to Eric that we complete the coursework together. For Eric, this would serve to build the foundational knowledge and concepts he needed to master in order to get predictable, repeatable results. For me, well partly as a show of solidarity and encouragement, but mostly so that I could help troubleshoot when he got stuck.

Eric [reluctantly] agreed and we set off – what follows are details I haven’t really shared before, but I will try to present them objectively in the hope that someone, somewhere will learn from them.

Sullenly Inevitable

Over the next few weeks, I dove head first into the well of knowledge, occasionally coming back up to share what I’d learned on this blog. Eric was less enthusiastic about this, seeing it as a chore, but went through the motions – at least for a while.

To me, each lesson represented one step closer to accomplishing my goals. To Eric, each assignment was a step backward, taking us further and further away from a completed project.

Toward the end of January, I was eager to test what I’d learned by making a simple game from start to finish – that game was Porker: The Quest for Tastiness, more on that below. Eric on the other hand was thoroughly demoralized, having completely given on up on the GPC, and would soon after withdraw himself from the project altogether. What went wrong?

Rowing in the Same Direction

“Much like a relationship, I don’t think a game development team works if the designers aren’t on an equal ability and similar mindset”
– ‘3kliks’ Philip Dyer

From the beginning, I recognized that if I wanted to develop games, I would need to develop my skill set which, as I often lamented, was lacking. But I worked on it and improved, little by little.

As an individual hobbyist, you can work at your own pace, are accountable to no one but yourself and your project(s) can drag on for months or even years on end. You can pick up where you left off, start over or let it collect dust in the dark recesses of a forgotten folder.

In a team of unpaid amateurs, some are genuinely passionate and are prepared to commit time, money and resources to realize their shared dream. Others are only along for the ride, hoping to be carried across the finish line. Somewhere in the middle are those who start strong, but lose steam quickly and eventually drop out.

This could be due to:

  • Change in personal circumstances
  • Distractions
  • Lack of commitment
  • Loss of interest
  • Discouragement
  • Unrealistic expectations
  • Disagreements and disputes…

In part 7 of his YouTube series called “The Game Making Journey,” Philip Dyer (AKA 3kliksphilip) begins by stating that, “Game design has a reputation for being a boring and lonely activity (probably), but it doesn’t have to be this way! Making them with other people is an exciting bonding experience that almost always ends in disaster. Much like a relationship, I don’t think a game development team works if the designers aren’t on an equal ability and similar mindset.

For Eric and I, there was a disconnect. When I committed to working on the project,  I threw everything I had into it. Between working sessions, I read everything I could get my hands on related to the subject from game design theory to level design concepts to lessons learned from other indie game developers, and tried to incorporate this into my thinking.

Eric wasn’t really interested in what the outside world did or thought, and to my knowledge, never actively sought information or did research. Perhaps he was afraid his ideas would be polluted by outside influences and lose their originality, or perhaps he was afraid that doing so would somehow detract from the magic of it.

A Kind of Magic

To him, creating videos games was a kind of magic – you simply needed to learn the incantation, wave your arms about and the computer would either reward you with your desired outcome or punish you for your insolence by refusing to work, or worse yet, play a trick on you and give you something you didn’t expect.

One dream, one soul, one prize, one goal
One golden glance of what should be
It’s a kind of magic” – Queen, It’s a Kind of Magic

Having the spent the last 20 years of my life (and career) working with computers, I knew this couldn’t be further from the truth.  If something didn’t work right, it was my fault for not understanding the syntax or behavior – garbage in, garbage out! The computer had no will of it’s own, and had no choice but to interpret the commands I gave it in the order they were received, and the order of things mattered.

Larger problems are broken up into smaller problems, and smaller problems can be solved, provided they’re well-defined – no room for ambiguity here!

Never Say Never!

By February 1, 2017, I’d completed the GPC. I wasted no time, and dove into re-coding Beaster’s Dungeon. By this point, Eric stepped down from coding and wanted to try his hand at game design. I created a Game Design Document (GDD) template for him, and set him to work while I tried to build a prototype.

By the end of the first week, basic movement was finished, and I was struggling with the trap placement mechanic. I had no idea how build this, and had to take a step back.

Despite all of the effort I put forward insofar as music, graphics and now code, I still wasn’t a “Game Developer” because I had no yet produced “a Game”. This bothered me a lot.

I was discussing this with my friend, Jim, and while talking about GMS, I pulled it up in Google Hangouts and started showing him things. He was drunk and we were having a great time – he’d shout out silly ideas to me while I coded the game. The artwork was mostly modified from open source images found here and there, but it had that late 90’s flash game charm and worked well enough – it didn’t have to be great game, but I did have to finish it, and after 2 days work, the result was, “Porker: The Quest for Tastiness”, which we released under Jim’s website, porkcircus.com.

###

I never did get back to Beaster’s Dungeon – forever confounded by that damned trap mechanic – until yesterday!

To be continued…

Porker Let’s Play Campaign – Post Mortem

In the wake a half dozen or so key requests on WeaselZone.com which yielded no Let’s Play videos, I decided to do a post-mortem on my advertising campaign to evaluate what went right and what went wrong.

###

It’d been a little over a year since I programmed Porker: The Quest for Tastiness. That little game was never intended to be a serious endeavor, but rather a means to get some experience creating and publishing a game.

All of that changed on on February 12, when I’d noticed a couple of kids had:

  1. Found that obscure little game on GameJolt
  2. Installed it
  3. Played it
  4. Posted it on YouTube

They did this all on their own, without any prompting, incentive or instruction.
I was so inspired and encouraged by their Let’s Play that I decided go ahead and expand the game significantly into a fully-featured game.

It took about 2 months to finish the game, and I was very proud of the result. We started looking at ways to advertise and I’d settled on engaging the YouTube Let’s Players community. After all, that’s where it all began, right? What follows is how I went about it and what I learned from the experience in hopes that this may help another fledgling game developer…

###

The Popularity Paradox

Although I did not coin the term, “Popularity Paradox,” as far as I am aware (as evidenced by the entire 2 minutes of Google Fu I spent looking) I’m the only person who has applied to the term to this context:

…many indie games become popular because they receive a lot of YouTube coverage from Let’s Players, but Let’s Players tend to only review games that are already popular…

Therein lies the rub! While I sent keys to the usual 1M+ subscriber Let’s Players, I doubted any of them would ever see, let alone play my game. My research seemed to indicate that their backlog of Let’s Play games was dictated by their audiences, usually by popular request via Reddit or some other medium.

So instead, I focused on smaller to medium sized channels, who I hoped would be willing to do a fellow small-fry a solid. Here are the numbers…

I started with the [now defunct] YouTuber Gaming Megalist, a spreadsheet of over 5,000 YouTubers and their demographic information. As I went through the list, I was able to prequalify about 100 or so potential YouTubers, spending about 5 minutes each on their channels to answer the following questions:

  • Do they post frequently (at least once a week)?
  • Do they cover small indie games, or just the ones everyone else is playing?
  • Does their ‘about’ page encourage developers to contact them, or state that they play indie/random/rage games?
  • Do they have an email address?

If I could answer, “Yes!” to all of these questions, they received a..

  1. Personalized message, tailored specifically to them (no mass-mailing)
  2. Game key for Porker to use for a Let’s Play video
  3. Let’s Player’s guide (PDF)

Of those original 100 or so emails sent out, 25 clicked the link to view their key, and of those, 14 claimed their key. Of those, only 3 went on to make Let’s Play videos.

So how do those figures stack up? Well according to Mail Chimp’s Email Marketing Benchmarks*, the Games industry average was a 19.71% open rate, and a 3.19% click through rate.

Since I emailed my recipients by hand, one message at a time, I can’t really say how many of the 100 odd that I emailed a key to actually opened the message, so instead I’m going to consider “key views” to be my open rate and “key claims” to be my click through rate.

Using those metrics, my open rate is 21% higher than the industry average, and my click through rate is nearly 4.5x greater than what I should reasonably expect.

I suppose that a 21% conversion rate (i.e. ~ 1 out of every 5 people who claimed a key made a video). That’s not terrible, but that was result of about 80 hours of work on my part…

I don’t have a full-time PR person, and have no way of distinguishing between people who are serious about exchanging services by helping each other grow versus dishonest scammers who just want something for nothing.

Going forward, if I do hand out keys, I will use a service like distribute() to do it.

Changes

“Time may change me, but I can’t trace time.” – David Bowie

About 8 weeks ago, I celebrated my 1 year anniversary in my new role. A week after that, my manager resigned and I was tapped to take his place. Since then, it’s been a whirlwind of changes and new responsibilities.

By all accounts, this is old hat for me, but the demands on my time have increased significantly, becoming greater and greater as I unravel years of mismanagement and willful neglect.

While I’m very happy in my new position, I am busier than ever, and even less inclined to do anything productive when I get home after 10-12+ hours of skull sweat…

Pressure requires a release valve, and lately, my pressures had been relieved by playing games rather than making them. What’s worse is, these games introduced a whole-new set of pressures and demands on my time – so much so that it felt like a second job, albeit one which I wasn’t being paid to do.

###

While playing games can be fun and interesting, that part fades quickly. What keeps me interested is the social interaction; meeting and spending time with new “friends”. What I found was that for the people I was spending a great deal of time with, the opposite was true – they had no interest in camaraderie, just a person to occupy a seat at the table so they could carry on their game.

Maybe it’s the age gap speaking here, and relationships have given way to instant gratification – maybe I found myself surrounded by the “single serving friends” of Chuck Palahniuk’s Fight Club…

So what did I do? I withdrew…

 

Credits: featured image, “Butterflies” by M.C. Escher

The Fourth Level

As mentioned in a previous post, Level 4 of the GPC was not a lesson, but rather a series of challenges meant to reinforce what was taught in Level’s 2 and 3. This time, I elected to create a new game rather than modify an existing one.

I settled on a top down shooter/adventure game and added features and flourishes as I went along. I used my earlier conceived true grid movement code, but this time, I used a switch statement instead of a series of if statements (or at least fewer of them):

switch (keyboard_key)
 {
  case vk_left:
  case ord("A"):
     sprite_index=spr_player_walk_left;
     if moving=0 {
       moving=1
       instance_destroy(obj_target) 
       target=instance_create(x-64,y,obj_target)
       move_towards_point(target.x,target.y,4) 
      }
      break; 
 case vk_right:
 case ord("D"):
    sprite_index=spr_player_walk_right;
    if moving=0 {
      moving=1
      instance_destroy(obj_target) 
      target=instance_create(x+64,y,obj_target)
      move_towards_point(target.x,target.y,4) 
     } 
     break;
 case vk_up:
 case ord("W"):
    sprite_index=spr_player_walk_up;
    if moving=0 {
      moving=1
      instance_destroy(obj_target) 
      target=instance_create(x,y-64,obj_target)
      move_towards_point(target.x,target.y,4) 
    } 
    break;
 case vk_down:
 case ord("S"):
    sprite_index=spr_player_walk_down;
    if moving=0 {
      moving=1
      instance_destroy(obj_target) 
      target=instance_create(x,y+64,obj_target)
      move_towards_point(target.x,target.y,4) 
     } 
     break; 
 }

While trying to figure out line of sight, I came a new function:

collision_line(x,y,obj_player.x,obj_player.y,obj_barrier,1,0)

That was my first time using the collision_line, which I don’t think is covered at all in the GPC, though it might be included in his how-to’s somewhere…I came across it after watching a tutorial video by “GM Wolf” on YouTube:

This would have solved a lot of issues for me…for instance, in a previous project, I had an idea to create a trap comprised of two moving blocks which collided into and bounced off of each other. While everything worked fine, it would occasionally cause the block to get stuck in a wall. This happened because it’s movement caused it to overlap before it detected the collision, thereby getting stuck inside the wall, unable to go anywhere.

I solved this before using the place_meeting function, but was never introduced to the End Step Event, which  would have been the right way to do it. Nevertheless, I can think of other uses for collision_line and am glad I learned of it!

All in all, I spent about 2 days (most of the weekend) working on this little mini-game, and here are some of the features I included:

  • Destructible walls that advance in damage by manipulating the image_index
  • Enemies that can navigate mazes (using John Janeka’s code from Level 12)
  • Line of Sight for enemies with projectiles
  • Health, ammunition and keys global variables that persistent between rooms
  • Lock and key mechanism
  • A switch that reveals the exit when touched
  • Randomized muzzle flare and smoke when firing bullets
  • Randomized impact splatters when an enemy is hit
  • Different sounds for each impact
  • Randomized health power up sprites to add variety using a single object
  • Exits that allow you to advance to the next room
  • Capped health at 100%

What I not did include were:

  • Fail condition/game over when you run out of hitpoints
  • Start screen
  • Game Over screen

All in all, it’s a neat little game though unfinished, and good practice for more serious projects to come!

GPC Level 3 Revisited

Shortcuts…

With my files recovered, I’m free to concentrate on getting back to my second run through the GPC. I’m back to Level 3, and bits and pieces of the GML syntax are coming back to me.

For instance, when calculating variables, the course suggests something like this:

hitpoints = hitpoints + 1

This works of course, but he also suggests that hitpoints = +1 does not work, which is true, however, if you reverse that (e.g. hitpoints +=1), it does the same thing with fewer characters to type. A small, but useful time saver.

###

A Hamburger That Fires Bananas (Improved)

In GPC 3-03-A, he introduces a function that randomizes an integer:

irandom_range(0,100)

This allows you use a random number generator (RNG) to decide the outcome of a particular variable within a specified range (i.e. 0 to 100 in the code above). This is used to manipulate the direction that a hamburger fires bananas, and the speed at which they are fired.

I took this a step further by adding the following step event (pun very much intended) to obj_banana:

image_angle+=speed*10

This (in my most humble opinion) makes the bananas far more interesting/exciting to watch as it causes them to spin through the air at a speed consistent with their velocity – i.e. a fast traveling banana rotates faster than a slower moving banana.

Try it yourself!

Aside: I couldn’t help but giggle at the silliness of this lesson – you wouldn’t think a burger that shoots bananas would be so amusing but you’d be wrong! Very entertaining stuff 🙂

Switching Things Up

In Level 3, “Challenge 2 – Random Generation,” the student is required to write a series of if statements to randomly move the player character in one of 4 orthogonal directions.

In my first time around, I recall touching on Switch statements, which can be a good way to handle this very scenario. Here’s how that would look:

Instead of this:

num=irandom_range(1,4)

if num=1 {
direction=0
speed=4
}

if num=2 {
direction=90
speed=4
}

if num=3 {
direction=180
speed=4
}

if num=4 {
direction=270
speed=4
}

You could do something like this:

speed=4
switch (irandom_range(1,4)) { 
 case 1: direction=90; 
 break; 
 case 2: direction=0; 
 break;
 case 3: direction=270; 
 break;
 case 4: direction=180;
 break;
}

This seems like a far more efficient and cleaner way to handle multiple (more than 3+) consecutive if statements, but is cheating at this stage as Switch isn’t covered in the GPC until much, much later*…

So why mention it now? Because I want to get in the habit of making my code clean and efficient.

And with that, on to Level 4!


*I can’t quite recall exactly when this was introduced… For all I know, it may not have even been in the GPC, but if I do come across it later, I will update this post with where you can find it.

Here I Go Again on My Own

It’s been over 6 months since my last post. I’ve settled into my new role, and as the year winds to a close, I’ve been getting the itch to return to game making. Unfortunately, circumstances are such that I’ll be going it alone as Eric has retired from V-Toad Games.

Game programming and design is difficult, frustrating, greatly rewarding -but it’s not for everyone. What I learned from my last go at it was that programming is not painting by the numbers. Being able to complete an exercise no more makes you a programmer than following a recipe makes you a cordon bleu chef. There are core skills you have to master, and to do that, you have to be passionate enough to stick with it.

Eric really tried – I sincerely believe that. Nevertheless, I know all too well that bills must be paid, and can’t blame him for not having the energy to go home and learn something that he just wasn’t passionate about.

So why is being a solo project a problem for me? Many game developers, past and present, were/are one-man shows. I suppose it’s because I like feedback; I like to share what I’ve learned and bounce ideas off of other people because this helps me to reinforce and retain that knowledge.

As a consequence of my hiatus, I’ve all but lost everything I learned about GML, and will need to go through the lessons again to get back in the saddle. This time around should be much quicker, albeit lonelier.

“…Here I go again on my own…” – David Coverdale, Whitesnake

Get on with it!!!

Conflicting Priorities

I spent most of last week preparing for a face-to-face interview, pouring over the job description and reviewing every detail so as to be prepared for whatever the interviewer might bring up – this meant putting just about everything else on hold and using every waking moment to study.

The interviewers seemed pleased with me, and I was disappointed to learn that the job descriptions were erroneous; most of what were listed as, “required skills” weren’t required at all! What a waste…

Earlier this week, I received a call back from the recruiter explaining that they elected to go with someone else. In short, a week wasted and nothing to show for it but a heaping helping of disappointment…

###

After about a day of laying about and feeling sorry for myself, I decided that what I really needed was to focus on something productive… the game I’d set aside to study up for the interview was sitting there, 90% done, waiting for me to cross the last mile…

“If you know where you’re going, you can get there very fast.” – Grandmaster Henrik Danielsen

The most time consuming part of making any game is getting a clear picture of what you want to do. This is true for every component, whether it’s creating artwork, making sound effects, writing music or programming.

Of these, artwork is probably the most difficult (for me) and time consuming…  Often times, I may not have a clear mental picture of what I want to do, and haven’t developed a good system for working through ambiguity yet – but once I do break through, things move very quickly!

I’ve accomplished more in two days than I have in the last 2 months, and the end is in sight! All that’s left is just putting in the time I need to spend to get through the last few pieces, a few days more to test, then on to distribution!

With luck, I’ll have a successful YouTube marketing campaign and will sell enough copies to support myself until I finish Beaster’s Dungeon.

Losing Steam, Second Wind

I just finished Level 12 out of 17 of the GPC. The momentum I built up going into Level 5 has been slowing to a crawl as I stumbled over structural incongruitiesweak examples, bugs, and project file configuration issues.

The Groove (or Rhythm or Flow…)

I am most productive when I find my ‘groove’. This happens when I’ve overcome my learning curve, have developed a pattern of work,  then get into a rhythm where I can knock out task after task by reusing the same steps.

Jimmy Diresta explains that first time you try something is the most difficult; you ‘go to school’ on the first one, and get progressively better and better – by time you finish, you’re an expert.

Matthew Inman made an incredible post on this subject, describing the agony and joy of creation…a few non sequiturs aside, he eventually gets to the meat of issue – how great and wonderful things can result from sweating over tasks that are often tedious and frustrating in and of themselves.

My problem is that I’m a perfectionist. If I encounter something that isn’t working to my satisfaction, I can’t ignore it – I have to ‘fix’ it. Maybe this is a characteristic of a good programmer, but it’s utterly frustrating when I’m working on it, and enormously satisfying when I get something to work properly…

The trouble, it seems, is that the content is getting thinner and thinner,  and the issues referenced above that are breaking my ‘flow’ are getting more and more numerous. At a guess, I’d say that these last few videos were produced at a busier time than the others, and as a result, he simply didn’t have the time required to carefully plan and produce them as well as he’d like.

Whatever the reason, I have to remind myself that no matter how annoyed I get with this course, it’s still free of charge, and of far superior quality to any other GML ‘tutorials’ on YouTube. More importantly, distractions, bugs and problem solving (especially when the problem is more complex than it first appeared) are all part of programming – so I’d best get used to them 🙂 .

That said, on with the lessons…

###

Levels 9-11 (Doesn’t that look ominous?)

Level 9 was more or less a rehash of Level 7, focusing on instance IDs, along with another ‘cheat sheet’ from the resource folder. The two share a lot of parallels, and might be better off merged into one big lesson rather than two disjointed ones. There was even a loop snuck into one of the project files to create more enemies, but that wasn’t covered as part of the lesson.

Level 10 was one of the shortest lessons so far, and anyone who attempted to include a sound as suggested in Challenge 10-01 part C will discover that they’ll need to enable the New Audio Engine (Global Game Settings > General > Use New Audio Engine) before they’ll hear anything.

Level 11 was a letdown. I was really looking forward to Lists, but the only output method covered was the number of total items in the list (e.g. the ds_list_size function). The examples in the lessons didn’t really fit my concept of ‘inventory’ – that is, being able to output all the values in the list, or how many of each unique value there were (e.g. 2 Burgers, 2 Apples etc.). I suspect that this will be covered later in Level 13 (Loops).

###

This brings us up to…

Level 12: Grid Game Concepts

Grid games can cover everything from board games (checkers, chess etc.) to turn-based and real-time strategy games to classic video games like Namco’s Pac-Man.

In all cases, movement in a grid game is fixed rather than free form. GPC 12-01-A covers movement, and attempts to tackle the issue of getting hung on on corners due to the collision geometry.

The Lesson’s Solution:

Make the sprites equally sized (32×32 pixels in this case) and use a collision mask of 32×32 to ensure that they stay within the confines of the maze corridors. That’s a good start.

Next, he introduces how to manually code keyboard and collision events inside a Step event like so:

if keyboard_check(vk_right)=true and place_meeting(x+4,y,o_block)=false {
 x=x+4
 }
 
if keyboard_check(vk_left)=true and place_meeting(x-4,y,o_block)=false {
 x=x-4 
 }

if keyboard_check(vk_up)=true and place_meeting(x,y-4,o_block)=false {
 y=y-4 
 }

if keyboard_check(vk_down)=true and place_meeting(x,y+4,o_block)=false {
 y=y+4 
 }

What the code is actually doing is checking to see if the player object will collide with a wall object (o_block), and if not (false), advance the player 4 pixels in the direction given.

Why this is an unsatisfactory approach:

Since are moving the player 4 pixels at a time, you have to manually tap back and forth until you are perfectly aligned or you will still get hung up on corners. This isn’t much of a problem when moving around 90 degree turns, becomes an issue when trying to get through ‘four-way’ and ‘T’ intersections.

How to fix it using only what we’ve covered so far up though Level 12:

When I set out to tackle this, the logic went something like so…

  1. Find a location 32 pixels (one character or tile length) ahead of whatever direction I was going
  2. Move to that location
  3. Stop moving when you reach it

I tried using the move_towards_point function using a variable (e.g. target=o_player.x+32) in conjunction with distance_to_point but found that it target would always be 32 pixels further ahead, so it would never close the gap and would move in that direction infinitely. The solution would have to be something relative to, but external from the player object.

That’s when it hit me; why not use another object? So here’s what I did (coded in the o_player object)…

Press D-Key Event:

//create a new object as a waypoint for the player to move towards
//move towards the target's coordinates at a speed of '4'
target=instance_create(x+60, y, o_target)
move_towards_point (target.x,target.y,4)

Collision with o_target Event:

//Destroy the o_target object on collision
with other {
instance_destroy()
}

//Stop the player
speed=0

Some of you may be looking at that code and scratching your heads – why 60 pixels? Well after lots of trial and error, I worked out that in order to move 32 pixels from where you were, you’d have to include the width/height of the player object (+32 pixels), giving you a total of 64.

When I tried 64, I found that I was stopping just short of where I needed to be, and worked out that the collision wasn’t actually detected when the edges of the two object met, but when the player object had overlapped it by the designated speed (4 pixels). So, 64-4=60, which places you at exactly 32 pixels over every time you press the key.

Once I had that working, it was time to clean up the code and put some utility conditions in it to prevent a keystroke from changing direction before you got to your destination, an additional event to stop you when you hit a wall, and to destroy any target objects you can’t reach (i.e. behind a wall) and some additional variables to be able to control the distance (pdist) and speed (pspd).

In the Press D-Key Event:

// check to see if character is moving
if moving=0 {
//set moving to true & destroy the old target
 moving=1
 with o_target {
 instance_destroy()
 }
//move target right by 'pdist' pixels at a speed of 'pspd'
 target=instance_create(x+pdist,y,o_target)
 move_towards_point(target.x,target.y,pspd)
}

Add moving=0 to the end of the o_target and o_block collision events and that’s pretty much it! Now using the Key Press event rather than just the Keyboard event will cause you to take a single step 32 pixels in whatever direction you are heading assuming you adjust the +/- pdist variable depending on whether you are going left (x-pdist), right (x+pdist), up (y-pdist) or down (y+pdist).

So once I got everything working, I swapped the WASD Events over from Key Press back to Keyboard and viola, true grid movement!

 

Image Credit: Don Quixote, oil on canvas painting by Jean-Baptiste-Camille Corot, 1868.

A Challenge Within a Challenge!

I’m up to Level 08, Lesson 03, and was working my way through the challenge that followed.

In playing through the game as-is, there a couple of notable bugs above and beyond what the challenge required:

  1. The collision event for the potion was missing altogether – easy fix
    givePlayerHealth(25)
    with other {
         instance_destroy()
    }

     

  2. The “health bar” didn’t accurately reflect the current hitpoints! Take a good look at the screen capture from his preview video below:

 

Look closely and you can clearly see “HP: 5” yet the health bar is nearly 1/3rd full! That doesn’t look right… so what’s wrong with it?

This one was a bit more tricky because neither draw_rectangle nor var have been covered yet…granted, the challenge was to change the color of the rectangle based on the # of hp, not to create a hitpoint bar from scratch – even so, it bothered me that it was broken and I wanted to see if I could troubleshoot and fix it…here’s the original code:

var healthBarWidth=100
var maxHealth=100
draw_set_color (c_white)
draw_rectangle(150,50,200+hp/maxHealth*healthBarWidth, 60, false)
draw_set_color(c_black)
draw_rectangle(150,50,200+healthBarWidth, 60, true)

The syntax for the draw_rectangle function is as follows:

draw_rectangle(x1, y1, x2, y2, true or false) where

x1=the x location of the upper left-hand corner of your rectangle
y1=the y location of the upper left-hand corner of your rectangle
x2=the x location of the lower right-hand corner of your rectangle
y2=the y location of the lower right-hand corner of your rectangle
true=filled with whatever your draw_set_color is
false=empty (transparent) with a 1 pixel border

Confused? Well here’s an illustration I made to visualize it better:

Now lets apply that to the code above. Working through his arithmetic for the filled (false) rectangle, here’s what you get for the x2 values if you substitute the variables for their numbers:

200+0/100*100=200 (0 hp)
200+100/100*100=300
(100 hp)

Expressed in code, it would look like this:

draw_rectangle(150,50,200,60,false)//0 hp
draw_rectangle(150,50,300,60,false)//100 hp

…and output like this (assuming you include the border rectangle):


The left edge (x1) starts at 150 pixels in, and the right edge ends at 300 pixels, giving you an overall length of 150 pixels (300-150=150) along the x axis.

When the player has 0 hp, the left edge is not 150, but 200, leaving you with a 50 pixel (1/3rd) wide rectangle, which is exactly what you see in the screenshot above.

So I dug into the code and came up with the simplest solution. This was the result:

Notice that it says “HP: 50” and the bar is exactly half way!

So how did I fix it?

Simple – move the left edge over by 50 pixels, giving the overall length of both rectangles to be 100 pixels rather than 150, removing the 50 pixel lead:

//health bar draw code - the first rectangle draws the filled color
//the second rectangle draws the border
draw_set_color (getBarColor())
draw_rectangle(200,50,200+hp,60,false)
draw_set_color(c_black)
draw_rectangle(200,50,300,60,true)

It’s no accident that my code is absent the healthBarWidth and maxHealth variables – since my healthbar is 100 pixels in length, the simplest solution was to shorten the bar. The reason those are included is size the fill rectangle proportionately to the amount of hp you have remaining so that you can name the hp bar longer or shorter depending on your needs.

But you can’t use the code from the original project file as-is…

First, you’d need to set minimum left-edge of x2 to match x1 for your filled rectangle. So instead of:

 draw_rectangle(150,50,200+hp/maxHealth*healthBarWidth, 60, false)

You’d replace 200+hp… with 150+hp…:

draw_rectangle(150,50,150+hp/maxHealth*healthBarWidth, 60, false)

Secondly, you’d need to change maxHealth proportionate to the total amount of your hp (100) with respect to the overall length of your rectangle (150). This would give you 100/150 or 2/3rds (66.6% repeating).

Since hp is evenly divisible by the length of the health bar, this presents a problem as putting in var maxHealth=66 is pretty close, but not a pixel perfect fit inside the border rectangle (it’s about 4 or 5 pixels off).

Nevertheless, if I just had to make a health bar bigger or smaller than 100 pixels, I’d try to use something that divides evenly, hide it using depth and alpha channels, or leave off the border altogether. There may be more elegant ways to solve it, but it’s nearly 2am here and I think I’ve learned what I wanted to from this exercise 😐 .