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

Legacy GM Two questions.

R

RisingKane

Guest
Hi everyone!

I'd like to know if it's possible to call a piece of code (something like a function) of an object from another one. Because I want to make things more organized in my code, and instead of writting it every time I need, I would just have it written in one object, and call it when necessary.

The other thing is, when I click to run my game, my character starts moving on its own. Now, I'm trying to make him stop one tile before a hole in the level, but I've only managed to make him stop in the tile above the hole. I put a picture of what I'm trying to do. The red circle is where I want the character to stop.

Thanks!
 

Attachments

First question, you use scripts for that. Check out scripts in the manual. You can call the code from any object at any time.

Second question, without seeing your code can't tell how you're doing your movement. But a simple way of tackling this is to use place_meeting, checking that the floor is below the character each step. If it's not there, don't move. Again, recomend looking up place_meeting in the manual if you're not familiar with this function.

Any issues post your code and we can look at it :)
 
  • Like
Reactions: 607
R

RisingKane

Guest
First question, you use scripts for that. Check out scripts in the manual. You can call the code from any object at any time.

Second question, without seeing your code can't tell how you're doing your movement. But a simple way of tackling this is to use place_meeting, checking that the floor is below the character each step. If it's not there, don't move. Again, recomend looking up place_meeting in the manual if you're not familiar with this function.

Any issues post your code and we can look at it :)
Hi Michael!

Didn't know about that, I'll check out the manual and read about scripts. Thanks!

About the second question, I'm already using place_meeting:

Code:
if (!place_meeting(x, y+1, obj_solid)) {
                    moveSpeed = 0;
            }
where obj_solid is the ground

The problem is that it only detects collision in the y axis, that's why the player stops in the tile above the hole. I tried adding some other value to the x argument in the place_meeting function, but it didn't work.

Again, thanks for your help!
 

Roderick

Member
You want to check place_meeting(x + sign(name of your horizontal speed variable goes here), y + 1, obj_solid) to see what's in the step ahead.
 
R

RisingKane

Guest
You want to check place_meeting(x + sign(name of your horizontal speed variable goes here), y + 1, obj_solid) to see what's in the step ahead.
Thanks for your help, Roderick!

I tried doing what you said, but the problem is that I need to check if there is not a collision in the y axis, that's why I used the "!" before the function. And in the x axis there's nothing for the player to collide with. Here's the piece of code I wrote:

Code:
if (!place_meeting(x+sign(movSpeed), y+1, obj_solid)) {
    movSpeed = 0;
}
 

Roderick

Member
I see what I missed. That code is still getting a collision with the ground that you're about to step off of, because place_meeting checks the current sprite mask at the target location.

You'll want to use position_meeting to check a single pixel.

Assuming that your sprite's origin is centered, this will still let the player move partway off the edge. To make them stop before they can step off the edge, you'd need to add half the width of the sprite.

x + (sign(movSpeed) * (sprite_width / 2 + 1))
 
R

RisingKane

Guest
I see what I missed. That code is still getting a collision with the ground that you're about to step off of, because place_meeting checks the current sprite mask at the target location.

You'll want to use position_meeting to check a single pixel.

Assuming that your sprite's origin is centered, this will still let the player move partway off the edge. To make them stop before they can step off the edge, you'd need to add half the width of the sprite.

x + (sign(movSpeed) * (sprite_width / 2 + 1))
I think I didn't get exactly what you said, Roderick, but I tried it like this:

Code:
if (position_meeting(x+(sign(movSpeed)*(sprite_width/2+1)), y+1, obj_solid)) {
                            movSpeed = 0;         
                        }
In this case, the player ignores the hole and keeps moving forward. I think that happens because there's no collision being detected horizontally and even though there is a collision vertically, it'll return false. Therefore, the player won't stop.
If I negate the position_meeting with a "!", the player stops above the hole, since both arguments are true.

Please correct me if I'm wrong, and thanks for your attention.
 
Oh I'm sorry, I didn't really think it through. Usually I would have the character go off the edge, hanging as far as possible before dropping off. But in your picture you don't want that, you want to stop at edge. In which case i'd probably use a collision point test, one pixel ahead and one pixel down from your sprite mask (testing direction of movement so know which direction to test) - if no floor no move.
 
R

RisingKane

Guest
Oh I'm sorry, I didn't really think it through. Usually I would have the character go off the edge, hanging as far as possible before dropping off. But in your picture you don't want that, you want to stop at edge. In which case i'd probably use a collision point test, one pixel ahead and one pixel down from your sprite mask (testing direction of movement so know which direction to test) - if no floor no move.
I'll check out the collision point test and see what I can do. As soon as I get something working or not, I'll post here.
Thanks for the support, everyone!
 
Top