Are my comments misleading my understanding?

S

septuagint84

Guest
GML:
if selected=0 {
    selected =1 //This means, draw the circle, no matter what object I click, but other won't go back to 0
}
else if selected=1{//wherever the circle is drawn, if left pressed, the circle would dissappear
    
    //you want -->selected to get to -->0 if you click on -->another one.>>>>[B]another could means WITH? I though I would use if or ID's in this scenario.[/B]
    
[B][U]    with o_bones{[/U][/B]
[U][B]        selected=0[/B][/U]
[B][U]    }[/U][/B]
//My question is why couldn't I figure out this with statement out
selected=0
}
So, you may or not, know, but I'm following this youtube course online since it's free and gives me "challenges". This was to click and select different characters. This was the first challenge I was stumped on. The BOLD part was what I could not figure out on my own. So I added the Bold comment, last. Before that BOLD COMMENT, that comment was suppose to help me figure it out. It didn't.

Basically, I'm trying to change my way of thinking, to understand coding better in general, but can anyone tell, where my comments Logic may mislead me? Or is this something I have to figure out in my journey.

Thanks
 

FrostyCat

Redemption Seeker
Read the Manual's entry on with statements.
The manual's entry on with statements said:
Its global form is:

GML:
with (<expression>) <statement>
<expression> indicates one or more instances, and for this you can use an instance id, the name of an object (which indicates all instances of this object are to run the code block) or one of the special keywords (all, self, other). <statement> is now executed for each of the indicated instances, as if that instance is the current (self) instance.
This information should have allowed you to deduce that this sets selected back to 0 in every instance of o_bones:
GML:
with (o_bones) {
    selected = 0;
}
Don't just rely on tutorials and course series on YouTube. Stop when you see an unfamiliar function/variable name or GML keyword, and look it up in the Manual's Index tab.
 

Rob

Member
So you have multiple instances of o_bones?

You want one instance to have its selected value be 1 if clicked and then make sure the rest of the instances have the same variablea set to 0?

Everything you posted looks right to me, apart from the order.

The steps are:
If clicked, set ALL o_bones to not selected (this will also affect the instance that is clicked, so this should be done first).

With the instance that is clicked -
selected = !selected ( a short hand way to code your if / else statement)

Basically you should use the with statement first as it affects all instances of that object. Doing it afterwards will overwrite the if/else.

You could do it in a different order but you'd need to check for the calling instance within the with statement and that's unnecessary as far as I can tell.
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
with is a loop that runs code from the perspective of everything it matches; usually a single object (which means every instance of that object, and also every instance of all of its children) but it's often handy to use the special keywords all or other to loop over every instance in the current room, and the other object in a collision event.

Thus,
GML:
with(o_bones){
  selected = 0;
}
will unset "selected" for every bone, not just this one.
 
Top