#Is this the correct way to instantiate and edit a node?

44 messages · Page 1 of 1 (latest)

covert torrent
#
extends Node2D

@export var wall_scene : PackedScene

# Called when the node enters the scene tree for the first time.
func _ready():
    # Get the size of the window
    var window_size = get_viewport_rect().size

    var top_left = Vector2(0,0)
    var top_right = Vector2(window_size.x, 0)
    var bottom_left = Vector2(0,window_size.y)
    var bottom_right = Vector2(window_size.x,window_size.y)

    var boarder_sizes = [
        [top_left, top_right],
        [top_right, bottom_right],
        [bottom_right, bottom_left],
        [bottom_left, top_left]
    ]

    pass

    #var boarder = []
    # Create 4 StaticBody2D nodes, one for each leangh/width
    for b in boarder_sizes:
        
        var wall = wall_scene.instantiate()
        add_child(wall)
        var foo = wall.get_tree_string()
        wall.get_node('CollisionShape2D').shape.a = b[0]
        wall.get_node('CollisionShape2D').shape.a = b[1]
        pass
    pass
torn zealot
#

That code will work fine but there is no need to have the two pass commands.

covert torrent
#

It doesn't seem to work though

#

I have a 'ball' that should be colliding with the CollisionShape2D nodes. But it doesn't seem that setting wall.get_node('CollisionShape2D').shape.a = b[0] works

#

The shape set is a 'SegmentShape2D' so it should have a and b properties

torn zealot
#

I was replying to your original unedited post. I'm not sure about the CollisionShape2D issue.

covert torrent
#

Hmmm ok, I guess what I mean is. I can Instantiate the scene with the StaticBody2D, but when try to access and change properties of the child node CollisionShape2D, they don't seem to take effect

#

That's the scene I'm trying to instantiate. Then add 4 StaticBody2D childs to my main scene

#

When I do it outside of a for loop, it works fine

#

But inside the for loop, it doesn't work

torn zealot
#

Should it be:

wall.get_node('CollisionShape2D').shape.a = boarder_sizes[b][0]
wall.get_node('CollisionShape2D').shape.b = boarder_sizes[b][1]
covert torrent
#

I don't think so

#

First loop of boarder_sizes, b should = [top_left, top_right]

torn zealot
#

If it's not this then I'm out of ideas:

for b in boarder_sizes.size():
        
        var wall = wall_scene.instantiate()
        add_child(wall)
        var foo = wall.get_tree_string()
        wall.get_node('CollisionShape2D').shape.a = boarder_sizes[b][0]
        wall.get_node('CollisionShape2D').shape.b = boarder_sizes[b][1]
covert torrent
#

Nope, no change

#

Actually.... I can only instantiate 1, if I add the other 3 none of them work

#

This works:

#

This doesn't:

torn zealot
#

Is the CollisionShape2D in your wall_scene set to be Local to Scene?

covert torrent
#

Where can I find that?

#

It doesn't show up in properties

torn zealot
#

Just checked and it actually has to be the shape itself that is set local to scene. Does your wall_scene have a default shape applied to it that you are replacing or is there none set in the original wall_scene?

covert torrent
#

It has a default shape applied

torn zealot
#

And is that Local to Scene?

covert torrent
#

Ohhh

#

I think I see

#

Thank you!

#

That is a very well hidden variable

#

I don't understand exactly what it did though

torn zealot
#

If a resource is not local to scene then every instantiated scene links to one copy of the resource, so if one scene changes the resource it effects all the scenes. If you make it local then there is a copy specifically for the scene and changing that does not effect the other scenes.

covert torrent
#

Ok that makes sense. And that caused none of the values set for shape to work?

torn zealot
#

I'm a bit confused why it was not working at all. I would have thought that after going through the for loop all four of the StaticBody2D's would have ended up having the same collision shape as the last one you set.

#

I take it you have tested and it is working now?

covert torrent
#

Yes it is working now

#

how do I solve it?

torn zealot
#

I'm confused, you said it is working but are asking how to solve it. Is your original problem not solved now?

covert torrent
#

Sorry, it is solved. I just want to mark the discord qustion as solved

torn zealot
#

Oh, I see. To be honest I have never started a question thread so I'm not sure!