Skip to content

Commit 0efdd6d

Browse files
committed
Update GDScript for Godot 4.x
1 parent ff6db30 commit 0efdd6d

File tree

6 files changed

+423
-172
lines changed

6 files changed

+423
-172
lines changed

components/prism-gdscript.js

Lines changed: 253 additions & 22 deletions
Large diffs are not rendered by default.

examples/prism-gdscript.html

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
11
<h2>Full example</h2>
2-
<pre><code>extends BaseClass
3-
class_name MyClass, "res://path/to/optional/icon.svg"
4-
5-
# Member Variables
6-
7-
var a = 5
8-
var s = "Hello"
9-
var arr = [1, 2, 3]
10-
var dict = {"key": "value", 2:3}
11-
var typed_var: int
12-
var inferred_type := "String"
13-
14-
# Constants
15-
16-
const ANSWER = 42
17-
const THE_NAME = "Charly"
18-
19-
# Enums
20-
21-
enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
22-
enum Named {THING_1, THING_2, ANOTHER_THING = -1}
23-
24-
# Built-in Vector Types
25-
26-
var v2 = Vector2(1, 2)
27-
var v3 = Vector3(1, 2, 3)
28-
29-
# Function
30-
31-
func some_function(param1, param2):
32-
var local_var = 5
33-
34-
if param1 &lt; local_var:
35-
print(param1)
36-
elif param2 > 5:
37-
print(param2)
38-
else:
39-
print("Fail!")
40-
41-
for i in range(20):
42-
print(i)
43-
44-
while param2 != 0:
45-
param2 -= 1
46-
47-
var local_var2 = param1 + 3
48-
return local_var2
49-
50-
# Functions override functions with the same name on the base/parent class.
51-
# If you still want to call them, use '.' (like 'super' in other languages).
52-
53-
func something(p1, p2):
54-
.something(p1, p2)
55-
56-
# Inner Class
57-
58-
class Something:
59-
var a = 10
60-
61-
# Constructor
62-
63-
func _init():
64-
print("Constructed!")
65-
var lv = Something.new()
66-
print(lv.a)</code></pre>
2+
<pre><code>@icon("res://my_class_icon.png")
3+
class_name MyClass
4+
extends Node3D
5+
## GDScript class example
6+
##
7+
## This class contains sample code for the GDScript language,
8+
## showcasing most of the language's highligthing possibilities.
9+
10+
signal my_signal
11+
signal another_signal(node: Node)
12+
13+
enum CustomAttribute {
14+
ZERO,
15+
ONE,
16+
TWO,
17+
FIVE = 5,
18+
}
19+
20+
const DEFAULT_LENGTH := 10
21+
22+
@export var editor_variable := 0
23+
@export_range(1, 5) var ranged_variable := 2
24+
25+
var attributes: Dictionary[String, CustomAttribute] = {}
26+
var node_path := ^"my/node/path"
27+
var string_name := &"string_name"
28+
29+
@onready var label: Label = $Path/To/Label
30+
@onready var button: Button = %Button
31+
32+
33+
func _ready() -> void:
34+
var args := OS.get_cmdline_args()
35+
if not args.is_empty():
36+
return
37+
elif "--headless" in args or editor_variable != 0:
38+
push_error("Cannot initialize scene")
39+
return
40+
move_node(ranged_variable)
41+
var inner_object := InnerClass.new(CustomAttribute.ZERO)
42+
print("Initialized object with %s" % [inner_object.inner_var])
43+
44+
45+
func move_node(value: int) -> void:
46+
position = Vector3.ONE * value * randf()
47+
# NOTE: Alternative syntax "(%.2f/%+-5.1f/%.2f)" % [position.x, position.y, position.z].
48+
print("Moved to ({x}/{y}/{z} &lt;{v}>)".format({
49+
"x": "%.2f" % [position.x],
50+
"y": "%+-5.1f" % [position.y],
51+
"z": "%.2f" % [position.z],
52+
"v": value,
53+
}))
54+
55+
56+
class InnerClass extends RefCounted:
57+
var inner_var: Variant = null
58+
59+
func _init(init_var: Variant) -> void:
60+
inner_var = init_var</code></pre>

tests/languages/gdscript/class-name_feature.test

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ extends Bar
33

44
class InnerClass:
55

6-
export(int) var baz
7-
export(int, 0) var i
8-
96
return foo as Node
107

118
const FOO: int = 9
129
var bar: bool = true
1310

1411
func add(reference: Item, amount: int) -> Item:
1512

13+
Array[MyClass]
14+
Dictionary[FirstClass, SecondClass]
15+
1616
----------------------------------------------------
1717

1818
[
@@ -21,52 +21,51 @@ func add(reference: Item, amount: int) -> Item:
2121

2222
["keyword", "class"], ["class-name", "InnerClass"], ["punctuation", ":"],
2323

24-
["keyword", "export"],
25-
["punctuation", "("],
26-
["class-name", "int"],
27-
["punctuation", ")"],
28-
["keyword", "var"],
29-
" baz\r\n",
30-
31-
["keyword", "export"],
32-
["punctuation", "("],
33-
["class-name", "int"],
34-
["punctuation", ","],
35-
["number", "0"],
36-
["punctuation", ")"],
37-
["keyword", "var"],
38-
" i\r\n\r\n",
39-
40-
["keyword", "return"], " foo ", ["keyword", "as"], ["class-name", "Node"],
24+
["control-flow", "return"], " foo ", ["operator-word", "as"], ["builtin-object", "Node"],
4125

4226
["keyword", "const"],
4327
["constant", "FOO"],
4428
["punctuation", ":"],
45-
["class-name", "int"],
29+
["builtin-type", "int"],
4630
["operator", "="],
4731
["number", "9"],
4832

4933
["keyword", "var"],
5034
" bar",
5135
["punctuation", ":"],
52-
["class-name", "bool"],
36+
["builtin-type", "bool"],
5337
["operator", "="],
54-
["boolean", "true"],
38+
["builtin-pseudo", "true"],
5539

5640
["keyword", "func"],
57-
["function", "add"],
41+
["function-definition", "add"],
5842
["punctuation", "("],
5943
"reference",
6044
["punctuation", ":"],
6145
["class-name", "Item"],
6246
["punctuation", ","],
6347
" amount",
6448
["punctuation", ":"],
65-
["class-name", "int"],
49+
["builtin-type", "int"],
6650
["punctuation", ")"],
6751
["operator", "->"],
6852
["class-name", "Item"],
69-
["punctuation", ":"]
53+
["punctuation", ":"],
54+
55+
["typed-array", [
56+
["builtin-type", "Array"],
57+
["punctuation", "["],
58+
["class-name", "MyClass"],
59+
["punctuation", "]"]
60+
]],
61+
["typed-dict", [
62+
["builtin-type", "Dictionary"],
63+
["punctuation", "["],
64+
["class-name", "FirstClass"],
65+
["punctuation", ","],
66+
["class-name", "SecondClass"],
67+
["punctuation", "]"]
68+
]]
7069
]
7170

7271
----------------------------------------------------
Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
and
22
as
33
assert
4+
await
45
break
56
breakpoint
67
class
@@ -10,82 +11,75 @@ continue
1011
elif
1112
else
1213
enum
13-
export
1414
extends
1515
for
1616
func
17+
get
1718
if
1819
in
1920
is
20-
master
21-
mastersync
2221
match
22+
namespace
2323
not
2424
null
25-
onready
2625
or
2726
pass
2827
preload
29-
puppet
30-
puppetsync
31-
remote
32-
remotesync
3328
return
3429
self
35-
setget
30+
set
3631
signal
3732
static
38-
tool
33+
super
34+
trait
3935
var
36+
when
4037
while
4138
yield
4239

4340
----------------------------------------------------
4441

4542
[
46-
["keyword", "and"],
47-
["keyword", "as"],
48-
["keyword", "assert"],
49-
["keyword", "break"],
43+
["operator-word", "and"],
44+
["operator-word", "as"],
45+
["builtin", "assert"],
46+
["keyword", "await"],
47+
["control-flow", "break"],
5048
["keyword", "breakpoint"],
5149
["keyword", "class"],
5250
["keyword", "class_name"],
5351
["keyword", "const"],
54-
["keyword", "continue"],
55-
["keyword", "elif"],
56-
["keyword", "else"],
52+
["control-flow", "continue"],
53+
["control-flow", "elif"],
54+
["control-flow", "else"],
5755
["keyword", "enum"],
58-
["keyword", "export"],
5956
["keyword", "extends"],
60-
["keyword", "for"],
57+
["control-flow", "for"],
6158
["keyword", "func"],
62-
["keyword", "if"],
63-
["keyword", "in"],
64-
["keyword", "is"],
65-
["keyword", "master"],
66-
["keyword", "mastersync"],
67-
["keyword", "match"],
68-
["keyword", "not"],
69-
["keyword", "null"],
70-
["keyword", "onready"],
71-
["keyword", "or"],
72-
["keyword", "pass"],
73-
["keyword", "preload"],
74-
["keyword", "puppet"],
75-
["keyword", "puppetsync"],
76-
["keyword", "remote"],
77-
["keyword", "remotesync"],
78-
["keyword", "return"],
59+
["setget", "get"],
60+
["control-flow", "if"],
61+
["operator-word", "in"],
62+
["operator-word", "is"],
63+
["control-flow", "match"],
64+
["keyword", "namespace"],
65+
["operator-word", "not"],
66+
["builtin-pseudo", "null"],
67+
["operator-word", "or"],
68+
["control-flow", "pass"],
69+
["builtin", "preload"],
70+
["control-flow", "return"],
7971
["keyword", "self"],
80-
["keyword", "setget"],
72+
["setget", "set"],
8173
["keyword", "signal"],
8274
["keyword", "static"],
83-
["keyword", "tool"],
75+
["keyword", "super"],
76+
["keyword", "trait"],
8477
["keyword", "var"],
85-
["keyword", "while"],
78+
["control-flow", "when"],
79+
["control-flow", "while"],
8680
["keyword", "yield"]
8781
]
8882

8983
----------------------------------------------------
9084

91-
Checks for keywords.
85+
Checks for keywords and control-flow keywords.

tests/languages/gdscript/number_feature.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@
99

1010
1_000_000_000_000
1111
0xBAD_FACE
12-
0b_0010_0010_0100_0100
12+
0b0010_0010_0100_0100
1313

1414
INF NAN PI TAU
1515

1616
----------------------------------------------------
1717

1818
[
1919
["number", "123"],
20-
["operator", "-"], ["number", "123"],
20+
["number", "-123"],
2121
["number", "123.456"],
2222
["number", ".5"],
2323
["number", "1.2e-34"],
2424

25-
["number", "0xBadFace"],
26-
["number", "0b01010101"],
25+
["number-hex", "0xBadFace"],
26+
["number-bin", "0b01010101"],
2727

2828
["number", "1_000_000_000_000"],
29-
["number", "0xBAD_FACE"],
30-
["number", "0b_0010_0010_0100_0100"],
29+
["number-hex", "0xBAD_FACE"],
30+
["number-bin", "0b0010_0010_0100_0100"],
3131

32-
["number", "INF"], ["number", "NAN"], ["number", "PI"], ["number", "TAU"]
32+
["builtin-number", "INF"], ["builtin-number", "NAN"], ["builtin-number", "PI"], ["builtin-number", "TAU"]
3333
]
3434

3535
----------------------------------------------------

0 commit comments

Comments
 (0)