Showing posts with label game maker. Show all posts
Showing posts with label game maker. Show all posts

Sunday, June 26, 2016

Game Maker Tip: Collision and Instances

Wondering why your collision code isn't working, despite the fact that your code is written exactly like the documentation suggests?  I was, too.  Five hours of my life down the drain, bashing my head against my keyboard in the hopes I'd hit the "fix my code plz" button we all secretly fantasize exists somewhere.  Bad news, I did not find this fabled button.  Good news, reading the docs until my eyes blurred did reveal this one snippet of information tucked away in the place_meeting function page:

#%&@#%

There it is.  "... this function can check... for collision... using the collision mask of the instance that runs the code for the check [because duh, why would the function itself let you decide?]"  Emphasis and commentary mine, of course.  I mean, how else would collision functions work-- by giving you a clear idea of what objects you're checking?  Or maybe the opportunity to set both objects?  Hah!  Foolish peasant, bow to the glory of the rabbit hole of obscured instancing!

Ahem.  Sorry about that.  Needed to get it all out.  To be a little more serious-- and hopefully helpful-- here's the problem I was having.  Since I love functions and want to offer both mouse and gamepad support for the project I'm working on, I decided to write a script to handle shooting.  The key press detection code is in the Player Object, and thus the Shoot script technically gets called by said Player Object.  So far so good, right?

However, I want to do some fancy shooting.  I made a sprite to act as a "cone of vision" that could be scaled depending on how much of a range the currently equipped weapon has.  So instead of shooting out a projectile, I want the Shoot script to detect any enemies that are under this cone and act on them accordingly.  I used the built-in place_meeting function to do this, as I remembered this function working pretty well for this purpose before.  Long story short, this did not work in my setup.  After much struggling, I finally realized that this place_meeting call was using the Player Object when it checked for collision with my enemies.  After some additional struggling, I figured out that this is the intended function of GameMaker and I had just glossed over the part of the documentation that stated this fact.

To be fair, it makes sense.  The place_meeting function only allows you to specify one of the objects you're checking for, so logically the code has to be getting the other object from somewhere.  Unfortunately, I would vastly prefer it not just assume the other objects is the one calling the function, and that the person writing the code is aware of this.  Clear code is happy code, after all.

Ultimately, I just added a with clause to the chunk of code checking collision and the issue cleared right up.  It also let me delete about 50 lines of code I had scrambled together trying to fix what technically wasn't broken, so now my code looks pretty again.  Hooray!

Thursday, December 6, 2012

Game Maker Tip: the Collision Event

Hello, dear readers!  I have been busy with life and school, and haven't had much time to think about, let alone update, my blog.  This ends today!  While I take a quick break from working on my final project for my Rapid Prototyping course, I shall take a moment to cover something I've learned about the collision event in Game Maker.

So I'm making a tower defense game, which means I need towers, right?  But wait, every time the enemy collides with a tower, they both die right away instead of dueling it out!  I have the enemy programmed to stop walking and hit the tower every x steps, but it's not working.  What's going on?!

The short answer is this: the collision event works differently depending on whether the object you're colliding with is solid or non-solid.  If you mark that object as solid, Game Maker will push the moving object back to its pre-collision position after they touch, as if the object actually bounced back slightly like it would in the real world.  If it's not a solid object, it'll leave the two "on top of" each other, meaning the collision doesn't really stop until something moves away.

What does this mean for my tower defense example?  Well, if I mark my towers as solid, the enemies will "bounce" back slightly when the two collide and will be able to stand there hacking away at my defenses.  If I  leave them as non-solid, the two will pretty much get stuck together and constantly recall the collision event until they both die.  Fun stuff, eh?

I hope this helps someone out there, as it gave me a lot of trouble while I tried to get my game up and running.  If you have any questions, concerning the collision events or just Game Maker coding in general, feel free to leave a comment.  I'm still pretty new to the program, but I might at least be able to point you in the right direction.  Happy developing!