Export Variables
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 15 of 28.
Put @export in front of a variable and Godot's editor shows it in the Inspector. The Inspector is the panel that lists the properties of the node you pick. You can change the value there without editing code:
@export var speed = 200The number in the script is the default. A value set in the Inspector is saved in the scene, and the node gets it when the game starts. Two robots with one script can have two speeds.
This lesson's scene sets the robot's speed to 90, as the Inspector would. The Scene tab under the game does not list exported variables, so you will not see it there.
Challenge
EasyLet the scene set the robot's speed.
- At the top of
robot.gd, add an exported variablespeedwith a default of200. - In
_process(), move the robot right atspeedpixels a second.
Press Play: the scene sets speed to 90, so the robot moves 90 pixels a second. Then Check.
Try it yourself
extends Node2D
func _process(delta):
position.x += 200 * delta
All lessons in Nodes and Scenes
1Your First Godot Game
What Is Godot?Ready Runs OncePrint in GodotThe Scene TreeSet a PositionMove a Node Once3Values You Can Tune
Variables on a NodeConstantsExport VariablesVector2 MathWrap Around the ScreenYour Own FunctionsPractice on your own: Godot playground