1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Windows Problem with multiple instances changing a single variable

Discussion in 'Programming' started by Reprom, Feb 12, 2020.

  1. Reprom

    Reprom Member

    Joined:
    Feb 12, 2020
    Posts:
    2
    Hi. I have no idea if I missed a point of something, but this is leaving me completely speechless.

    I have this object that is supposed to be picked up by a player. After getting player into certain range it should set a variable to 1, indication that it is in a pickable range. If player is in this range you can press a button to pick it up. Everything is working as it should except that variable.

    Problem I have is that only the newest instance of that object is changing that variable. The older ones can still be correctly picked up, but it will not change that variable.

    objRedMushroom
    STEP:

    if collision_circle(self.x,self.y,collisionSize ,obj_player,false,false)
    {
    objDrawInteractGUI.showPickup = 1;
    if gamepad_button_check_pressed(0, gp_face1)
    {scrItemPickup(Item.RedMushroom);}
    if keyboard_check(ord("Q"))
    {scrItemPickup(Item.RedMushroom);}
    }
    else
    {
    objDrawInteractGUI.showPickup = 0;
    }

    If I use a collision event istead for changing the variable it works and every single instance of that object will be able to change it.

    Why its not working in a step event? Am I missing something?
     
  2. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,144
    By using the point method to target an object you are effectively asking gamemaker to choose which one to target, as that won't target all of them. This is usually the last one created, hence your issue (but it might not always be!). Instead use "with".

    Code:
    with (objDrawInteractGUI)
    {
    showPickup = 1;
    }
    Something like that...
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,442
    Remove the "else" condition. Have objDrawInteractGUI reset showPickup to 0 in itsBegin Step event.
     
  4. Reprom

    Reprom Member

    Joined:
    Feb 12, 2020
    Posts:
    2
    Thank you.

    Not only for a fix, but also steering me to the right direction. As Nocturne suggested, I also had my mind suck on the idea, that if it could be fixed with "with" statement. But now i finaly see the problem, that the last instace of that object was always determining/overwriting the showPickup value.

    I feel kind of stupid right now.
     
    Nocturne likes this.

Share This Page