Getting scene from array of Node2D

Godot Version

4.2.2

Question

Hi,
I’m a total beginner in Godot and am trying to create my first game. I have some Unity experience, and might have gotten stuck in the Unity way of doing things.

Currently working on a very basic Mole Bash game.


This is my current layout. I have a main scene in which I have a five mole holes which are a scene I’ve dragged into the main scene.
In my main script I have created and @export an array of Node2D (as the holes gets created as Node2D when dragged into the scene), that array I’ve populated with the holes I’ve dragged into the scene. I have a timer and each time the timer reaches 0, I want to select a random hole and spawn a mole in that hole.
The mole hole has a packed scene which is the mole (characterbody2d + sprite + collision shape) which I want to animate up through the hole.

My inital problem however I getting a element from my array of mole holes, so that I can access child nodes…
I get the following error when trying to get a random element from my array:

Any help would be most welcome :slight_smile:

In your scene tree are your nodes, and you access their properties directly, thus you randomly get a node from a node array by pick_random(), why don’t we just use that node we just got?

That instance_from_id() is not needed.

By the way, I just came from Unity, and currently struggling in writing C++ in Godot.

1 Like

Thus you can type:

var spawn_point = spawns.pick_random()

# your spawns are an array of "Node2D", so "spawn_point" has a position
var spawn_position = spawn_point.position
1 Like

When working with assigning in GDScript, it’s recommended to use := instead of =. Here’s an example to illustrate the difference:

var very_dynamic = 123
very_dynamic = "Hello Whatever"
very_dynamic = Node2D.new()

var very_static := 321
very_static = 24
very_static = "Type Mismatched" # You will get an error if you do this!

As you can see, when you use := in the initial assignment, the variable is restricted to the type of its initial value. In the example above, we initially assign an int value, so we cannot then assign a string.

In C#, dynamic types are known to be expensive due to additional calculations for dynamic implementation. The reason for this will be really straightforward, I think.

It’s important to note that := is only used for the initial assignment, and you can specify the variable to a specific type like this:

var my_node: Node = a_very_dynamic_function_without_specific_type()

In the above case, this method for specifying type will be quite useful.

1 Like

This was a nice and easy solution to get the spawn point. The reason I wanted to load the scene however was because one of the elements in that hole scene is a Marker2D which I wanted to use for spawn point, but I guess I can just offset the point manually and the instantiate a mole directly instead of have the mole as a resource on the hole as it is now:

Thanks for your help :slight_smile:

I looked at the image, and the spawn_point we’ve gotten in the code seems to be your instantiated mole hole scene.

We can treat this spawn_point as if it were the mole hole scene itself because essentially that’s what it is. If you have some nice methods like pop_mole() in your code that is attached to your mole hole scene, you can then use the spawn_point like this:

var spawn_point = spawns.pick_random()

# Your spawns are an array of "Node2D" that actually is your "MoleHole"
# So "spawn_point" is a "Node2D" variable that holds "MoleHole" (inheritance, right?)
spawn_position.pop_mole()
1 Like