Initiating a player object animation contingent on the meeting of two other objects.

Bung

Member
I currently have a 'key object' in my room which, if it is in contact with another object, and the enter key is released, the room restarts. When the same conditions are met, but those two objects are not in contact when the enter key is released, the game moves to the next room.

My player object is in the same room. When the enter key is released, it initiates it's animation, which freezes on the last frame. The animation is currently the same for both conditions with regards to the two objects either meeting or not meeting. I'd like to retain this animation for when alarm 0 is triggered and the game moves to the next room. I would like to implement a different animation for when alarm1 is triggered, and before the room restarts. Or, have no animation event at all.

My question is:

How can I refer to these two other objects in the player object's code, so as to trigger a different animation (or no animation) for when the room restarts. I am very new to GML and GMS2 (ten days left on my free trial) and am keen to learn. I have studied the manuals and the forums, but I'm struggling a little I'm afraid. I am also developing a frame of reference for many of the functions, so I thank you in advance for your patient guidance and assistance. I really do appreciate it.

Thank you very much in advance.

The Code for the key object that controls the room transition/restarts:

KEY OBJECT STEP EVENT

if (!place_meeting(x,y,obj_blocker)) and keyboard_check_released(vk_enter) and !instance_exists(obj_fade)
{
alarm[0] = 200
}
if place_meeting(x,y,obj_blocker) and keyboard_check_released(vk_enter) and !instance_exists(obj_fade)
{
alarm[1] = 150
}

PLAYER OBJECT STEP EVENT

x = x + hsp
if place_meeting(x,y,obj_wall)
{
x = x - hsp
}
if keyboard_check_released(vk_enter)
{
sprite_index = spr_skycheck;
}
if image_index >= 41
{
image_speed = 0
}
 
Alright, welcome to GameMaker! Many good times await.

I believe the reason it's taking so long for you to get an answer is that it's a little hard to understand what you looking for. Usually, beginner questions get hit with an answer right out of the gate.

Are you wanting to check if two objects are colliding, and then in another object entirely, run code contingent on the collision status of the first two objects? If so just check for the collision of the first two objects, and set a variable based on the results. And then simply check the variable in the final object.

GML:
//with first_object
collision = place_meeting(x,y,second_object)//

//third object
if(first_object.collision = true)//maybe "true" isn't the right thing to check for.. I forget. It might actually return the id of the object collided with
{
    //excacute animation
}
else
{
    //you know
}
Is this in the ballpark of what you were looking for? I wasn't sure if a couple of things that you were talking about were relevant to the question (like the room restarting) It would be easier to understand if you remove everything that isn't absolutely necessary to the question.
 
Last edited:

Bung

Member
Thank you for replying, I really appreciate it.

Yes that is what I'm trying to achieve, and apologies if I wasn't very clear before hand.

I have attempted your suggestions but I'm afraid I am still having a great deal of trouble. Here is the code I have in the player object.

CREATE

hsp = 2

STEP

if place_meeting(x,y,obj_wall)
{
x = x - hsp
}

if keyboard_check_released(vk_enter)
{
sprite_index = spr_skycheck;
}

if image_index >= 41
{
image_speed = 0
}

However, no matter how I use the collision variable and indicating a return of true and false the animations do not happen how I'd like them to. One example of what I tried is the following:

if keyboard_check_released(vk_enter) and )obj_stars.collsion = false)
{
sprite_index = spr_skycheck;
}
else
{
spr_index = spr_fail
image_speed =
}

if image_index >= 41
{
image_speed = 0
}
 
Firstly I'm presuming that the two syntax error just from copying your code. Because your game would have errored.
GML:
//lots of end brackets, no starting brackets
if keyboard_check_released(vk_enter) and )obj_stars.collsion = false)
//equals what?
image_speed =
Your main issue might be that you are telling the game to only switch sprites to spr_skycheck when the enter key is released (which only happens for 1 step, or a 30th of a second) and the rest of the time to switch the sprite to spr_fail. Perhaps this would be more effective.

GML:
if (keyboard_check_released(vk_enter))
{
    if(obj_stars.collsion = false)
    {
        sprite_index = spr_skycheck;
    }
    else
    {
        sprite_index = spr_fail
    }
}
You should put your code inside code brackets because it looks nicer. It's just an option on the top panel where B I U are
 

FrostyCat

Member
Both of you, read these articles:
Meeting vs. Instance
When you use a function that has meeting in its name, it just tells whether there is a collision and returns either true or false. On the other hand, functions starting with instance returns the colliding instance's id, or the special value noone (-4) when there is no collision.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.

Corollary: NEVER set or use an instance's own variables with object.variable. An instance's own variables can be referenced as-is without dot prefixes. DO NOT use self. If multiple instances of the object exists, you might end up setting the value for all instances or for some other instance (depending on the export).
 
Don't patronize me frosty. I am well aware of how instances and objects work. Simply forgetting the exact return value of a redundant function that doesn't ever need to be used doesn't warrant getting blasted by pages of user manual type documentation.
 

Bung

Member
Thank you for all your help. It was hugely helpful and has really contributed to me getting what I want out of my code.

I really appreciate you taking the time.

Thanks again.
 
Top