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

Discussion in 'Programming' started by Energy Engine, Mar 17, 2019.

  1. Energy Engine

    Energy Engine Member

    Joined:
    Jun 23, 2016
    Posts:
    60
    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!
     
  2. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    301
    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
     
  3. VagrantWhaleGames

    VagrantWhaleGames Member

    Joined:
    Jul 10, 2018
    Posts:
    58
    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);
       }
    
     
  4. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,345
    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.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    Just break the with loop once you find an omini to throw away. No point letting the with keep running.
     
    Nocturne likes this.
  6. Energy Engine

    Energy Engine Member

    Joined:
    Jun 23, 2016
    Posts:
    60
    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.

    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 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.

    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.
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    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.
     
  8. Energy Engine

    Energy Engine Member

    Joined:
    Jun 23, 2016
    Posts:
    60
    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.
     
  9. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    301
    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
     
  10. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    No. Attaching and being attached are two different things.

    The omino needs to check if it is connected to the player.

    with omini
    if place_meeting(x,y,other) {
    hspeed=7; break;
    }
     
    Energy Engine likes this.
  11. Energy Engine

    Energy Engine Member

    Joined:
    Jun 23, 2016
    Posts:
    60
    TheouAegis, I tried your adjustment and it works correctly now!

    Thanks everyone for their input!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice