Reputation: 369
I have three scenes: Main
, Player
, and Crop
.
My main scene has the tilesets and a few other child nodes linked (including Player and Crop).
My player scene has a Sprite2D and an AnimationPlayer
My crop scene has a Sprite2D.
Inside my Crop
scene I have a script that has one variable and one function.
@export var is_watered: bool = false
func _process(_delta):
if is_watered:
$Sprite2D.texture=ResourceLoader.load(image)
is_watered
is just a Boolean while the _process
function changes the image of the Crop to its next stage.
Inside my Player
scene I have an AnimationPlayer
that has an animation called water_down
. Also inside the Player scene there's a function called water()
.
func water():
is_watered = true
But because the Player and Crop nodes are different scenes it didn't recognize what is_watered
is. How can I change the Boolean inside of Crop
to true while using an AnimationPlayer from the Player
scene? I tried emitting a signal but that too only connects to nodes inside the same scene.
Upvotes: 0
Views: 191
Reputation: 51
You could have a separate script that is autoloaded and pass variables through that.
You would save the variable in the autoloaded script and then whenever you want to access it you would just call sciptName.variableName instead of just variableName.
For example, in your case:
Data_transfer
.var is_watered: bool = false
.Crop
script to use the Data_transfer.is_watered
var:@export var is_watered: bool = Data_transfer.is_watered
@export
part of the variable, but since changing it doesn't change the Data_transfer variable you will have to have something like: Data_transfer.is_watered = is_watered
when the script is loaded like in the _ready()
method. Otherwise, the value of the export won't be used.Crop
script to use the Data_transfer.is_watered
variable instead of is_watered
.Player
script to use Data_transfer.is_watered
instead of is_watered
.Upvotes: 1