You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-15 13:51:40 +00:00
GDScript: Fix some bugs with static variables and functions
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# GH-77098 p.4
|
||||
|
||||
@static_unload
|
||||
|
||||
class A:
|
||||
class InnerClass:
|
||||
pass
|
||||
|
||||
enum NamedEnum { VALUE = 111 }
|
||||
enum { UNNAMED_ENUM_VALUE = 222 }
|
||||
const CONSTANT = 333
|
||||
static var static_var := 1
|
||||
|
||||
static func static_func() -> int:
|
||||
return 444
|
||||
|
||||
class B extends A:
|
||||
func test_self():
|
||||
print(self.InnerClass is GDScript)
|
||||
print(self.NamedEnum)
|
||||
print(self.NamedEnum.VALUE)
|
||||
print(self.UNNAMED_ENUM_VALUE)
|
||||
print(self.CONSTANT)
|
||||
@warning_ignore("static_called_on_instance")
|
||||
print(self.static_func())
|
||||
|
||||
prints("test_self before:", self.static_var)
|
||||
self.static_var = 2
|
||||
prints("test_self after:", self.static_var)
|
||||
|
||||
func test():
|
||||
var hard := B.new()
|
||||
hard.test_self()
|
||||
|
||||
print(hard.InnerClass is GDScript)
|
||||
print(hard.NamedEnum)
|
||||
print(hard.NamedEnum.VALUE)
|
||||
print(hard.UNNAMED_ENUM_VALUE)
|
||||
print(hard.CONSTANT)
|
||||
@warning_ignore("static_called_on_instance")
|
||||
print(hard.static_func())
|
||||
|
||||
prints("hard before:", hard.static_var)
|
||||
hard.static_var = 3
|
||||
prints("hard after:", hard.static_var)
|
||||
|
||||
var weak: Variant = B.new()
|
||||
print(weak.InnerClass is GDScript)
|
||||
print(weak.NamedEnum)
|
||||
print(weak.NamedEnum.VALUE)
|
||||
print(weak.UNNAMED_ENUM_VALUE)
|
||||
print(weak.CONSTANT)
|
||||
@warning_ignore("unsafe_method_access")
|
||||
print(weak.static_func())
|
||||
|
||||
prints("weak before:", weak.static_var)
|
||||
weak.static_var = 4
|
||||
prints("weak after:", weak.static_var)
|
||||
@@ -0,0 +1,25 @@
|
||||
GDTEST_OK
|
||||
true
|
||||
{ "VALUE": 111 }
|
||||
111
|
||||
222
|
||||
333
|
||||
444
|
||||
test_self before: 1
|
||||
test_self after: 2
|
||||
true
|
||||
{ "VALUE": 111 }
|
||||
111
|
||||
222
|
||||
333
|
||||
444
|
||||
hard before: 2
|
||||
hard after: 3
|
||||
true
|
||||
{ "VALUE": 111 }
|
||||
111
|
||||
222
|
||||
333
|
||||
444
|
||||
weak before: 3
|
||||
weak after: 4
|
||||
@@ -0,0 +1,17 @@
|
||||
# GH-41919
|
||||
|
||||
class_name TestStaticFuncAsCallable
|
||||
|
||||
class InnerClass:
|
||||
static func inner_my_func():
|
||||
print("inner_my_func")
|
||||
|
||||
static func my_func():
|
||||
print("my_func")
|
||||
|
||||
var a: Callable = TestStaticFuncAsCallable.my_func
|
||||
var b: Callable = InnerClass.inner_my_func
|
||||
|
||||
func test():
|
||||
a.call()
|
||||
b.call()
|
||||
@@ -0,0 +1,3 @@
|
||||
GDTEST_OK
|
||||
my_func
|
||||
inner_my_func
|
||||
@@ -33,24 +33,24 @@ func test():
|
||||
prints("perm:", perm)
|
||||
prints("prop:", prop)
|
||||
|
||||
print("other.perm:", StaticVariablesOther.perm)
|
||||
print("other.prop:", StaticVariablesOther.prop)
|
||||
prints("other.perm:", StaticVariablesOther.perm)
|
||||
prints("other.prop:", StaticVariablesOther.prop)
|
||||
|
||||
StaticVariablesOther.perm = 2
|
||||
StaticVariablesOther.prop = "foo"
|
||||
|
||||
print("other.perm:", StaticVariablesOther.perm)
|
||||
print("other.prop:", StaticVariablesOther.prop)
|
||||
prints("other.perm:", StaticVariablesOther.perm)
|
||||
prints("other.prop:", StaticVariablesOther.prop)
|
||||
|
||||
@warning_ignore("unsafe_method_access")
|
||||
var path = get_script().get_path().get_base_dir()
|
||||
var other = load(path + "/static_variables_load.gd")
|
||||
var other = load(path + "/static_variables_load.gd")
|
||||
|
||||
print("load.perm:", other.perm)
|
||||
print("load.prop:", other.prop)
|
||||
prints("load.perm:", other.perm)
|
||||
prints("load.prop:", other.prop)
|
||||
|
||||
other.perm = 3
|
||||
other.prop = "bar"
|
||||
|
||||
print("load.perm:", other.perm)
|
||||
print("load.prop:", other.prop)
|
||||
prints("load.perm:", other.perm)
|
||||
prints("load.prop:", other.prop)
|
||||
|
||||
@@ -3,14 +3,14 @@ Inner._static_init inner
|
||||
InnerInner._static_init inner inner
|
||||
data: data
|
||||
perm: 0
|
||||
prop: prefix Hello! suffix
|
||||
prop: Hello! suffix
|
||||
perm: 1
|
||||
prop: prefix World! suffix
|
||||
other.perm:0
|
||||
other.prop:prefix Hello! suffix
|
||||
other.perm:2
|
||||
other.prop:prefix foo suffix
|
||||
load.perm:0
|
||||
load.prop:prefix Hello! suffix
|
||||
load.perm:3
|
||||
load.prop:prefix bar suffix
|
||||
other.perm: 0
|
||||
other.prop: Hello! suffix
|
||||
other.perm: 2
|
||||
other.prop: prefix foo suffix
|
||||
load.perm: 0
|
||||
load.prop: Hello! suffix
|
||||
load.perm: 3
|
||||
load.prop: prefix bar suffix
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
@static_unload
|
||||
|
||||
class A:
|
||||
static var x: int = 1
|
||||
|
||||
static var y: int = 42:
|
||||
set(_value):
|
||||
print("The setter is NOT called on initialization.") # GH-77098 p.1
|
||||
|
||||
static func _static_init() -> void:
|
||||
prints("A _static_init begin:", x)
|
||||
x = -1
|
||||
prints("A _static_init end:", x)
|
||||
|
||||
static func sf(p_x: int) -> void:
|
||||
x = p_x
|
||||
prints("sf:", x)
|
||||
|
||||
# GH-77331
|
||||
func f(p_x: int) -> void:
|
||||
x = p_x
|
||||
prints("f:", x)
|
||||
|
||||
class B extends A:
|
||||
static func _static_init() -> void:
|
||||
prints("B _static_init begin:", x)
|
||||
x = -2
|
||||
prints("B _static_init end:", x)
|
||||
|
||||
static func sg(p_x: int) -> void:
|
||||
x = p_x
|
||||
prints("sg:", x)
|
||||
|
||||
func g(p_x: int) -> void:
|
||||
x = p_x
|
||||
prints("g:", x)
|
||||
|
||||
func h(p_x: int) -> void:
|
||||
print("h: call f(%d)" % p_x)
|
||||
f(p_x)
|
||||
|
||||
func test():
|
||||
prints(A.x, B.x)
|
||||
A.x = 1 # GH-77098 p.2
|
||||
prints(A.x, B.x)
|
||||
B.x = 2
|
||||
prints(A.x, B.x)
|
||||
|
||||
A.sf(3)
|
||||
B.sf(4)
|
||||
B.sg(5)
|
||||
|
||||
var b := B.new()
|
||||
b.f(6)
|
||||
b.g(7)
|
||||
b.h(8)
|
||||
@@ -0,0 +1,15 @@
|
||||
GDTEST_OK
|
||||
A _static_init begin: 1
|
||||
A _static_init end: -1
|
||||
B _static_init begin: -1
|
||||
B _static_init end: -2
|
||||
-2 -2
|
||||
1 1
|
||||
2 2
|
||||
sf: 3
|
||||
sf: 4
|
||||
sg: 5
|
||||
f: 6
|
||||
g: 7
|
||||
h: call f(8)
|
||||
f: 8
|
||||
Reference in New Issue
Block a user