how do we change (Godot 3) to Godot 4)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rodell2020

Godot 3:
var coins = 0
var rng = RandomNumberGenerator.new()

func _ready():
$Coins.text = String(coins)

func _on_coin_collected():
coins = coins + 1
_ready()

in Godot 4
the line $Coins.text = String(coins) is creating this error
Invaid set index ‘Text’ (on base: ‘null instance’)with value of type ‘string’.

if i works in Godot 3 it should work in Godot 4

if i works in Godot 3 it should work in Godot 4

If understand correctly, backwards compatibility isn’t the main goal in 4.0. That said, I can’t reproduce the issue in godot 4.0. I’m guessing the problem has to do with you calling get_node() on Coins before it is initialized. Or it is in a different place.

wetbadger | 2022-07-21 14:49

That’s correct. In fact, it’s not a goal at all. Godot 4 is intentionally breaking things to allow for features that couldn’t be implemented in the Godot 3.x code base, and to clean up the API.

There are several (many?) things in 3.x projects that will need to be converted to work with 4. Some are trivial name changes, and some are wholesale breaks with backwards compatibility.

stormreaver | 2022-10-03 17:17

:bust_in_silhouette: Reply From: ka0s420

Hi, yeah for some reason casting floats and ints to String doesn’t work.

You have to use str(coins)