Node Copied to Clipboard!

Variables


Variables are powerful and have multiple uses in Modest3D. You can change a variables value using the ChangeTo node or save a value to a variable using the saveTo node.

Simple Static Variables

In this example we will create a simple lesson that will change the label to match the shape that is clicked.

The scene is setup with three shapes and a label right above the center cube.

The three shapes are dragged from the editor window to the story border. Each dereference a WaitForClick node and connect to a loop, which are all attached to a fork.

Now we are going to create a static Variable. In the Namespaces Tab right-click on the Lesson text and selecting Add > Variable

Give your variable a name and click add.

Variables also require a type. To give your variable a type click on the variable name in the Namespace tab and look at the properties tab.

There are several different types of variables:

  • Bool - Variable represents either true or false
  • Int - Variable represents a round number without decimals
  • Float - Variable represents a decimal number
  • String - Variable represents text
  • Vector2 - Is a 2d position and is usually used for positioning things on the screen
  • Vector3 - Is a 3d position and is usually used for positioning things in the 3d scene
  • Quaternion - Is a 3d rotation. These are used for storing what direction a 3d object is facing

For this example we will choose a string. Type the word 'string' into the Type property of your variable.

After naming & selecting a variable type, you will be able to populate the variable in the storyboarder by dragging the variable from the Namespaces tab to the storyboarder. Dereference the variable by clicking the bottom handler. Then start typing change to, to populate the Change To node.

Right-click in the empty space of the ChangeTo node. Type String(Constant) to populate a blank string constant node. Click on this node and type the name of one of the shapes in the value property. The words will appear on the string node inside the ChangeTo node.

Attach the ChangeTo node to the WaitForClick node dereferenced from the Box.

Copy this node twice by holding Alt while dragging it or right-click copy and pasting. click on the string(constant) and change the values in these duplicated nodes to match the other shape names.

Attach the the ChangeTo nodes to their respective WaitForClick nodes.

Drag the Label from the Scene Hierarchy to the storyborder. Dereference the Label node by clicking & dragging the bottom handler and select Text. Dereference the Text node and select the ChangeTo node.

Connect this ChangeTo node to the ChangeTo Box node.

Drag the variable from the Namespace tab into the empty ChangeTo parameter slot.

Copy this node twice and attach it to the end of the other two loops.

If done correctly, every time you click on a shape the name in the label should change to the name of the shape clicked.

Node

Local Variables

Local variables are created right in the flow of execution. They can be called anywhere in the flow after the variable has been declared and initialized.

Saving a value to a variable

The simplest way to make a local variable is to use the SaveTo node.

First, you must declare the type and name of the variable.

Right-click on the storyboarder and type string(type) hit enter.

Click the bottom handler and select create variable.

Rename the variable in the properties tab and connect this node to the line of execution.

Now we will initilize the variable by giving it a value. Populate the constant value that you wish to save to a variable by right-clicking the storyboarder and typing the type name of the constant in the search bar.

In this example we are making a string variable so we search for string(constant) and press enter.

Now that we have the string(constant), we give it a value by typing its text in the value property.

Dereference the string(constant) by clicking the bottom handle and select the SaveTo node.

Right-click in SaveTo's empty parameter slot and choose variables > New.

An unnamed variable will populate in the SaveTo node click on it and change its name in the properties tab.

Now we have our welcome message saved to the WelcomeText variable.

We can now use this variable to display this text in a messagebox for example.

Populate the MessageBox node in the storyboarder and ungraph the text property.

Delete the blank argument, right-click in the parameter slot and type the name of your string variable.

Now when you play the lesson the message will display the text from the WelcomeText variable.

Changing a variables value

To change the value of the variable we just made we can use the ChangeTo node.

Call the variable in the storyboarder and dereference it with at ChangeTo node.

Right-click in the parameter slot and choose string(constant), type its text in the value property.

Copy the messagebox after this ChangeTo node.

The first messagebox will show the "Welcome to Lesson One" text and the second messagebox will show the "Welcome to Lesson Two" because we changed the value of the WelcomeText variable.

Node