Send message to all scripts in a GO

#1

Hi, are there anyway to send messages to all the scripts in a gameobject?
I dont know where I read it, that if you send a message to a gameObject without specify a component, the message is sent to all components of the GO.

In a GO I have multiple gameobjects each one with a script, I dont know how to send a mesaage to all of them.

If this isnt possible, would this be an interesting feature?

0 Likes

#2

Yes, that is correct:

msg.post("bob", "foobar") -- post message "foobar" to all components on go "bob"
msg.post(".", "foobar" -- post message "foobar" to all components on the current go

There is no function to do this. But don’t you know which game objects you have? How were they added as children? Run-time or in the editor? Either way you know right?

0 Likes

#3

Yes , I created them in editof, but sending 100 or more messages, and each one with different name its too time consuming… I thinkg this is very neccesary

0 Likes

#4

Ok, so you have a main game object and it has 100 child objects? Some ideas:

  • How are they named? Do they have a predictable naming scheme? Foobar1, Foobar2, Foobar3 etc? Then use a for loop to msg.post() to each of them.
  • Do they all have a script attached? If so, then this script could msg.post() to the parent and “register”. The parent game object would then build a list and could use this list to post messages to the children.
  • Do they have a collision object attached? If so the parent could have a trigger object that detects the children and collects them into a list.
2 Likes

#5

nice ideas, I like the aproach of the second point. One more question.

How can I detect what objects are being rendered by the camera? when the camera moves, I send all objects that are part of the map, the position of the camera, and they use for moving sprites, anyway… If I have a big map, it has to much cost , send the information and updating the positon for example, in every GO, then, my aproach is only run the logic in the objects that are rendering in that moment by the camera.

Any way to do this?

thanks.

0 Likes

#6

If this is 2d, then the simplest solution would be to check if GO’s current position is in the range of a rectangle that your camera is now showing - you know its borders, because you have the camera position and its range.
In general this problem is called “culling”, you can search for it, there’s a lot of solutions. There is also this thread on the forum :wink:

Oh, there is also this thread, but I don’t know what is the progress?

0 Likes