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

SOLVED Interact with a single instance

flyinian

Member
I have instances that the player can set on fire.

If these instances are close to one another, the player can interact with all of them at the same time.

What can I do to prevent this from happening.

Code(Step):

GML:
if(distance_to_object(Obj_Player) < 30 && !_SetonFire){
_Close = true;}
else{
_Close = false;};

if(keyboard_check_pressed(ord("E")) && _Close){
    _SetonFire = true;
    show_message(string(id) +" : "+ string(current_time)); // When I have this, it only sets one instance on fire. W/o it, it sets all interacted instances on fire.  Is this because the show_message halts code?
};
  1. I know that I could potentially prevent this by shrinking the distance to the object, but I think a better way may be needed in case other scenarios rise up.
  2. I've thought about doing a list and only interacting with the earliest entry.
  3. Also thought about using a global variable as a control variable for interactions.


Thanks.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
How are you checking whether the player is close or not? The code above tells us nothing really... Although it DOES tell me the grass is doing the checking? I would have the check code in the player, as that'll make everything MUCH easier. But anyway, we need to know how you're checking the distance for now.
 

flyinian

Member
How are you checking whether the player is close or not? The code above tells us nothing really... Although it DOES tell me the grass is doing the checking? I would have the check code in the player, as that'll make everything MUCH easier. But anyway, we need to know how you're checking the distance for now.
Updated the code in the op.
 

chamaeleon

Member
Seems to me that show_message() displaying a dialog box affects the keyboard_check_pressed() status and it gets reset. In other words, when the next instance comes along and runs it step event in the same step as the first, unlike normally, where keyboard_check_pressed() will return true for all instances testing it in that step, it has now been reset. Maybe it has something to do with window focus changing, I couldn't say. Perhaps a bug worthy of a bug report if you feel strongly about it, but I wouldn't necessarily count on it being changed.
 

chamaeleon

Member
A trivial project without any dependencies for bug submission would only need a single object with a step event containing
GML:
if (keyboard_check_pressed(ord("A"))) {
    show_debug_message("PRESSED KEY A [" + string(id) + "]");
} else if (keyboard_check_pressed(ord("S"))) {
    show_debug_message("PRESSED KEY S [" + string(id) + "]");
    show_message("IRRELEVANT POPUP");
}
Placing more than one instance in a room would result in the below output after pressing A two times and S three times.
Code:
PRESSED KEY A [100036]
PRESSED KEY A [100037]
PRESSED KEY A [100038]
PRESSED KEY A [100036]
PRESSED KEY A [100037]
PRESSED KEY A [100038]
PRESSED KEY S [100036]
PRESSED KEY S [100036]
PRESSED KEY S [100036]
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Do the checks in the PLAYER object. That way it'll be easier, since you know what direction the player is facing and can get the instance using instance_position. For example:
GML:
var _inst = noone;
switch(dir)
{
case left: _inst = instance_position(x - 10, y, obj_GRASS); break;
case right: _inst = instance_position(x + 10, y, obj_GRASS); break;
case up: _inst = instance_position(x, y - 10, obj_GRASS); break;
case down: _inst = instance_position(x, y + 10, obj_GRASS); break;
}
if instance_exists(_inst)
    {
    _inst. _SetonFire = true;
    }
}
Something like that would be better (the code above is just "pseudo code" to give you an idea about what I am suggesting... you'll need to adapt the idea to suit how your player is set up).
 

flyinian

Member
A trivial project without any dependencies for bug submission would only need a single object with a step event containing
GML:
if (keyboard_check_pressed(ord("A"))) {
    show_debug_message("PRESSED KEY A [" + string(id) + "]");
} else if (keyboard_check_pressed(ord("S"))) {
    show_debug_message("PRESSED KEY S [" + string(id) + "]");
    show_message("IRRELEVANT POPUP");
}
Placing more than one instance in a room would result in the below output after pressing A two times and S three times.
Code:
PRESSED KEY A [100036]
PRESSED KEY A [100037]
PRESSED KEY A [100038]
PRESSED KEY A [100036]
PRESSED KEY A [100037]
PRESSED KEY A [100038]
PRESSED KEY S [100036]
PRESSED KEY S [100036]
PRESSED KEY S [100036]

Shouldn't all show_debug_message be show_message since that's what's being tested?
 

chamaeleon

Member
Or if you prefer to not have a specific direction (but still do the work in the player as that seems more appropriate)
GML:
var closest = instance_nearest(x, y, obj_GRASS);
if (distance_to_object(closest) < 30 && !closest._SetonFire) {
    closest._SetonFire = true;
}
Edit: It is not clear from the first post whether another instance, further away from the closest, should be picked if the closest is already on fire.. The above simple code does not do that.
 
Last edited:

chamaeleon

Member
Shouldn't all show_debug_message be show_message since that's what's being tested?
I'm not testing show_message(). I am testing whether keyboard_check_pressed() returns true for each instance in a single step. When show_message() is used, it does not, when show_message() is not used, it does.
 

flyinian

Member
Do the checks in the PLAYER object. That way it'll be easier, since you know what direction the player is facing and can get the instance using instance_position. For example:
GML:
var _inst = noone;
switch(dir)
{
case left: _inst = instance_position(x - 10, y, obj_GRASS); break;
case right: _inst = instance_position(x + 10, y, obj_GRASS); break;
case up: _inst = instance_position(x, y - 10, obj_GRASS); break;
case down: _inst = instance_position(x, y + 10, obj_GRASS); break;
}
if instance_exists(_inst)
    {
    _inst. _SetonFire = true;
    }
}
Something like that would be better (the code above is just "pseudo code" to give you an idea about what I am suggesting... you'll need to adapt the idea to suit how your player is set up).
Thank you.
 
Top