#How to make a script unique to multiple instances of a node?

1 messages · Page 1 of 1 (latest)

paper whale
#

I have one script connected to a node and I duplicated the node (It's a textureRect). For some odd reason, the functions in the script executes for all of the instances. How can I make all the functions in the script, or the script itself, only execute for one node only and not the duplicated nodes?

marble meteor
#

this has nothing to do with the script not being unique; when you can a function it will only run in the object (ie. the specific node) that you called it on, regardless of the script being unique or shared

#

how are you calling that function?

paper whale
#

I’m calling one of the functions through connecting a signal, and the rest of the functions are in _ready and _input

marble meteor
#

_ready() and _input() runs on all nodes, _ready() on each node when it enters the scene tree for the first time (basically when it "spawns"), and _input() runs on all nodes every time an input event is received by the engine (note: all nodes with that function always receive every input, it's not like _gui_input which filters clicks inside of the node)

#

signal connections are also from a specific object to another specific object

#

a signal of one object can be connected to multiple target objects, but these connections still need to be made individually somehow

#

if you expected the texture rects to handle clicks inside of them you probably want to change _input to _gui_input

paper whale
#

Should I use _enter_tree()?

shut patrol
#

If you want something to run in only one of those nodes, you're going to need either an outside node that selects one of them and then calls a function on just that one, or some value that differs between them, like for example an id that you set with an exported variable and you only run a function if the id is equal to, say, 0

#

Using _ready() or _enter_tree() or_input() with no further checks will make the code run on every node

#

It would help to know exactly what the functions in question are

#

The approach will depend on that a bit

marble meteor
#

and yes connecting signals through code is fine

paper whale
#

I figured it out by adding a var to the _Ready() function

#

This actually solved my other issue actually for a different project. The question is, is process the same thing too?

marble meteor
#

yes, it runs on each node with that function as well

#

things like that aren't per script, they're per node