Setting variables within the room editor

R

relic1882

Guest
I've been using some assets that allow me to change some of their values within the room editor by going to the variables button. I'm uncertain how this is accomplished.

My goal here is that I made a text box object that works perfectly, but I'd have to set the text manually within the object and that would require making a whole lot of them just for basic dialogue. Is there a way that I can conveniently set the text variable within the creation code of my dialog box so that I can access it with the variables button in the room editor? Can I do that while putting the object in multiple locations in a room and give them each a different text that way?

Thanks for the read! I put the code below for my dialogue box.

Code:
////////////CREATE///////////////////

y_button_on = false;
showTextBox = false;

//variable storing the dialogue that is drawn
text = "This is the text. Please change me to make it work for the scene.";

//get the width of the sprite for the dialogue box for word wrapping
boxWidth = sprite_get_width(TextBoxSprite);

//get the height of the font used for spacing purposes
stringHeight = string_height(text) / 2;

//create button prompt
instance_Y_Button = instance_create_layer(x, y - 20, "UIlayer", Y_button_Prompt);


//////////////STEP///////////////////


//activate interaction prompt if player is close
if (distance_to_object(Player1) <= 10) && !showTextBox
{
    y_button_on = true;
    instance_activate_object(instance_Y_Button);    //activate player prompt
}
else if (distance_to_object(Player1 > 10))
{
    y_button_on = false;
    instance_deactivate_object(instance_Y_Button);    //deactivate player prompt
}

//if interaction prompt is active, enable text box and deactivate the interaction prompt
if (y_button_on && Player1.inputInteract)
{
    instance_deactivate_object(instance_Y_Button);
    showTextBox = true;
}

//if player walks away from object while text is up, disable text and reset values
if (distance_to_object(Player1) > 10) && showTextBox
{
    y_button_on = false;
    showTextBox = false;
}



/////////////////////DRAW////////////////////

//if player interacts, draw text

if (showTextBox)
{
    //draw textbox
    draw_sprite(TextBoxSprite, 0, x, y - sprite_get_height(TextBoxSprite) - 10);

    //draw text
    draw_set_font(fontTextBox);
    draw_text_ext(x, y - sprite_get_height(TextBoxSprite) - 10, text, stringHeight, boxWidth);
    
}
 

CloseRange

Member
Honestly I don't know how the new Variables button works and i'd like to learn (even though it seems very neich)

However you can just do it the old 1.x way. if you have an object called obj_text
Code:
/// obj text create event
text = "";
then drag 2 into the room, go into the "creation code" button (right below Variables) and do:
Code:
text = "hello"
for one text and:
Code:
text = "world"
for the other.

now one instance has the variable text = "hello and the other has text = "world"
 
R

relic1882

Guest
Honestly I don't know how the new Variables button works and i'd like to learn (even though it seems very neich)

However you can just do it the old 1.x way. if you have an object called obj_text
Code:
/// obj text create event
text = "";
then drag 2 into the room, go into the "creation code" button (right below Variables) and do:
Code:
text = "hello"
for one text and:
Code:
text = "world"
for the other.

now one instance has the variable text = "hello and the other has text = "world"
That's working very well. I still want to know how the Variables button works too, but this will do just fine. Thanks for the reply! If anyone knows how to explain the Variables button, please enlighten us! Thank you!
 
Variables defined in the Variables dialog are processed *before* the Create event runs. So these variables are created before the Create event runs. You can override them in the Create event if you wish.

Also, these variables are automatically inherited by any child objects.

You can edit the inherited values in the child objects if you wish to change them.

It's a convenient way to change instance defaults when working from the room editor, you don't have to open the Create Event or deal with any code. A potentially useful tool for artists / designers if they are using GMS 2 on a team, and want to change instance properties without touching code.

There are more details in the manual under Editors->Objects

https://docs2.yoyogames.com/index.html?page=source/_build/2_interface/1_editors/objects.html
 
R

relic1882

Guest
Good deal. Thanks for the information. It seems I'll be better off using the creation code. Seems like the easiest way to get what I need while getting more customization options.

Thanks again guys!
 

samspade

Member
I never use creation code anymore - at least not to set variables. It is less useful both because of when it runs and because it is easy to forgot and harder to access and harder to customize. If you are placing objects in the room editor, you should use the instance variables. They are significant improvements in almost every way (for things placed in the room editor - otherwise they probably shouldn't be used at all).
 
R

relic1882

Guest
Honestly I don't know how the new Variables button works and i'd like to learn (even though it seems very neich)

However you can just do it the old 1.x way. if you have an object called obj_text
Code:
/// obj text create event
text = "";
then drag 2 into the room, go into the "creation code" button (right below Variables) and do:
Code:
text = "hello"
for one text and:
Code:
text = "world"
for the other.

now one instance has the variable text = "hello and the other has text = "world"
I've dug into it a little more and the Variables button is actually very easy to use.

All you have to do is click the Variable Definitions... button when you're on your object in the workspace and you can add your variables from there. Once added they are available for editing in the room editor under the Variables button. As long as you don't have those variables defined in creation code or a create event, you can set them there very easily. I can place as many text box or dialogue objects as I want in the room, while giving each one different strings to display when interacting with them. Now that I understand how it works I'll be using this feature as it will make object placement a whole lot easier.

P.S. I also just made this work for my door objects. Now instead of having to code in where a door takes the player and make different objects every time for each teleport, a single door object has all the variables easily accessible to use for every single door in the game. This is an amazing feature and it's making my life a whole lot easier. Thanks again all you guys for the input!
 
Last edited by a moderator:
Top