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

GameMaker [Solved] Affect "other" one at a time during collision

Heya,

A simple issue. I have a player object, "oplayer "and an object "omini" that "clings" to oplayer. I'm trying to have it so that multiple instances of omini are able to cling to oplayer via a collision event, and oplayer is able to throw off those instances by pressing a key.

Normally, this would be straight-forward if I wanted to throw off the omini instances all at once by simply using with(other) {//throw code} inside a collision event with object omini (or a parent maybe). But what if I wanted oplayer to throw off any of the omini instances one at a time instead of all at once? The order in which they get thrown off doesn't matter.

I've tried looking around a way to address this, but didn't find anything. What would be a good way to achieve this?

Thanks!
 
T

Taddio

Guest
You have to use an instance id for obj_omini, otherwise it will affect all of them. Same principle as when you click on a chest and they all open, or when you shoot an enemy and they all die.
Mark these words: Instance ID.
You use with(inst_id){} for your throwing code after that
 
V

VagrantWhaleGames

Guest
I think instance find could help with this.

Code:
instance_find

var i;
for (i = 0; i < instance_number(objBlock); i += 1)
   {
   block[i] = instance_find(objBlock,i);
   }
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Just use the "with" function but control it with a variable. So:

Code:
var _thrown = false;
with (obj_Omni)
{
if !_thrown
    {
    // throw code here
    _thrown = true;
    }
}
That should work fine and only throw them off one at a time.
 

TheouAegis

Member
Just use the "with" function but control it with a variable. So:

Code:
var _thrown = false;
with (obj_Omni)
{
if !_thrown
    {
    // throw code here
    _thrown = true;
    }
}
That should work fine and only throw them off one at a time.
Just break the with loop once you find an omini to throw away. No point letting the with keep running.
 
Thank you all for the replies.

I'm still having trouble though. I'll address each solution, but I'd like to preface by saying that I am aware I'm doing something wrong, and will post my code.

You have to use an instance id for obj_omini, otherwise it will affect all of them. Same principle as when you click on a chest and they all open, or when you shoot an enemy and they all die.
Mark these words: Instance ID.
You use with(inst_id){} for your throwing code after that
In the case you highlighted above, with(other) would work fine. But if you had, say, two treasure chests close enough to one another so that both's collision mask covered a cursos object's mask, clicking would open both. How would you open only one while both were in contact with the cursor? With your method, I tried the following in oplayer's collision event with omini:

Code:
if keyboard_check_pressed(vk_left)
{
    with (omini)
    {
        with(instance_id)
            {
            hspeed=7
            }
    }
}
Nothing triggers however.

I think instance find could help with this.

Code:
instance_find

var i;
for (i = 0; i < instance_number(objBlock); i += 1)
   {
   block[i] = instance_find(objBlock,i);
   }
I tried this, but my syntax is off. In the collision event, I used (block (with and without the i) in place of (other), but this gives unknown variable errors.

Just use the "with" function but control it with a variable. So:

Code:
var _thrown = false;
with (obj_Omni)
{
if !_thrown
    {
    // throw code here
    _thrown = true;
    }
}
That should work fine and only throw them off one at a time.
Adjusted code (w/ TheouAegis's suggestion) in Collision Event with omini:

Code:
if keyboard_check_pressed(vk_left)
    {
    with (omini)
        {
        var _thrown = false;
        if !_thrown
            {
            hspeed=7
            _thrown = true;
            break;
            }
        }
    }

This method causes only one specific omini obj to get thrown off regardless of which of them comes in contact with oplayer. If the break is commented out, it affects all of them.

Thanks for everyone's input.
 

TheouAegis

Member
With nocturnes code, you have to check if the omini in question is actually attached to the player. And you don't need the _thrown variable.
 
Isn't the check for if omini is attached to the player already being done? The collision event was doing that:

upload_2019-3-17_16-30-40.png

In case you meant using place_meeting in the Step event, adjusted code with _thrown variable removed:

Code:
if place_meeting(x,y,omini)
{
if keyboard_check_pressed(vk_left)
    {
    with (omini)
        {      
            {
            hspeed=7 //"thrown off" part   
            break;
            }
        }
    }
}
This produces the same result though. I made sure to comment out the code in the collision event.

Thanks.
 
T

Taddio

Guest
In the case you highlighted above, with(other) would work fine. But if you had, say, two treasure chests close enough to one another so that both's collision mask covered a cursos object's mask, clicking would open both. How would you open only one while both were in contact with the cursor? With your method, I tried the following in oplayer's collision event with omini:
Im guessing you have your throw action setup in STEP and not in collision event, and if that's the case, "other" doesn't mean anything in the step event, unless it's inside a with(), but then again, you'd want the inst_id to be in the parenthesis, not just the obj_name (unless you just have one instance of it throughout, in this case nevermind what I say).
And you have to actually GET the instance ID, not just copy ---> with(inst_id)
You would have to actually assign inst_id to the "real" instance ID
 

TheouAegis

Member
Isn't the check for if omini is attached to the player already being done? The collision event was doing that:
No. Attaching and being attached are two different things.

In case you meant using place_meeting in the Step event, adjusted code with _thrown variable removed:
The omino needs to check if it is connected to the player.

with omini
if place_meeting(x,y,other) {
hspeed=7; break;
}
 
Top