About nodes within scenes and attached scripts.
Asked Answered
I

1

0

Suppose i have a sub-scene that contains a sprite, and i have 10 instances of that scene that i want to load at start-up, but i want the size of the sprite within each one to be a different size at start-up. If i attach a script to the parent node of that scene then i can write a function within that script that directly tells the sprite node what size to be based on its name in the tree of nodes and call that function in the _ready() function of that attached script. Something like:

func _ready():
   resize()
func resize():
   var mysprite = $Sprite2D #you can have a pepsi though
   var myspritename = 
   get_parent().get_name()
   if myspritename == "mysprite1"
      mysprite.scale = 1
   if myspritename == "mysprite2"
      mysprite.scale = 0.9
   if myspritename == "mysprite3"
      mysprite.scale = 0.8

etc.
This works, but it gives me red errors. It says "Node not found: "sceneparentnode/mysprite" (relative to "/root/mysubscenescript")" even though it does what i want.
If i write a function in my main scene's script that iterates through all the parent nodes of every instanced scene and calls the resize() function on each of those nodes it also works, but it is a lot more work and this is an extremely simplified illustration. What works without errors is something like this in my main scene script:

func _ready:
   callresizer()
func callresizer():
   var scenenode
   var x = how ever many + 1
   for i in range(1, x)
      scenenode = get_node(”topnode/nextnodes/mysceneparent" + str(i))
      scenenode.resize()#calls the function from the attached script.

Assuming i have named all the instanced scenes the same with different numbers. If i do this instead of putting the resize() in the _ready() of the sub-scene's attached script it does the same thing but no errors, but this gets much more complicated quickly. Do you know if the first way's errors are something i can just ignore, or if I'm doing this whole thing in a silly way?

Innovation answered 25/4, 2023 at 3:52 Comment(0)
M
0

Innovation or if I'm doing this whole thing in a silly way?

Quite silly, I'd say 🙂

Seriously though, you should go through some entry level tutorials that cover the basics of scene tree and node manipulation via scripting.
This is a rather simple thing that can be done in a few lines of code but I can't give you a specific example without seeing your exact scene structure.

Mckibben answered 25/4, 2023 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.