I tried this code in GDScript, in Godot 4:
export (int) var width; export (int) var height;
And at Metadata add width and height but these below don't show and have an error.
I don't know what the problem is.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
That syntax would work in Godot 3.x.
Let us break it down first. This is a variant variable called
# Godot 3.x or Godot 4.x var width
We can add an optional
; at the end. The
; separates multiple statements on the same line. Since we are not adding more statements to the line, this is the same thing as before:
# Godot 3.x or Godot 4.x var width;
Now, to export the variable (so it is available in the inspector), in Godot 3.x you did this:
# Godot 3.x export (int) var width;
Here the variable is being exported as an
int. However, the variable is still a variant. It could have values that are not
int if you set them from code. But it will show as an
int in the inspector.
We need to fix that. Because in Godot 4.x, you don't do that. In fact, we could have fixed that in Godot 3.x, it looked like this:
# Godot 3.x export var width:int
Now the variable is an
int, and we don't need to tell it to export it as an
int, because exporting an
int as an
int is the default (yes, there is another option: exporting at as an Enum).
Yes, Godot 3.x had typed variables. It is not a type hint.
And now, this is how that looks like in Godot 4.x:
# Godot 4.x @export var width:int
That is because
export is no longer a keyword, but an annotation instead. Annotations are a new language feature in GDScript 2.0 (the one that comes with Godot 4.x), and they all start with