Say you have
Using "new" Unity networking, you must have (as far as I know) a NetworkManager
somewhere. It will spawn the ridiculous "player" object.
The ridiculous "player" object must have a class which inherits from NetworkBehaviour
.
Most teams call that class something like "Comms" or "AbstractPlayer" or "Junction".
public class Comms : NetworkBehaviour { }
So, every command (like "Jump") has to be mediated by that "Comms" class.
For each command (like "Jump") you need a pair of functions in the Comms class:
public class Comms : NetworkBehaviour {
// for every command (say, "Jump") you need THIS PAIR of functions:
public void CommandJump() {
Debug.Log("this is comms - client, do a Jump");
CmdJump();
}
[Command]
void CmdJump() {
Debug.Log("this is comms - server. a Jump arrived");
// simply pass on that command to the correct script on a server...
s = Object.FindObjectOfType<Some Class On The Server>();
s.ClientJumped();
}
}
So!
With new Unity networking, in the client side, every time you want to
send to server Jump()
you in fact have to send it VIA the "comms" class on the abstract player.
public class Jumping: MonoBehavior...
// here in the client side ... we want to send "Jump"
Comms comms = Object.FindObjectOfType<Comms>();
comms.CommandClick1();
(Note - obviously, you would cache the "comms" there. It's just an example.)
Aside: extremely important: that is just a outline example.
In practice you must do this:
So!
the thing is this:
in "old" Unity networking, any old client script could just directly make a network call ("CmdJump" in the example). All you needed was a link to a NetworkView.
Now you have to have this new "in-between" class, "Comms" in the example.
I am trying to understand,
is there a way for a normal script on the client side (like Jumping - a MonoBehavior) to directly call a Command
?
As far as I know, only NetworkBehaviour
derived classes can do that. In the old networking, any class could make such a call.
In "new" Unity networking, as far as I know you must you pass every call through a NetworkBehaviour
derived class, as in my example here.
As you can see, if I am "missing something," and the classes such as Jumping can just do it directly, then my code is heinous :)
Can you call Command
s directly from in a MonoBehavior?
Am I in fact completely wrong that you have to have the ridiculous auto-spawned "player" objects, and so on? Maybe there's an entirely other way to call Command
s.
The short answer to your question is No. Simply because using the
[Command]
keyword in aMonoBehavior
will immediately result in an error in your editor, since the code can't be compiled.However there are other possibilities that get very close to what you want to achieve.
[Command]
function to another function from a class that inherits fromMonoBehavior
.I have just made a simple interface that does not implement any other interfaces, it just declares a command. It looks like this:
Then I made a simple class that will implement this interface (the one method) without using the
[Command]
keyword. My custom class inherits fromMonoBehavior
and looks like this:I was then successfully able to call my
CmdCustomSpawn
function and spawn a player on the server.