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

Player being pushed by a flying block

Klanes

Member
Hello everyone, first, sorry for my English. I'm a complete coding noob, but I love to get in trouble creating some very very very basic games xD

In the game you must dodge some flying blocks. This blocks can push the player if he can't climb on top of them (or dodge them). Everything seems to be going well in the tests when the blocks don't move, but when they move the player ignores them or gets stuck in them.


Here is my code (sorry, i want try it in DnD mode).
(This code is in a script, ino a player setp event)

Here is a game preview with the problem.

Thanks in advance for the help, I thought that having well defined collisions flying blocks would not be a problem :(
 
Last edited:

NightFrost

Member
I don't know DnD so no code, but here's the general idea: after the flying block has moved, it must check for collision with the player. If a collision is detected, it must run a while-loop that continues until there is no player collision. Inside the loop, the player must be moved one pixel to the direction the flying block is moving (take sign of its horizontal speed). Note that flying blocks must be far enough apart that this code does not push player into another block. If player must avoid getting pushed inside a block while being pushed out of another, the code becomes more complex.
 

Klanes

Member
I don't know DnD so no code, but here's the general idea: after the flying block has moved, it must check for collision with the player. If a collision is detected, it must run a while-loop that continues until there is no player collision. Inside the loop, the player must be moved one pixel to the direction the flying block is moving (take sign of its horizontal speed). Note that flying blocks must be far enough apart that this code does not push player into another block. If player must avoid getting pushed inside a block while being pushed out of another, the code becomes more complex.
Thanks for your reply dude!
Well, broadly speaking, I understand the concept, but as I was saying, I am a complete noob in coding (my background is fully illustrator, designer and motion grapher but I love to get into creative trouble xD). In DnD you can also insert code directly.

I understand that all of these checks would go into a step event on the "flying block" object, right?

Although I know it's too much to ask, Would it be possible to have an example of this code?

Thank you in advance.
 

Klanes

Member
Hi again, I just realized another behavior that these "flying blocks" should have on playing. When the player is on top of one of them, it should also move at speed and in the direction of the block. I have no idea how to integrate this behavior either.
1589530785921.png
 

NightFrost

Member
Yes, the flying block would run the code. Written in GML, the push code would look something like (untested code):
GML:
if(position_meeting(x, y, obj_player)){
    do {
        obj_player.x += sign(Move_X);
    } until(!position_meeting(x, y, obj_player));
}
Substitute your own object and variable names to that code.

As for flying blocks moving a player that is riding on top, they must check for collision with player at their x - 1 position af if there is a collision, they must add their horizontal movement speed to player x-position.
 

Klanes

Member
Yes, the flying block would run the code. Written in GML, the push code would look something like (untested code):
GML:
if(position_meeting(x, y, obj_player)){
    do {
        obj_player.x += sign(Move_X);
    } until(!position_meeting(x, y, obj_player));
}
Substitute your own object and variable names to that code.

As for flying blocks moving a player that is riding on top, they must check for collision with player at their x - 1 position af if there is a collision, they must add their horizontal movement speed to player x-position.
👍 Thanks for your time. I will try it later and tell you. Cheers!
 

Klanes

Member
Yes, the flying block would run the code. Written in GML, the push code would look something like (untested code):
GML:
if(position_meeting(x, y, obj_player)){
    do {
        obj_player.x += sign(Move_X);
    } until(!position_meeting(x, y, obj_player));
}
Substitute your own object and variable names to that code.

As for flying blocks moving a player that is riding on top, they must check for collision with player at their x - 1 position af if there is a collision, they must add their horizontal movement speed to player x-position.
Hi again :) I'm testing the code and i have one question, What is exactly "Move_X" in your code? I have replaced the player object with the one I have in my game but I cannot identify move_x (sorry for this noob question).

Thanks!
 

NightFrost

Member
The Move_X variable refers to horizontal movement of the flying block; the value you add to its position every step.
 

Klanes

Member
Thanks again!

Well, it looks like the code works good but at some point it crashes completely (I have to use windows manager to close the game window).
I have the code in the "flying block" (object name "o_flying_block") collision event:

GML:
if(position_meeting(x, y, o_player)){
    do {
        o_player.x += sign(-o_flying_block.x);
    } until(!position_meeting(x, y, o_player));
}
But also, something must be really wrong because it only works if I put the object in the room and rescale it to make it smaller in Y, if I leave it at normal size it doesn't work :eek:

Thanks in advance for the help you are giving me!
 

Nidoking

Member
The amount you add to the block's x coordinate each step is x? It only moves by doubling its x position?

This will be an infinite loop if o_flying_block.x is 0.
 

NightFrost

Member
Also, since the code is running on the flying block, it doesn't need to reference self, using just the variable name is enough. (Incidentally, using o_flying_block object reference wouldn't be the right way to go about it anyway, as it selects a semi-random instance.)
 

Klanes

Member
The amount you add to the block's x coordinate each step is x? It only moves by doubling its x position?

This will be an infinite loop if o_flying_block.x is 0.
Also, since the code is running on the flying block, it doesn't need to reference self, using just the variable name is enough. (Incidentally, using o_flying_block object reference wouldn't be the right way to go about it anyway, as it selects a semi-random instance.)



Good morning and thanks for your responses guys!

I have been doing various tests and cannot get it to work properly. I'm a complete nooob and my poor English doesn't help either. Would it be possible to have an approximate code that also includes the behavior of holding the player on top and moving it in the direction of the block? I know it is a lot to ask, but I have the brain fried of trying a thousand tutorials .For my little game I have in mind solving this behavior of the flying block would be vital 😞

I hate asking for the code directly but I assure you I have been trying things all weekend
 
Hi Klanes,

You're on the right track. To me, it looks like the problem is you shouldn't use position_meeting for this. position_meeting only checks a single pixel, so when you're checking (x,y), you're actually only checking for a collision with the very top-left corner of the boxes. Instead of position_meeting, use place_meeting. Keep everything else the same. I think that will fix your problem.
 

NightFrost

Member
Oh, good catch, I didn't notice he was using position_meeting instead of place_meeting. Also, looking at your DnD code I assume the variable your blocks use for horizontal movement is called hsp, so that's what you want to take the sign from in the loop code, above.
 

Klanes

Member
Hi veryone! I will try what you tell me throughout the week and I inform you if everything is going well :) Thanks for your time!
 

Yal

🐧 *penguin noises*
GMC Elder
do {
o_player.x += sign(-o_flying_block.x);
} until(!position_meeting(x, y, o_player));
The infinite loop happens if the player and the flying block has the same x coordinate, since sign(0) = 0... and if you add 0 to the position until you don't collide anymore, you're never going to stop, since you're not moving anywhere.
 

Klanes

Member
The infinite loop happens if the player and the flying block has the same x coordinate, since sign(0) = 0... and if you add 0 to the position until you don't collide anymore, you're never going to stop, since you're not moving anywhere.
Please keep in mind that I am a real noob in coding... :( is the initial code of any use or can I not throw it away? xD
 

Yal

🐧 *penguin noises*
GMC Elder
Just change it like this and it shouldn't crash:
GML:
do {
  o_player.x += sign(-o_flying_block.x);
} until( !position_meeting(x, y, o_player) || sign(-o_flying_block.x) == 0);
Basically, if the speed is 0, abort the loop. (Otherwise the loop never ends if it is zero, since adding 0 to something doesn't change it)
 

Klanes

Member
Just change it like this and it shouldn't crash:
GML:
do {
  o_player.x += sign(-o_flying_block.x);
} until( !position_meeting(x, y, o_player) || sign(-o_flying_block.x) == 0);
Basically, if the speed is 0, abort the loop. (Otherwise the loop never ends if it is zero, since adding 0 to something doesn't change it)
Many thanks! This week I will test this solution and I hope it can finally work for me, I will keep you informed ♥
 
Top