Godot: GD Script error when using get_tree().root.foo = bar

Created on 9 Aug 2019  路  4Comments  路  Source: godotengine/godot

Godot version:
Tested on nightly build and 3.1.1

OS/device including version:
GNU/Linux x64

Issue description:
The engine seems to only throw an error when using get_tree().root.foo = bar, I tried with another object and the script ran fine.
image

Steps to reproduce:
use get_tree().root.foo = bar in a script

Minimal reproduction project:
GDScriptBug.zip

bug gdscript

Most helpful comment

I think I commented this on another issue.

If you set the last value "property chain" like this

a.b.c.d = new_val_d

Godot will update each property in reverse. Once the d value is set for c the updated value for c will be set for b and then b for a. Like the code gets transformed to this:

var b = a.b
var c = b.c
c.d = new_val_d
b.c = c
a.b = b

So in your code Godot sets a property for root and tries to call get_tree().root = root but the latter is an invalid operation (only read access).

It's been a while though and I don't remember exactly where this behavior applies in GDScript. I think this covers the C++ code that is just exposed.

Calling a get-method will break the chain and is a valid workaround.

All 4 comments

Possibly because you are trying to access a parent node in _ready within the games main scene. The scenetrees root viewport may not have readied by then.

Possibly because you are trying to access a parent node in _ready within the games main scene. The scenetrees root viewport may not have readied by then.

Probably not since storing the root in a variable and using it works as shown in the code at the bottom and doing the same in _process also causes an error.

Calling get_root() method directly works:

get_tree().get_root().render_target_v_flip = true

When attempted with property, strangely the following hack works: :eyes:

((get_tree().root) as Viewport).render_target_v_flip = true

(this casts the root to viewport before accessing)

The following prints true, though:

print(get_tree().root is Viewport) # true

I think I commented this on another issue.

If you set the last value "property chain" like this

a.b.c.d = new_val_d

Godot will update each property in reverse. Once the d value is set for c the updated value for c will be set for b and then b for a. Like the code gets transformed to this:

var b = a.b
var c = b.c
c.d = new_val_d
b.c = c
a.b = b

So in your code Godot sets a property for root and tries to call get_tree().root = root but the latter is an invalid operation (only read access).

It's been a while though and I don't remember exactly where this behavior applies in GDScript. I think this covers the C++ code that is just exposed.

Calling a get-method will break the chain and is a valid workaround.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EdwardAngeles picture EdwardAngeles  路  3Comments

mefihl picture mefihl  路  3Comments

timoschwarzer picture timoschwarzer  路  3Comments

testman42 picture testman42  路  3Comments

RebelliousX picture RebelliousX  路  3Comments