• 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!

GameMaker Is there any way I can clean this code up?

Z

zakthdrgnslyer

Guest
My collisions script is really messy, is there way I can make this cleaner? (don't mind the object[]'s, it's just nicknames for my objects)

GML:
    //////////////  
    //COLLISIONS//
    //////////////
   
    //horizontal
   
   
if (place_meeting(x+hsp,y,object[1]))
{
    while(!place_meeting(x+sign(hsp),y,object[1]))
    x+= sign(hsp);
    hsp=0;
    obj_player.state="hurt"
    obj_enemy.state="attack"
}
    //vertical
if (place_meeting(x,y+vsp,object[1]))
{
    while(!place_meeting(x,y+sign(vsp),object[1]))
    y+= sign(vsp);
    vsp=0;
    obj_player.state = "hurt"
    obj_enemy.state = "attack"
}
//horizontal
if (place_meeting(x+hsp,y,object[2]))
{
    while(!place_meeting(x+sign(hsp),y,object[2]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[2]))
{
    while(!place_meeting(x,y+sign(vsp),object[2]))
    y+= sign(vsp);
    vsp=0;
}
//horizontal
if (place_meeting(x+hsp,y,object[4]))
{
    while(!place_meeting(x+sign(hsp),y,object[4]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[4]))
{
    while(!place_meeting(x,y+sign(vsp),object[4]))
    y+= sign(vsp);
    vsp=0;
   
}

//horizontal
if (place_meeting(x+hsp,y,object[8]))
{
    while(!place_meeting(x+sign(hsp),y,object[8]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[8]))
{
    while(!place_meeting(x,y+sign(vsp),object[8]))
    y+= sign(vsp);
    vsp=0;
   
}
//horizontal
if (place_meeting(x+hsp,y,object[9]))
{
    while(!place_meeting(x+sign(hsp),y,object[9]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[9]))
{
    while(!place_meeting(x,y+sign(vsp),object[9]))
    y+= sign(vsp);
    vsp=0;
}
//horizontal

if (place_meeting(x+hsp,y,object[12]))
{
    while(!place_meeting(x+sign(hsp),y,object[12]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[12]))
{
    while(!place_meeting(x,y+sign(vsp),object[12]))
    y+= sign(vsp);
    vsp=0;
   
}

//horizontal
if (place_meeting(x+hsp,y,object[13]))
{
    while(!place_meeting(x+sign(hsp),y,object[13]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[13]))
{
    while(!place_meeting(x,y+sign(vsp),object[13]))
    y+= sign(vsp);
    vsp=0;
   
}
/*
//horizontal
if (place_meeting(x+hsp,y,object[15]))
{
    while(!place_meeting(x+sign(hsp),y,object[15]))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,object[15]))
{
    while(!place_meeting(x,y+sign(vsp),object[15]))
    y+= sign(vsp);
    vsp=0;
   
}
*/

//horizontal
if (place_meeting(x+hsp,y,obj_stump))
{
    while(!place_meeting(x+sign(hsp),y,obj_stump))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,obj_stump))
{
    while(!place_meeting(x,y+sign(vsp),obj_stump))
    y+= sign(vsp);
    vsp=0;
   
}
//horizontal
if (place_meeting(x+hsp,y,obj_fence_h))
{
    while(!place_meeting(x+sign(hsp),y,obj_fence_h))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,obj_fence_h))
{
    while(!place_meeting(x,y+sign(vsp),obj_fence_h))
    y+= sign(vsp);
    vsp=0;
   
}

//horizontal
if (place_meeting(x+hsp,y,obj_fence_v))
{
    while(!place_meeting(x+sign(hsp),y,obj_fence_v))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,obj_fence_v))
{
    while(!place_meeting(x,y+sign(vsp),obj_fence_v))
    y+= sign(vsp);
    vsp=0;
   
}

if obj_player.state != "driving"{
if (place_meeting(x+hsp,y,obj_car))
{
    while(!place_meeting(x+sign(hsp),y,obj_car))
    x+= sign(hsp);
    hsp=0;
}
    //vertical
if (place_meeting(x,y +vsp,obj_car))
{
    while(!place_meeting(x,y+sign(vsp),obj_car))
    y+= sign(vsp);
    vsp=0;
   
}
}
 

rytan451

Member
You'll probably want to loop through the objects 2, 4, 8, 9, 12, and 13.

So:

GML:
var collision_objects = [2, 4, 8, 9, 12, 13];
var l = array_length_1d(collision_objects);
for (var i = 0; i < l; i++) {
   if (place_meeting(x + hsp, y, object[i])) {
      while (!place_meeting(x + sign(hsp), y, object[i])) {
         x += sign(hsp);
      }
      hsp = 0;
   }
   //vertical
   if (place_meeting(x, y + vsp, object[i])) {
      while (!place_meeting(x, y + sign(vsp), object[i])) {
            y += sign(vsp);
         }
         vsp = 0;
       }
}
Don't use my code verbatim. Think about what it does, and consider how it works, and how you can apply those same concepts to your game.

This code will do exactly the same thing as your current collision code for those objects (though that isn't exactly a good thing: it will teleport your calling object if there's no open space for it).

Better yet, make a single parent object for objects 1, 2, 4, 8, 9, 12, and 13, and add a flag that makes it attack (can_attack, perhaps) to trigger the "make all obj_player objects go to state "hurt" and all obj_enemy objects go to state "attack" – wait, is that your intent? – and did you notice that if calling object collides with object[1] while obj_player.state == "driving", the player ends up colliding badly with the car?

Consider making states enums: they're faster and take less memory space.

So:

Code:
enum States {
   HURT,
   ATTACK,
   DRIVING
}
Use obj_player.state = States.HURT rather than obj_player.state = "hurt". It's easier, faster, it takes less memory, and it also automatically checks if you've made a typo.

Don't worry about space. Add additional whitespace before and after binary operators (so add spacing around "a + b" or "a = b", but not "-a" or "!a"), as well as space after commas.

Feel free to add additional newlines as needed; never feel pressured to fit as much as possible into as little space as possible. Feel free to make repeated code into function calls "scripts".

Finally, remember this: "It takes twice as much cleverness to debug code as it takes to write it. So, no matter how clever you are, never write code as clever as you can make it. You will be unable to debug it. Rather, write code like it is meant to be read and understood by an idiot."
 
Z

zakthdrgnslyer

Guest
You'll probably want to loop through the objects 2, 4, 8, 9, 12, and 13.

So:

GML:
var collision_objects = [2, 4, 8, 9, 12, 13];
var l = array_length_1d(collision_objects);
for (var i = 0; i < l; i++) {
   if (place_meeting(x + hsp, y, object[i])) {
      while (!place_meeting(x + sign(hsp), y, object[i])) {
         x += sign(hsp);
      }
      hsp = 0;
   }
   //vertical
   if (place_meeting(x, y + vsp, object[i])) {
      while (!place_meeting(x, y + sign(vsp), object[i])) {
            y += sign(vsp);
         }
         vsp = 0;
       }
}
Don't use my code verbatim. Think about what it does, and consider how it works, and how you can apply those same concepts to your game.

This code will do exactly the same thing as your current collision code for those objects (though that isn't exactly a good thing: it will teleport your calling object if there's no open space for it).

Better yet, make a single parent object for objects 1, 2, 4, 8, 9, 12, and 13, and add a flag that makes it attack (can_attack, perhaps) to trigger the "make all obj_player objects go to state "hurt" and all obj_enemy objects go to state "attack" – wait, is that your intent? – and did you notice that if calling object collides with object[1] while obj_player.state == "driving", the player ends up colliding badly with the car?

Consider making states enums: they're faster and take less memory space.

So:

Code:
enum States {
   HURT,
   ATTACK,
   DRIVING
}
Use obj_player.state = States.HURT rather than obj_player.state = "hurt". It's easier, faster, it takes less memory, and it also automatically checks if you've made a typo.

Don't worry about space. Add additional whitespace before and after binary operators (so add spacing around "a + b" or "a = b", but not "-a" or "!a"), as well as space after commas.

Feel free to add additional newlines as needed; never feel pressured to fit as much as possible into as little space as possible. Feel free to make repeated code into function calls "scripts".

Finally, remember this: "It takes twice as much cleverness to debug code as it takes to write it. So, no matter how clever you are, never write code as clever as you can make it. You will be unable to debug it. Rather, write code like it is meant to be read and understood by an idiot."
Thank u sir
 
Top