Dagger
Dagger

Reputation: 369

How do I activate a Boolean from one scene using AnimationPlayer from another in Godot?

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

Answers (1)

Djkcool
Djkcool

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:

  1. Create another script called Data_transfer.
  2. Set that script as an autoloaded script in Project>Project Settings>AutoLoad.
  3. Create a variable in the new script: var is_watered: bool = false.
  4. Change the initial variable declaration in the Crop script to use the Data_transfer.is_watered var:
    @export var is_watered: bool = Data_transfer.is_watered
    This will allow you to continue to be able to use the @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.
  5. Change the rest of the Crop script to use the Data_transfer.is_watered variable instead of is_watered.
  6. Change the Player script to use Data_transfer.is_watered instead of is_watered.

Upvotes: 1

Related Questions