if-statement Problem

Discussion in 'Legacy GameMaker Community Tech Support' started by MPrange, Jul 8, 2018.

Tags:
  1. MPrange

    MPrange Member

    Joined:
    Sep 11, 2017
    Posts:
    3
    Hi,
    I have a problem with an if-statement and I can't figure it out.

    I am working on a point 'n click game.

    What I want:
    a) When the mousepointer (obj_mousepointer) is moved over an object then show textline 1 (showtext = 1) in a textbox (obj_textbox). e.g.: "This is an old door".
    b) When the mousepointer is moved on an object AND the player stands on top of the object then show textline 2. "The door is locked".
    c) When neither is the case just show no nothing at all.
    The textbox should be removed as soon as the mousepointer is not placed over the object any more or another line is displayed.

    Problem:
    When I run the game and place the mousepointer in an object the textbox is not displayed.
    What confuses me to no end is, that two of my debug messages are displayed: "Mousecursor on Obj" AND "No Object selected".
    It seems to me, that the first condition is always true therefore it destroys obj_textbox.
    When i comment out instance_destroy(obj_textbox) everything is displayed.

    I changed the order of the statements, tried switches and some other variations of if, if not, else, else if etc. but to no avail.

    Obj_Textbox holds only information on the textbox itself. The Draw -Event just starts the text-script.

    Sorry if I missed the solution in an other thread.

    I am glad for every hint.

    Code:
    if (!place_meeting(x,y, obj_mousepointer)) {
        showtext = 0;
        instance_destroy(obj_textbox);
        show_debug_message(string("No Object selected"));
    }
    
    else if (place_meeting(x,y, obj_mousepointer) && !place_meeting(x,y,obj_player)) {
        showtext = 1;
        show_debug_message(string("Mousecursor on Obj"));
    }
    
    else if (place_meeting(x,y, obj_mousepointer) && place_meeting(x,y,obj_player)) {
        showtext = 2;
        show_debug_message(string("Player & Mousecursor on Obj"));
    }
    
     
  2. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,454
    In what object do you run this? Do you run this in more than one instance?
    Whenever this objects x and y is not colliding with obj_mousepointer object you destroy all obj_textbox.

    If you run this in multiple object !place_meeting(x,y, obj_mousepointer) will always be true and destroy any obj_textbox.
     
  3. MPrange

    MPrange Member

    Joined:
    Sep 11, 2017
    Posts:
    3
    sorry, i forgot to add that info:
    i am running this in an parent object named "obj_object_parent". It is the overall parent for all "Objects" in the Game and all Objects should have this textbox code (which means every object should show a textline to the player)
    So far i created two sub-parents, for Items and for Doors. So it runs on both these sub-parents and the objects e.g. obj_door_open, obj_door_closed, obj_door_barred etc.

    The condition !place_meeting(x,y, obj_mousepointer) should be false as soon as the mousepointer hits a child of the "obj_object_parent", right? In that case the obj_textbox should be visible.
    Or to a have a logic-error in my thinking? Sorry, I'm still learning the basics of programming...
     
  4. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,454
    If you run this in two instances at least one of them is going to destroy all obj_textbox. Because one will not have the mouse object colliding with it.
     
  5. MPrange

    MPrange Member

    Joined:
    Sep 11, 2017
    Posts:
    3
    you were quite right of course, thanks a lot! Today i was able to test it by removing the second child-object and that did the trick. I missunderstood the parent-child logic. I thought GM would count every place_meeting with an child object as an interaction with obj_object_parent. But if i understand you correctly it the other way around. place_meeting = true with a child means for a second child automatically place_meeting = false, right? As long as both children are not at the same place.
    Unfortunately it also means, i have to re-think my whol parent structure. Is there an elegant way to fix this? Using an alarm to remove the obj_textbox after a second or so seems okay to me, but not really flexible.
     

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