Question by
Hr-Orange · Apr 02, 2019 at 04:44 PM ·
c#networkingcommandlobbynetworkplayer
i just can't seem to figure out how to make a voting system, using SyncVar hooks and commands
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class KingVoteV1 : NetworkLobbyPlayer { public VotingScriptFinal Script;
void Start()
{
Script = GameObject.Find("VotingObject").GetComponent<VotingScriptFinal>();
}
/*activates when specific player is clicked. It will send a message to a object on the server that
holds everybodies vites as SyncVar with hooks that are supposed to change the votes for everyone,
when the votes are given*/
public void Clicked()
{
//doesnt work
Script.Vote(slot);
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking;
public class VotingScriptFinal : NetworkBehaviour { private bool FirstVote = true; public uint LastVotedGuy = 6;
public Text LastGuyText;
public Text VotesGivenText;
public Text P1;
public Text P2;
[SyncVar(hook = "P1Change")]
public int P1V;
[SyncVar(hook = "P2Change")]
public int P2V;
void Start()
{
LastGuyText = GameObject.Find("Last Guy").GetComponent<Text>();
VotesGivenText = GameObject.Find("VotesGiven").GetComponent<Text>();
P1 = GameObject.Find("P1").GetComponent<Text>();
P2 = GameObject.Find("P2").GetComponent<Text>();
P1Change(P1V);
P2Change(P2V);
UpdateTexts();
}
void P1Change(int ChangedTo)
{
P1V = ChangedTo;
}
void P2Change(int ChangedTo)
{
P2V = ChangedTo;
}
public void Vote(uint Slot)
{
if (Slot != LastVotedGuy)
{
if (FirstVote == true)
FirstVote = false;
else CmdRemoveVoteKing();
CmdVoteKing(Slot);
LastVotedGuy = Slot;
UpdateTexts();
}
}
[Command]
public void CmdVoteKing(uint Slot)
{
if (Slot == 0)
P1V++;
else if (Slot == 1)
P2V++;
}
[Command]
public void CmdRemoveVoteKing()
{
if (LastVotedGuy == 0)
P1V--;
else if (LastVotedGuy == 1)
P2V--;
}
public void UpdateTexts()
{
//updatere a text in the game with how many votes everybody has, and som extra ;)
LastGuyText.text = "Last Voted Guy: " + LastVotedGuy.ToString();
P1.text = "P1: " + P1V.ToString();
P2.text = "P2: " + P2V.ToString();
}
}
Comment