I’m trying to get access to a variable (not static) that is apart of an object (rock) script, I’ve done this before using groups, so I made a new “rock” group and added the main node to that group. Now what I’m trying to do is to use that variable on another script which is on another node in another scene.
I obviously thought that @onready var rockscript = get_tree().get_first_node_in_group("rock") would work, but apparently not? Because when I try call that variable it says invalid access to property etc on type null instance??
That means you don’t have any node inside the scene tree that is from “rock” group. How you added this node on the “rock” group? Have you added the node inside the scene tree?
also if you’re wondering if I’ve added the “rock” node to the main scene tree where I am trying to call the variable on a different script, no, and I can’t because my game won’t work if I do.
But if you don’t add the node in the scene tree, you can’t expect to get him from groups, because the engine only knows the node is inside some group when the node is added to the scene tree. What is your objective with your code and what happens when you add the node in the scene?
Okay so I really just want to create instances of rocks into my main scene and be able to access a variable that is on the rock script within a node in my main scene that acts as the spawner. How would you propose I do this? I tried to access the variable via the instance but that just gave me a NIL error? And when I tried to use the get_tree.get_first_node_in etc ON the actual rock script and then tried to call that within my spawner script it also gave me an error saying that the property doesnt exist
The first problem i see here is the ROCK.rockscript.count, ROCK in your code is a PackedScene (a preload of your scene) so you cant access anything from there, this code is to limit the max amounts of rocks spawned at time? Because in this case you can limit that with:
Also you can explain why you need the count value to spawn the rocks? Because at the start of the scene you’ll have no rock in scene, and with no rock in scene you can’t access the variable count because you don’t have any rock. Also, when you start to spawn the rocks, any rock count will be valid? Because with two or more rocks you needs to choice which rock you want to check the count value
Yea sorry, I really suck with naming stuff, basically the rock “count” is not really to count how many rocks in order to limit them, I already got that far, the problem is that I want to respawn a random number of rocks everytime the player has destroyed all the rocks on screen, so count is not to count how many rocks there are but actually to count how many rocks have been destroyed, so that when the number of rocks destroyed matches the number of rocks that were spawned we can then spawn some more
Okay, before i write the code to help you i just need to understand one more thing, the count in this case is the “durability” of the rock, like you need to hit the rock 4 times to break them, right?
yes indeed, but I’m pretty sure we could add this pretty easy by just multiplying the spawn_count by 4: so if count (durability of rocks) == spawn_count * 4: etc