How to access properties from other scenes!

Godot Version

godot 4

Question

I’ve ran into this same problem several times and I’ve asked before but never really got an answer, HOW DO I GET ACCESS TO A VARIABLE THAT IS IN THE SCRIPT OF A NODE IN ONE SCENE, SO THAT I CAN USE IT AND CHANGE IT IN THE SCRIPT OF ANOTHER NODE IN A COMPLETELY DIFFERENT SCENE!?!? (WITHOUT INSTANCING)

What’s your specific use case? There are several methods, depending on what you want to do.

For example, if Node A is an Area and Node B is a CharacterBody, you get B via the _on_body_enter signal and then you can manipulate it directly.

Or if you want to display the health of the player (Node A) in a Label (Node B), you could query a static reference in some kind of Main script, or you could use a signal that you put in an AutoLoad which emits as soon as the player’s health changes.

But IMHO it really would make more sense if you have a concrete description of what you’re trying to do.

2 Likes

sure thing:
Basically what I’m doing is I have a main scene, in it there is a spawner node that spawns a bunch of rocks, each one has durability (how many hits it takes to destroy a rock) initially set to 4. Now I have a timer as well in the main scene. Once the timer finishes it changes the scene to a shop scene. In the shop scene I have a button that when pressed SHOULD change the rock durability to one less each time it is pressed (from 4 to 3, from 3 to 2 etc) however, I have no idea how to do this, I thought by just creating an instance of the rock:

var ROCKSCRIPT = preload("res://scenes/rock.tscn")
var rockitem = ROCKSCRIPT.instantiate()

func _on_pressed() -> void:
	rockitem.durability -= 1
	print(rockitem.durability)

would work, but it doesn’t, because when I return to the main scene the rocks still take 4 hits to destroy, any ideas?

I guess the script you’ve shown is the script of the shop scene? How do you change to the shop scene? My bet is that you can give the shop scene the rockitem directly, but I’d have to see some code. My guess right now is that you instantiate two rocks.