You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
1.8 KiB
109 lines
1.8 KiB
2 years ago
|
|
||
|
# Everything after "#" is a comment.
|
||
|
# A file is a class!
|
||
|
|
||
|
# (optional) class definition:
|
||
|
class_name MyClass
|
||
|
|
||
|
# Inheritance:
|
||
|
extends BaseClass
|
||
|
|
||
|
# Member variables.
|
||
|
var a = 5
|
||
|
var s = "Hello"
|
||
|
var arr = [1, 2, 3]
|
||
|
var dict = {"key": "value", 2: 3}
|
||
|
var other_dict = {key = "value", other_key = 2}
|
||
|
var typed_var: int
|
||
|
var inferred_type := "String"
|
||
|
|
||
|
|
||
|
var ss = '''
|
||
|
func invalid_single_quote():
|
||
|
pass
|
||
|
'''
|
||
|
|
||
|
var sd = """
|
||
|
func invalid_func_double_quote():
|
||
|
pass
|
||
|
"""
|
||
|
|
||
|
# Constants.
|
||
|
const ANSWER = 42
|
||
|
const THE_NAME = "Charly"
|
||
|
|
||
|
# Enums.
|
||
|
enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
|
||
|
enum Named {THING_1, THING_2, ANOTHER_THING = -1}
|
||
|
|
||
|
# Built-in vector types.
|
||
|
var v2 = Vector2(1, 2)
|
||
|
var v3 = Vector3(1, 2, 3)
|
||
|
|
||
|
|
||
|
# Functions.
|
||
|
func my_func(arg1 : int = sin(1), arg2 : String = "") -> void:
|
||
|
return
|
||
|
|
||
|
func some_function(param1, param2, param3):
|
||
|
if param1 < local_const:
|
||
|
print(param1)
|
||
|
elif param2 > 5:
|
||
|
print(param2)
|
||
|
else:
|
||
|
print("Fail!")
|
||
|
|
||
|
for i in range(20):
|
||
|
print(i)
|
||
|
|
||
|
while param2 != 0:
|
||
|
param2 -= 1
|
||
|
|
||
|
match param3:
|
||
|
3:
|
||
|
print("param3 is 3!")
|
||
|
_:
|
||
|
print("param3 is not 3!")
|
||
|
|
||
|
var local_var = param1 + 3
|
||
|
return local_var
|
||
|
|
||
|
|
||
|
# Functions override functions with the same name on the base/super class.
|
||
|
# If you still want to call them, use "super":
|
||
|
func something(p1, p2):
|
||
|
super(p1, p2)
|
||
|
|
||
|
|
||
|
# It's also possible to call another function in the super class:
|
||
|
func other_something(p1, p2):
|
||
|
super.something(p1, p2)
|
||
|
|
||
|
|
||
|
# Inner class
|
||
|
class Something:
|
||
|
var a = 10
|
||
|
|
||
|
func inner_function():
|
||
|
return
|
||
|
|
||
|
func my_func(arg1 : int = sin(1), arg2 : String = "") -> void:
|
||
|
return
|
||
|
|
||
|
|
||
|
# Inner class with inheritance
|
||
|
class fish extends Node2D:
|
||
|
|
||
|
func _init():
|
||
|
_randomize()
|
||
|
|
||
|
func _randomize():
|
||
|
randomize()
|
||
|
_size = rand_range(0.75,2.0)
|
||
|
|
||
|
# Constructor
|
||
|
func _init():
|
||
|
print("Constructed!")
|
||
|
var lv = Something.new()
|
||
|
print(lv.a)
|