GMS 2 Make not solid object go through each other.

V

Vitalii Lukyanov

Guest
Hello, I have a problem, I have characters and obstacles in my game and I need to take one life from the character while colliding with this obstacle but at the same time I need characters to be able to go through obstacles. How can I do it?
 
Last edited by a moderator:

samspade

Member
This description doesn't make a lot of sense. Can you:
  • Describe what is currently happening in better detail and what you would like to happen in better detail?
  • Post your code?
 
V

Vitalii Lukyanov

Guest
This description doesn't make a lot of sense. Can you:
  • Describe what is currently happening in better detail and what you would like to happen in better detail?
  • Post your code?
Well, I have collision event with each obstacle in which I take one life from a character. But, I think because of having collision event these instances can't pass through each other and get stuck in each other when they collide. And I want to know if there is a possibility to have a collision event and instances to be able to pass through each other. I don't think that code can help here in any way, cuz I don't check collisions in step event.
 

samspade

Member
Well, I have collision event with each obstacle in which I take one life from a character. But, I think because of having collision event these instances can't pass through each other and get stuck in each other when they collide. And I want to know if there is a possibility to have a collision event and instances to be able to pass through each other. I don't think that code can help here in any way, cuz I don't check collisions in step event.
Are you saying that you have an object which has the specific Collision Event in it which has code that says something like with (other) life -= 1 ? That code should be posted.

Collision Events don't stop objects from moving. They actually don't do anything at all except run the code that is in them every step/frame there is a collision. So there's no reason one object wouldn't pass through another object with a collision event unless you put code in it that stopped it.
 
V

Vitalii Lukyanov

Guest
Are you saying that you have an object which has the specific Collision Event in it which has code that says something like with (other) life -= 1 ? That code should be posted.

Collision Events don't stop objects from moving. They actually don't do anything at all except run the code that is in them every step/frame there is a collision. So there's no reason one object wouldn't pass through another object with a collision event unless you put code in it that stopped it.
Here's my code for collision event:
if (!invincible) {
invincible = true;
alarm[0] = room_speed * 5;
life--;
image_alpha = 0.5;
}

and for alarm0 event:
invincible = false;
image_alpha = 1;

I don't see anything here that can make these objects get stuck in each other and prevent each one of them from moving. And I will repeat just in case that they are not solid.
 

samspade

Member
Here's my code for collision event:
if (!invincible) {
invincible = true;
alarm[0] = room_speed * 5;
life--;
image_alpha = 0.5;
}

and for alarm0 event:
invincible = false;
image_alpha = 1;

I don't see anything here that can make these objects get stuck in each other and prevent each one of them from moving. And I will repeat just in case that they are not solid.
Yup. Nothing there would cause the problem you describe. But since collision events don't effect movement at all unless you tell them to, the probably must be somewhere else.

What does your movement code look like? Or really, what does any code which changes the character's x/y look like?
 
V

Vitalii Lukyanov

Guest
Yup. Nothing there would cause the problem you describe. But since collision events don't effect movement at all unless you tell them to, the probably must be somewhere else.

What does your movement code look like? Or really, what does any code which changes the character's x/y look like?
{Movement Key} Press:
sprite_index = {sprite for moving};
{h/v}speed = {-/+}spd;

{Movement Key} Up:
sprite_index = {sprite for idle};
{h/v}speed = 0;
 

samspade

Member
{Movement Key} Press:
sprite_index = {sprite for moving};
{h/v}speed = {-/+}spd;

{Movement Key} Up:
sprite_index = {sprite for idle};
{h/v}speed = 0;
I realize your abbreviating for space, but that seems like fairly unusual code. Does it actually say:

Code:
///in movement key event
hspeed += spd;
In other words are you using the built in variable hspeed (e.g. turns green) and adding to it every step? What is the value of spd?

However, that is unlikely to really be the problem here as that would only mean it speeds up. What code do you have that stops an object from moving?

Are you using physics?
 
V

Vitalii Lukyanov

Guest
I realize your abbreviating for space, but that seems like fairly unusual code. Does it actually say:

Code:
///in movement key event
hspeed += spd;
In other words are you using the built in variable hspeed (e.g. turns green) and adding to it every step? What is the value of spd?

However, that is unlikely to really be the problem here as that would only mean it speeds up. What code do you have that stops an object from moving?
Uhm, I want speed of character be constant everytime and spd is 10. {Movement Key} Up event is used for stopping character by making speed 0 and it was in my previous reply. And yes, I'm using built-in variables.

Edit: Oh, yeah and also collision event with walls, that sets speed to 0, but I dont think that's the case.
 

samspade

Member
Uhm, I want speed of character be constant everytime and spd is 10. {Movement Key} Up event is used for stopping character by making speed 0 and it was in my previous reply. And yes, I'm using built-in variables.

Edit: Oh, yeah and also collision event with walls, that sets speed to 0, but I dont think that's the case.
Are the obstacles children of walls?

Or to put it another way, I only know of a few ways to stop an object. Directly through code, through the physics system, or by using solid tags. It's possible the built in variables have an interaction somewhere that I'm not aware of though as I don't use built in variables precisely for that reason.

Some things you could try:
  • Delete the original collision event, does the obstacle still stop the player?
  • Delete the wall collision event setting speed to 0, does the player still stop?
  • Stop using the built in variables, does the player s till stop?
  • But a show debug message the the step event that says show_debug_message(speed) - when does speed drop to 0?
  • Do a control + shift + f search for every use of speed, hspeed, and vspeed and look for any other places you're messing with them.
  • Do the same for any GM functions that mess with speed. For example, I'm not positive, but I'm pretty sure that all of the motion add, and similar functions, change speed.
 
V

Vitalii Lukyanov

Guest
1. No, obstacles are not children of walls.
2. When I delete collision events, a character goes through these obstacles.
3. I don't know how to not use built-in vars in collision events, cuz I use step event only for checking if character passes finish line or if it is in the camera view.
4. Speed drops to 0 when I collide with walls(when collision events are present). Speed stays the same while colliding with obstacles and pressing movement keys at the same time.
5. I only change speed of the character in movement key events and collision event with walls.
6. So I think that for some reason collision events make objects behave themselves as they're solid, but they are not.
 
V

Vitalii Lukyanov

Guest
****. Ok, so collision event does make object solid, so I changed solid variable to false in collision event and it's working now. FUUUUUUUUUUUUUUCK!!!!
 

samspade

Member
1. No, obstacles are not children of walls.
2. When I delete collision events, a character goes through these obstacles.
3. I don't know how to not use built-in vars in collision events, cuz I use step event only for checking if character passes finish line or if it is in the camera view.
4. Speed drops to 0 when I collide with walls(when collision events are present). Speed stays the same while colliding with obstacles and pressing movement keys at the same time.
5. I only change speed of the character in movement key events and collision event with walls.
6. So I think that for some reason collision events make objects behave themselves as they're solid, but they are not.
That's great because I was out of ideas.

As a side note, it's why I've moved away from using most built in variables and many of the specific event types. They often have unintended side effects because they do a lot of things automatically and behind the scenes. I'm not saying you should do that though as they provide a lot of benefit and ease of use as well. It's really just personal preference.

Here's how you would do roughly the same thing without built in variables:

Code:
///create event
hsp = 0;
vsp = 0;
spd = 10;

///how to set speed (replace in your current movement keys)
hsp = spd;
hsp = -spd;
vsp = spd;
vsp = -spd;

///how to stop movement
hsp = 0;
vsp = 0;

///step vent
x += hsp;
y += vsp;
There are other ways of course and lots of YouTube tutorials on it as well. See Friendly Cosmonaut or Shaun Spalding for example.
 
V

Vitalii Lukyanov

Guest
That's great because I was out of ideas.

As a side note, it's why I've moved away from using most built in variables and many of the specific event types. They often have unintended side effects because they do a lot of things automatically and behind the scenes. I'm not saying you should do that though as they provide a lot of benefit and ease of use as well. It's really just personal preference.

Here's how you would do roughly the same thing without built in variables:

Code:
///create event
hsp = 0;
vsp = 0;
spd = 10;

///how to set speed (replace in your current movement keys)
hsp = spd;
hsp = -spd;
vsp = spd;
vsp = -spd;

///how to stop movement
hsp = 0;
vsp = 0;

///step vent
x += hsp;
y += vsp;
There are other ways of course and lots of YouTube tutorials on it as well. See Friendly Cosmonaut or Shaun Spalding for example.
Thanks, but when I try to change x or y in step it doesn't work sometimes for some reason. GameMaker is just buggy I guess, but unfortunately, we must use it at university for projects.
 

TheouAegis

Member
That's the thing, no physics)
Not in the object? Not in the room?

Wait, you said it doesn't work SOMETIMES? That's not a bug in Game Maker, that's a serious bug in your code. You should post the revised code. Game Maker's bugs have nothing to do with such simple things (although there was that one time when image_xscale and image_yscale were broken).
 
V

Vitalii Lukyanov

Guest
Not in the object? Not in the room?

Wait, you said it doesn't work SOMETIMES? That's not a bug in Game Maker, that's a serious bug in your code. You should post the revised code. Game Maker's bugs have nothing to do with such simple things (although there was that one time when image_xscale and image_yscale were broken).
Well, I don't need it en anyways, and I don't have that much time before the deadline, so it'll wait till I need it someday.
 
Top