Hey guys. So I’m trying to come up with an efficient system to execute basic “events” on my character. It seems like the “SendMessage” method is the easiest way to direct an object to many different functions from one central script.
My question is, I’ve seen that “SendMessage” is a very inefficient and resource intensive way to accomplish things. This is obviously more of a concern on the iPhone. Is there any other way to accomplish the same type of thing using more efficient scripting methods?
Sorry if the question is vague, I guess I just want to know if I should stay away from “SendMessage” on the iPhone.
Hmm, you could get an instance to the script on the character from your controlling script and execute the functions directly. Is that what you’re looking for?
Example:
ControllerScript.js
...
character.SendMessage("someFunction");
...
Instead and approx. 100x faster:
ControllerScript.js
// assign this via the inspector or GameObject.Find(...).GetComponent
var characterScript : CharacterScript;
...
characterScript.SomeFunction();
This is an important issue. If you want to call the function and do it now without delay for the next frame then direct function call as Martin mentioned is the best idea. But don’t call GetComponent() every frame.
Moreover, SendMessage() function call will delay for next frame execution. I used it in my Game Message passing system. Though it’s not frequent.
Hey Martin, thanks for the info! It’s actually YOUR code that I’m trying to mod right now. I am trying to simplify it a bit so I can learn from it, but also because in my project I only need to control one “unit” (so no need for a Unit Manager) which is always the player.
Eventually, though, I will need some kind of central controller to determine WHAT it is that the player has clicked. For example, if they click on a KEY nearby, I want that key to go into their inventory, but if they click on a spot on the TERRAIN, I want them to move to that location.
Most of the setups in the Unit Manager use send-message, and after seeing a few posts about how it might not be the fastest (internally) way to do things, I was concerned it might not be the right approach for the iPhone.
Is that a correct assumption, or does that not apply in this case?
This is SO off-topic but I must give props for the excellent avatar. Is that a self-portrait via Wind Waker style? Love it. Um… and don’t hijack threads, kids.
Wow man - thank you SO much for the compliment! Yes it is a self-portrait and the wind-waker style was something that took me FOR-EVER to get down. Even though the style seems simplistic, I basically ended up reverse engineering the windwaker proportions and style, then tried replicate the inking, coloring and paper texture.
Wow man - thank you SO much for the compliment! Yes it is a self-portrait and the wind-waker style was something that took me FOR-EVER to get down. Even though the style seems simplistic, I basically ended up reverse engineering the windwaker proportions and style, then tried replicate the inking, coloring and paper texture.
I’m all about performance, but there are times that I use send message. Just depends on when and how you are using it.
As long as you are using it for events, rather than say, manipulating the transform of a game object in an update method, I wouldn’t sweat it.
It’s not as if you will notice a lockup when sendmessage is invoked.
I think sendmessage is good for writing reusable code, since you don’t need to verify what you are sending a message. Such as a projectile can just do collider.gameObject.SendMesage(“ImHit”,… and you don’t have to worry about it. If it hits something that responds to that, the magic will happen. If it hits something like a wall, it’s going to just be ignored.
I think from other threads you are talking about a zack and wiki style mouse-click controlled adventure game, right?
In that case, I’d just use sendMessage(“playerTouched”) to whatever you click on. This isn’t an RTS where someone is going to be clicking like mad. If your raycast is hitting the environment, though, you do something else, like extrapolate out the point where it hit and figure out how to move there.
Off-topic again: Just played through Wind Waker again with my 3.5 year-old daughter (watching) and she loved it. It’s probably my favorite Zelda adventure as the story is the most cohesive and the art-style makes it so much more alive than the others.
Couldn’t agree more about Wind Waker. Really the only Zelda that I’ve played all the way through and it was precisely because it was such a whimsical, immersive and cohesive experience.
And yes, this is exactly for a zack and wiki style game. Oddly, although its gameplay is derivative, the implementation is quite unique. It is obviously a ‘point-and-click’ puzzle adventure game, but it uses 3D characters AND backgrounds.
I’ve seen plenty of games w/ 3D characters and static 2D backgrounds, or 2.5D (grim fandango) or games that are full 3D but you control the player directly with WASD style controls, but I can’t find anything other than Z&W that does full 3D point and click (with moving cameras, etc)
Also, Z&W’s pathfinding seems to be quite elaborate. You can generally click on a part of the world quite far away and Zack will find his way there, and stop EXACTLY where you clicked. Not the “nearest node” but exactly at the spot in 3D space.
This is what I’m trying to figure out how to do, but I’d be happy to just make a simple avoidance system that works efficiently on the iPhone.
Funny, after I posted this, I thought the same thing but I think it might not be that simple because CLOSE clicks work with great accuracy too.
The game is very good about getting to point A to B, even when you click on point B and there happens to be a small (moveable) rock in between A and B.
So, they possibly (for item 4 in your list) they are doing some kind of constant “feeler” style raycasting around to get to the final point without any problems. I can’t imagine that they would use this “feeler” method to get EVERYWHERE though, that wouldn’t make sense right?
BTW - here is a good example of the movement in action:
If the click-point is closer than the nearest node and there is nothing blocking… then the character just moves to it. Otherwise it does the node/pathfind thing. Right?