GameMaker: Studio

GameMaker: Studio

View Stats:
chefjeff Jul 7, 2015 @ 4:34pm
Making object A change sprite from a click of object B
I have this puzzle, hopefully you guys can help me figure it out:

When the player hovers over object A or B while they are not selected, they change their sprites from a mouse enter event.

When the player clicks on object A or B, they become selected. Only one object may be selected at a time.

So if object A is selected and object B is clicked to become selected, how does object A change its sprite back to the unselected sprite?

I cannot use a step event to check the variable holding information for the selected object, as then it would ruin the use of the mouse enter event (the step event would constantly change the sprite back to the unselected sprite even when the mouse is hovering over a object)

Can you guys help me? Thanks.
< >
Showing 1-13 of 13 comments
Blind Jul 7, 2015 @ 4:38pm 
From your clicker object

with(selectableobject){
if other.clickableID = id selectable = true
if !other.clickableID = id selectable = false
}

clickableID is the storeage variable on your clicker of what item "should" be selectable and should be the exact ID of the clickable object. variable selectable is the local seletableobject variable for if I'm currently selected.
Last edited by Blind; Jul 7, 2015 @ 4:39pm
chefjeff Jul 7, 2015 @ 4:56pm 
Originally posted by BBX:
From your clicker object

with(selectableobject){
if other.clickableID = id selectable = true
if !other.clickableID = id selectable = false
}

clickableID is the storeage variable on your clicker of what item "should" be selectable. variable selectable is the local seletableobject variable for if I'm currently selected.

Thanks for the quick reply.

I don't have a mastery of GML, but I can kinda of tell what this is trying to do (correct me if I am wrong please):

With the object that is not selected

if the local variable of the other object that is not selected is true...

and

if the local variable of the other object that is not selected is false...
Alcator Jul 8, 2015 @ 5:42am 
Originally posted by BBX:
From your clicker object

with(selectableobject){
if other.clickableID = id selectable = true
if !other.clickableID = id selectable = false
}

clickableID is the storeage variable on your clicker of what item "should" be selectable and should be the exact ID of the clickable object. variable selectable is the local seletableobject variable for if I'm currently selected.

DO NOT USE "=" for comparison. Use "==". It's a big headache saver.

Furthermore, learn the practice of ALWAYS putting the condition of "if" statement into brackets:

if (condition) { statement; statement; ... statement; }

Why?
Because if you later add additional content to the curly brackets {...}, then it will work; if you do not use the curly brackets, the second statement will not be conditional (only if the "if" is true), which is a nightmare to troubleshoot.


chefjeff Jul 8, 2015 @ 6:49am 
Thanks for the reply, Alcator. But can you help me understand what this code is trying to say? (in lame man's terms)
Thew Jul 8, 2015 @ 7:26am 
Originally posted by The Jew:
(in lame man's terms)
Ha, I like that one.

Use the same object and duplicate it in the room, then try something like this:
//Global global.clicked_id = -1; //Best to declare this outside of the object //Create Event selected = false; hover = false; //Step Event if (position_meeting(mouse_x, mouse_y, obj_clickable)){ hover = true; //If mouse is over object, hover is true if (mouse_check_button_pressed(mb_left)){ global.clicked_id = id; //If mouse left clicks object, id is store globally } } else { hover = false; } if (global.clicked_id == id){ //Does the globally stored id match this object's id? selected = true; } else { selected = false; }

(Sorry for any typos)
Last edited by Thew; Jul 8, 2015 @ 10:01am
Blind Jul 8, 2015 @ 9:39am 
It was just psuedo code

Also, I've never had to use == in Gamemaker
Alcator Jul 8, 2015 @ 10:26am 
Originally posted by BBX:
It was just psuedo code

Also, I've never had to use == in Gamemaker

Trust me, learn it / switch to it.

The PROBLEM is that "=" can mean "assignment" (evaluate the stuff on the right and assign it to the variable on the left), but it can also mean "comparison".

And one of the coolest programming technique is to shorten code:
instead of:

if (a equals b) then varEqual = true else varEqual = false;

you would write:

varEqual = (a equals b).

But if "=" means both, you would get:

varEqual = a = b

And suddenly, you have a problem. Does this mean:
take the value of b and assign it to variable a, then take the new value of a and assign it to varEqual?
or does it mean "assign the logical value of the comparison between a and b and assign it to varEqual?

imagine:
a = 5;
b = 6;

varEqual = a = b

in the first style, varEqual will become 6. In the second, it will become "false".

You really do not want this kind of ambiguity in your code!

varEqual = (a == b) will always mean exactly one thing: compare the two, and store the result in varEqual.

Blind Jul 8, 2015 @ 11:55am 
I wouldn't want such ambiguity either, however... what kinda junky line contains two comparisons?
Alcator Jul 8, 2015 @ 1:56pm 
Originally posted by BBX:
I wouldn't want such ambiguity either, however... what kinda junky line contains two comparisons?

Not two comparisons -- two assignments. That's actually quite common in C programming, in which an assignment is automatically an expression that can be further used.
Blind Jul 8, 2015 @ 2:14pm 
I grew up with QBasic, so there's always only one assignment per line. Indeed, the difference between = and == is if precision is to be counted (such as if 1.00001 is equal to 1 or not)
chefjeff Jul 8, 2015 @ 5:00pm 
Originally posted by Thew:
Originally posted by The Jew:
(in lame man's terms)
Ha, I like that one.

Use the same object and duplicate it in the room, then try something like this:
//Global global.clicked_id = -1; //Best to declare this outside of the object //Create Event selected = false; hover = false; //Step Event if (position_meeting(mouse_x, mouse_y, obj_clickable)){ hover = true; //If mouse is over object, hover is true if (mouse_check_button_pressed(mb_left)){ global.clicked_id = id; //If mouse left clicks object, id is store globally } } else { hover = false; } if (global.clicked_id == id){ //Does the globally stored id match this object's id? selected = true; } else { selected = false; }

(Sorry for any typos)

Okay I see what you're trying to do. This is sort of what I had in mind, but I didn't know you could put events inside of events (having the mouse button press in the if statement). This is all cool knowing that it's inside the step event too, so I can remove my separate code dealing with mouse enter and exit assigning sprites.
Blind Jul 8, 2015 @ 5:11pm 
Indeed, you can have every single action in the entire program run from a single "Main" object, with zero code on the objects themselves.
Last edited by Blind; Jul 8, 2015 @ 5:11pm
chefjeff Jul 8, 2015 @ 7:52pm 
It works! Thanks guys. I learn more about gamemaker every day...
< >
Showing 1-13 of 13 comments
Per page: 1530 50

Date Posted: Jul 7, 2015 @ 4:34pm
Posts: 13