It appears I'm writing this as you are also fixing this... This is the version of the code I took:
bloon.gd
extends Node3D
@export var resistances := []
@export var texturePath := "res://sprites/bloons/Red.png"
# Speed is in red bloons / second
@export var speed := 1
#For fortified leads
@export var health = 1
@export var children := []
# TODO
# add moving across map
func _ready():
var sprite = $Sprite
sprite.texture = load(texturePath)
pass
func _process(delta):
pass
Here you load the texture on _ready
:
sprite.texture = load(texturePath)
Now, modifying the value of texturePath
afterwards won't change the texture, because there is no reason for _ready
to run again.
There is a pattern that would work in this case, you need a setter, and an update function:
@export var texturePath := "res://sprites/bloons/Red.png":
set(mod_value):
if texturePath == mod_value:
return
texturePath = mod_value
if is_inside_tree():
update()
else
request_ready()
func _ready() -> void:
sprite = $Sprite
update()
func update() -> void:
sprite.texture = load(texturePath)
So, when you modify the texturePath
, either the Node3D
is or isn't in the scene tree.
If it is in the scene tree, then you can go ahead an load the texture and set it to the sprite
.
If it isn't then we request to run _ready
when it enters the scene tree again (with request_ready
) and in _ready
you also go ahead and load the texture and set it to the sprite
.
Since the "load the texture and set it to the sprite
" part is the same in both cases, we have it extracted to an update
method and call it from both places.