SOLVED Is there a way to null a variable from being used?

flyinian

Member
I am using the parenting system for my navigation system in my project.

Some buttons will go to a room when clicked and others will create instances.

If I use the below code with the buttons that don't go to a room, it will give an error.

Is there a way to have a variable return a value to tell game maker to not give an error and not execute the function?

I know i could add another argument to the if statement and check to see if _GoToRoom returns a room or not.

GML:
// Create Event
_GoToRoom = room1;


// Step Event
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y, _Object_1))
{
    if(room != _GoToRoom)          
    {
    room_goto(_GoToRoom);             
    };
};
 

TsukaYuriko

☄️
Forum Staff
Moderator
try ... catch in 2.3, but don't use that for this, as it's just misusing exception handling and therefore a bad approach to begin with. Using room_exists is not going to cause the world to end.
 

Evanski

Raccoon Lord
Forum Staff
Moderator
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y, _Object_1)) { if(room != _GoToRoom) { room_goto(_GoToRoom); }; };
I think you're going to have to bite the bullet and edit it like this
GML:
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y, _Object_1))
{
    if (Room_exists(_GoToRoom)) && (room != _GoToRoom)
    {
        room_goto(_GoToRoom);
    }
    else
    {
        //code here i guess
    }
};
 

flyinian

Member
I think you're going to have to bite the bullet and edit it like this
GML:
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y, _Object_1))
{
    if (Room_exists(_GoToRoom)) && (room != _GoToRoom)
    {
        room_goto(_GoToRoom);
    }
    else
    {
        //code here i guess
    }
};

I was thinking like this.
GML:
// Create Event
_GoToRoom = 0;        // create event for buttons that dont go to rooms
_GoToRoom = room1;    // create event for buttons that go to rooms.


// Step Event
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y, _Object_1) && _GoToRoom != 0)
{
    if(room != _GoToRoom)        
    {
    room_goto(_GoToRoom);            
    };
};
 

FrostyCat

Redemption Seeker
I would suggest using Object Variables for _GoToRoom. That way the variable is guaranteed to exist, and the value can be manually tuned by code in the Object Editor or by hand in the Room Editor.
GML:
// Create Event
_GoToRoom = 0;        // create event for buttons that dont go to rooms
_GoToRoom = room1;    // create event for buttons that go to rooms.


// Step Event
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y, _Object_1) && _GoToRoom != 0)
{
    if(room != _GoToRoom)        
    {
    room_goto(_GoToRoom);            
    };
};
This is no good, the default empty value has to be something like -1 or noone (-4). Anything 0 and above could be an actual room ID.
 

flyinian

Member
I would suggest using Object Variables for _GoToRoom. That way the variable is guaranteed to exist, and the value can be manually tuned by code in the Object Editor or by hand in the Room Editor.

This is no good, the default empty value has to be something like -1 or noone (-4). Anything 0 and above could be an actual room ID.
Thank you for the clarification
 
Top