• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

How is this code not working? (And crashing my game)

C

CongoBongo

Guest
Hi, so i'm fairly new with programming with physics in Game Maker, I used to do simple games with the implemented features and buttons (like 8 years ago) and decided to re-download Game Maker after I got an idea for a game this morning, I didn't check tutorials or anything just used the Game Maker Manual which had all the variable names and stuff I'd use and just put my noob-ish Python knowledge in action to see if I could make a simple side-scrolling physics game.

Basically, you're a submarine underwater in the form of an endless side-scroller with 'gravity', if you go into the depths you lose or if you run into enemies too much (haven't even gone past the players physics), you have a limited amount of boost which you use with Spacebar and slowly replenishes over time, you'd use the Up-Down keys to tilt the sub and then boost to go in that direction, ideally you're like cruising so you have to be fluent and not use up all boost. You can recharge fuel from 'fuel-trails' from other ships or grab fuel bars and avoid mines, etc... with a score timer and a boost 'bar', in theory I know how to code this in terms of logical sense but in terms of semantics and how GM interprets the code I'm lost...

Under the Create Event I put two different 'Execute code' buttons, the first one would define some variables and the second one would apply these whenever you press space to boost.

1st code:

boost = 0;
press_space = keyboard_check_pressed(vk_space);

2nd code:

if press_space == 1
{
boost ++;
}
while boost < 100
{
physics_apply_force(ship.x,ship.y,10,0);
}

I tried to put the While inside the If, changed indentations, fooled around with operators, changed the 1's to trues' and trues' to 1's. I also swapped their Events around, using Step or Spacebar (which just crashed it), and also changed the ship.x and ship.y to numerical values or 'ship' alone /// ship is the player name

After this I tried simplifying my code into:

if keyboard_press_check(vk_space)
{
physics_apply_force(x,y,10,0)
}

Still, even without the whole boost thing it didn't work :L I incremented the boost to a much higher value and nothing happened.

The results were either that the game crashed and couldn't even start-up, forcing me to shut it down or it crashed when I pressed Space or just didn't work at all. I tried running it in de-bug mode but didn't retrieve any good info, to be honest I could troubleshoot after changing single variables which I mentioned above but it would take too much time and I'd rather get an insightful answer from the community :)

Here are some screenshots of the player phys properties and world phys. if you need 'em:

Sorry for the long thread, I don't post much on forums but I'd really appreciate a hand or at least an explanation of why it doesn't work :) Thanks.

EDIT:
Before using the above mentioned physics_apply_force I was using physics_apply_impulse. However, the results didn't differ.
 
Last edited by a moderator:

FrostyCat

Redemption Seeker
A reminder of basic GM theory:
  • Create events happen only once, Step events repeat once every frame.
  • keyboard_check_pressed() is true only on the step that the key was hit, keyboard_check() stays true while the key is down.
  • Chronic actions should be done with Step events, not instantaneous loops.
All of these you should have learned from 8 years ago and is still true with GMS 1.x today, but your code clearly demonstrates that you've forgotten it all.

This is what it should be like:

Create:
Code:
boost = 0;
Step:
Code:
if (keyboard_check(vk_space)) {
  boost++;
}
if (boost < 100) {
  physics_apply_force(phy_position_x, phy_position_y, 10, 0);
}
Quite frankly, I think your first priority at the moment isn't learning the physics system, it's getting your basics back.
 
C

CongoBongo

Guest
Hey, thanks for your reply... You're right, I went too far ahead of myself so I'm going over the basics again, anyway I've fixed it up and it's working. I also decided to make it an exploration game instead, with views and darkness and creepy abysmal depths. So... Problem solved !
 
Top