Introduction to variables, methods, and parameters
Use methods, variables, and parameters to store data, perform actions, and control the flow of your Logic scripts.
Read time 2 minutesLast updated 2 days ago
Variables
A variable is a container you can use to store information like numbers, text, colors, or references to objects to use in your Logic scripts. For example:- You operate a solar-plus-battery site.
- You create a variable named to store the battery’s current charge level.
BatteryStateOfCharge
- is a number that changes as the battery charges and discharges.
BatteryStateOfCharge
- You make a
BatteryStateOfCharge
type to hold decimal values for precise control.float
- When sensors report a new reading, you update . If it drops below a threshold, your Logic triggers an alert.
BatteryStateOfCharge
Methods
Use methods to group a set of actions together that perform a specific task. Methods can also help keep your Logic scripts modular and organized. For example:- You have a door in your scene that you want to open when the user clicks on it.
- You create a method called that contains the logic to open the door:
OpenDoor
- Animate the door object as it opens.
- Play a creaking sound.
- Then, whenever you want to open the door in your logic, you just call the method instead of rewriting the same logic over and over again.
OpenDoor
Return values
A method can return a value after it runs. This is useful when you want to get some information back from the method. For example, with theOpenDoor
true
Use methods in your logic
If you create a method with a return type other than Void, you can drag the method node from the right panel into the Logic Editor.Parameters
A parameter is a variable that you pass into a method when you use it. Parameters let you reuse the same method but with different inputs each time. For example:- You want to create a notification that shows the user different messages.
- You create a method called and give it a parameter called
ShowNotification
of typeMessage
.String
- You give the method the following logic:
- Display the text on the screen.
Message
- Animate the UI element to slide in and out.
- Play a notification sound.
- Display the
- Now, whenever you want to show a notification, you call the method and pass in the message you want to display:
ShowNotification
You have a new message.
An error occurred.