Basically I have this:
public class ShipDoor : UsableObject // UsuableObject inherits from NetworkBehavior
{
[SyncVar]
bool isOpen = false;
[SyncVar]
float currentTransition = 0;
public override void Use()
{
CmdUseDoor();
Debug.Log("Opening " + m_name);
}
[Command]
void CmdUseDoor()
{
Debug.Log("Opening(Server) " + m_name);
isOpen = !isOpen;
currentTransition = 0;
}
}
When a player clicks an object he gets a small dialog with a list of commands, when he clicks the command it is called on the object and the object will do whatever it needs to in the [Command].
However, since my client has no authority over this object he can’t call the [Command]. How can I allow this? There will be many objects with various commands all doing different things. It would feel a bit messy to cram them all into a single script on the Player object.
The example here is just the client saying “server, I want to use this door” and letting the server do whatever is necessary for that. But without authority I can’t interact with the object at all.
My guess is I’d have to call something on the player like:
void Use(GameObject obj)
{
CmdUse(obj);
}
[Command]
void CmdUse(GameObject obj)
{
obj.GetComponent<UsableObject>.Use();
}
And make sure they have a method for every interaction type. I’m hoping I can do it from the object itself, but if not, would this be the least messy alternative?
Further explanation of how it is called:
Essentially it will create buttons for a tooltip with delgate commands to send when the buttons are clicked. The below is in the door script:
public override void CreateUIInterface(Text titleText, GameObject buttonMenu, GameObject toolTipButton, PlayerInteraction playerInteraction)
{
titleText.text = GetName();
GameObject obj = (GameObject)Instantiate(toolTipButton, buttonMenu.transform);
obj.transform.SetParent(buttonMenu.transform);
obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
if (isOpen)
obj.GetComponent<Button>().GetComponentInChildren<Text>().text = "Close";
else
obj.GetComponent<Button>().GetComponentInChildren<Text>().text = "Open";
obj.GetComponent<Button>().onClick.AddListener(delegate { Use(); });
obj.GetComponent<Button>().onClick.AddListener(delegate { playerInteraction.ClearTooltip(); });
Debug.Log("Instantiated Open: " + toolTipButton.name + ", " + buttonMenu.transform.name);
obj = (GameObject)Instantiate(toolTipButton, buttonMenu.transform);
obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
obj.GetComponent<Button>().GetComponentInChildren<Text>().text = "Destroy";
obj.GetComponent<Button>().onClick.AddListener(delegate { Destroy(); });
obj.GetComponent<Button>().onClick.AddListener(delegate { playerInteraction.ClearTooltip(); });
obj = (GameObject)Instantiate(toolTipButton, buttonMenu.transform);
obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
obj.GetComponent<Button>().GetComponentInChildren<Text>().text = "Switch";
obj.GetComponent<Button>().onClick.AddListener(delegate { Switch(); });
obj.GetComponent<Button>().onClick.AddListener(delegate { playerInteraction.ClearTooltip(); });
}
So with this it doesn’t have an initial method that is called, it just goes straight to the door with the method. I have tried using the Command as the delegate but it’s the same as calling it straight from the door itself (no authority).