Introduction to variables, methods, and parameters
Use methods, variables, and parameters to store data, perform actions, and control the flow of your Logic scripts.
읽는 시간 2분최근 업데이트: 24일 전
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
BatteryStateOfChargetype 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
For instructions on how to create a variable, refer to Create a variable.
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
For instructions on how to create a method, refer to Create a method.
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 the example, you might want to know when the door finishes opening. You can modify the method to return a true/false value, which returns when the door is open.
OpenDoortrueIn this case, the return type of the method is a Bool value. However, if you don't want to return any value, you can set the return type to Void. The method performs the action but doesn't return anything.
For more information about return types, refer to New Method window reference.
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
ShowNotificationof 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:
ShowNotificationYou have a new message.An error occurred.
This way, you can use the same method to show different messages without rewriting the logic each time.
You can create parameters when you create a method. For information about parameter properties, refer to Parameter details.