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.
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:
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.
Click to download the sample asset used in demonstration.
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.
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.
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
Some nodes such as WaitForAnyClick will return values of multiple types. WaitForAnyClick will return the object that is clicked whether it is a scenemodel, 3dLabel, inventory grid, etc. If we want to then use this output as an input to another node we need to be sure of the type we are passing in we use the Cast node to do this.
The Cast Node will forcibly change the type of a variable to the type we specify. In the following example we will create a node sequence that will change any scenemodel clicked to a random color. You will need to be in Programmer mode.
Call a WaitForAnyClick node and dereference the Cast node from it.
Fill the parameter with the Scenemodel(type). We must cast the output of WaitForAnyClick to a Scenemodel because we will be using a setColor node which can only be used on Scenemodel objects. Lets save this to a variable using the SaveTo node so we can reference this ouput later.
If the object that is returned from WaitForAnyClick is not a scenemodel object, for example the user clicked on a 3D label, then casting to a scenemodel will turn the value to null (meaning not a scenemodel). We do not want to use the setColor node on a null value so we will need to use an if node to check for this condition.
Right-click on the storyboarder and create an "Is Not Equal" node. Add the variable we just created as the first parameter and "null" as the second.
Dereference and if node from the "Is Not Equal". We will add the set color node attached to the top parameter handle of the if because we want it to only change the color of a scenemodel if a scenemodel was actually clicked and not any other type of object.
Dereference the set color node from the model variable. Graph the Color property to the node by pressing the 'x' next to it in the properties tab.
Create the random color generator by calling "Random" hit tab then select "ColorHSV". Drag this node into the color property of the "SetColor" node.
Add a loop at the begining of the sequence to continously repeat it.