- Home /
Is SendMessage immediate or not?
Just want to clarify something before I go about implementing it in code. Anyway, I'm setting up carriers that launch fighters at the player, then recall them if the player gets too far away. When a fighter gets recalled by a carrier, that carrier reserves space for the fighter.
However, if the fighter gets shot down while returning, I want the carrier to clear the reserved space. The current plan is for the HP-handling script on the fighter to send a message to the carrier-recall-handling script on the fighter when lethal damage is dealt, and then have the carrier-recall-handling script notify the carrier - all before the HP-handling script finishes up the method that sent the message, destroying the fighter.
As long as the sent message is handled immediately (before the method in the HP-handling script runs the rest of its code and destroys the fighter), there is no problem. However, if there's a possibility that the sent message might get handled later, then it would wind up getting sent to a GameObject that no longer exists, and the carrier would not be notified.
Hence, the question: is SendMessage always processed completely, on the same line where it is called?
Not sure I know the answer to your question (apologies). I believe so - under the hood, I think it's just using reflection to identify methods and then calling them within the same thread - but I'm not certain.
However, I have to ask: why do you want to use Send$$anonymous$$essage? Send$$anonymous$$essage is actually quite slow, and typically not something you want to use for regular communications within your game.
Is there a technical reason you need to use it, like some of your scripts are written in C# and others in JS?
The basic idea is that I want a function in one script attached to a GameObject to call a function in another script attached to that GameObject. Both scripts use Javascript. If there's a better way to achieve that goal than Send$$anonymous$$essage, I'd be happy to hear it :)
There are a couple ways to do it. The simplest is probably to use GetComponent (see the script reference for more info).
If you're going to be calling methods between the two scripts often, just call GetComponent when the object is instantiated and save the result.
Once you get a reference to the script using GetComponent, it's just targetScript.methodToCall (assu$$anonymous$$g that methodToCall is public and not static), and this will always execute immediately.
@Julien.Lynge It's not only the simplest, it's also the fastest and only way it should be done. Send$$anonymous$$essage
in general is completely evil and should never get used. There are much better ways of doing the same thing.
It's like the difference between looking up an address or driving randomly until you reach your destination.
That's not really true. Send$$anonymous$$essage can be useful for generic functions and modular design where you don't necessarily know the type of component(s) that are attached. I would be suspicious of any statements about "X is evil" and "there is only one way" because those are rarely true. It does use strings so it's not the first solution I think of, but I've used it a few times. Also, "slow" is highly relative and basically a non-issue for any actual usage.
Answer by Benproductions1 · Mar 25, 2014 at 09:34 AM
I don't know how it would work otherwise, but here I go:
SendMessage
uses reflection to find any methods on all components that are attached to the object SendMessage
was called on. SendMessage
goes not tie into the coroutine system, it's pure and simple .NET reflection. It's implementation looks roughly like this (pseudocode):
function SendMessage(name:String, argument:object) {
//Get all components
var components = this.GetComponents(Component);
//Loop through all components
for (var component in components) {
//Get the type of the component (reflection)
var type = component.GetType();
//Get the method named <name> of the type
var methodInfo = type.GetMethod(name);
//Invoke the method on the components with the argument
methodInfo.Invoke(component, [argument]);
}
}
As is made obvious by the code, SendMessage
does not "delay" anything, so like all other pieces of code, they execute immediately (Yes, all code runs immediately (in the same thread), there might just be more code hidden behind which you haven't realized)
You can really easily test this:
class Test extends MonoBehaviour {
function Awake() {
SendMessage("Test");
Debug.Log("Finish Test");
}
function Test() {
Debug.Log("Testing");
}
}
That's a couple lines of code, written in a couple seconds, which could have easily saved you the 5 minutes writing the question and the 10 hours of waiting for an answer ;)
But you doing this 5 $$anonymous$$utes writing save each one of us that read it 4 $$anonymous$$utes :P
Answer by Eric5h5 · Mar 25, 2014 at 10:20 AM
All code is always processed immediately, unless specifically stated otherwise in the docs (those few async functions that Unity has). Programming would never be able to work otherwise.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
LoadLevel Health Question. 1 Answer
Webplayer String length fails? 1 Answer
How to Do Simple Interactions? 1 Answer