• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

if-statement Problem

MPrange

Member
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"));
}
 

The-any-Key

Member
In what object do you run this? Do you run this in more than one instance?
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")); }
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.
 

MPrange

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

The-any-Key

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

MPrange

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