SOLVED obj.vis = false, having some trouble

mitzy

Member
Hello everyone! I'm very new to Gamemaker and GML and I was wondering if anyone could help me out with a problem I am having.

I am trying to make it so that when an obj_Slider1 is left clicked the sprite for obj_Slider1 switches and that a seperate Object named Obj_Moon turns visible/invisible. I have been able to make the Slider switch sprites back and forth when you click but Obj_Moon is only able to be visible and not invisible. My code is below- any help is appreciated. Thank you!


if (sprite_index == spr_Slider1Left)
{
sprite_index = spr_Slider1Right;
obj_Moon.visible = true
}

//Switches sprites, makes obj_Moon invisible
else if (sprite_index == spr_Slider1Right)
{
sprite_index = spr_Slider1Left;
obj_Moon.visible = false
}
 

Mk.2

Member
Are you sure there's no code in obj_Moon using the visible variable? Or any other objects referencing it?
 

mitzy

Member
I've had a look thru the other objects and sadly obj_Slider1, the one using it, is the only one referencing it :(
 

Skull00

Member
Maybe try to set the visibility from obj_Moon in the object itself, by checking the sprite_index of obj_Slider1:

GML:
// obj_Moon STEP EVENT
visible = true;
if (obj_Slider1.sprite_index == spr_Slider1Left)
    visible = false;
 

CruelBus

Member
Disregard the following with my apologies:

If you apply visibility to an object, any instances of that object that are created AFTERWARDS will follow that visibility.

You need to be changing the visibility of the instance of obj_Moon that is in the room.


End disregard.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
If you apply visibility to an object, any instances of that object that are created AFTERWARDS will follow that visibility.

You need to be changing the visibility of the instance of obj_Moon that is in the room.
No, that's not how that works. Writing to a variable in an object context will write to all instances of that object.

What you're describing is the function object_set_visible, which is unrelated to this topic.

Hello everyone! I'm very new to Gamemaker and GML and I was wondering if anyone could help me out with a problem I am having.

I am trying to make it so that when an obj_Slider1 is left clicked the sprite for obj_Slider1 switches and that a seperate Object named Obj_Moon turns visible/invisible. I have been able to make the Slider switch sprites back and forth when you click but Obj_Moon is only able to be visible and not invisible. My code is below- any help is appreciated. Thank you!


if (sprite_index == spr_Slider1Left)
{
sprite_index = spr_Slider1Right;
obj_Moon.visible = true
}

//Switches sprites, makes obj_Moon invisible
else if (sprite_index == spr_Slider1Right)
{
sprite_index = spr_Slider1Left;
obj_Moon.visible = false
}
You describe the object as being named Obj_Moon, but in your code, you call it obj_Moon. What is its actual name? GML is case-sensitive, so mistyping an object name will result in a crash at best, or at worst, it will be treated as 0 if you're using an older version of GameMaker that still supports treating uninitialized variables as 0, which would lead to unintended behavior.
 

CruelBus

Member
@TsukaYuriko ah, thanks then. I've always referenced a variable that holds an instance reference. The docs for "Objects" are misleading then:

IMPORTANT! Changing anything about an object will not change any instances currently present in the room, only those that are created after the change
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Please file a ticket if you'd like to report any mistakes in the documentation. I personally wouldn't say it's misleading, as you are in fact not changing anything about an object, but about all instances of an object, but I'd leave it up to our technical writer to decide if something needs to be clarified about this.
 

Nidoking

Member
You can't do anything to an object without using a function that specifically manipulates the object. Using an object index as a reference to read or write a variable will implicitly affect one (or all) instance of that object, not the object, which is still a relatively immutable type of thing defined in the game's resources. It's like changing a diagram in the printing plates for a book. Any new books will be printed with the new diagram, but the books that have already been printed will still have the old diagram, and you'll have to go track them all down if you want to paste in a correction.
 

mitzy

Member
Thanks for all your help everyone! A friend of mine helped me out and the problem was that I had part of my background transparent which was causing the game not to draw it or something among those lines. All the information you have given me was very insightful and I have learned alot though! Cheers! 😀
 
Top