Skip to content

Instantly share code, notes, and snippets.

@SocketWeaver
Created June 17, 2019 00:00
Show Gist options
  • Save SocketWeaver/1f2640670ad78b7acbe8de13c54d7731 to your computer and use it in GitHub Desktop.
Save SocketWeaver/1f2640670ad78b7acbe8de13c54d7731 to your computer and use it in GitHub Desktop.
Update PlayerWeapon.cs to use SyncProperty to sync player's currentWeapon Index
using System.Collections;
using System.Collections.Generic;
using SWNetwork;
using UnityEngine;
public class PlayerWeapon : MonoBehaviour
{
public int CurrentWeapon = 0;
public List<Gun> Guns;
Player player;
NetworkID networkID;
SyncPropertyAgent syncPropertyAgent;
const string CURRENT_WEAPON = "CurrentWeapon";
private void Start()
{
player = GetComponent<Player>();
syncPropertyAgent = GetComponent<SyncPropertyAgent>();
networkID = GetComponent<NetworkID>();
if (networkID.IsMine)
{
for (int i = 0; i < Guns.Count; i++)
{
Guns[i].AssignIndex(i);
}
ActivateWeapon();
}
}
void Update()
{
if (player.Dead || !networkID.IsMine)
{
return;
}
if (Input.GetKeyUp(KeyCode.Alpha1))
{
syncPropertyAgent.Modify(CURRENT_WEAPON, 0);
}
else if (Input.GetKeyUp(KeyCode.Alpha2))
{
syncPropertyAgent.Modify(CURRENT_WEAPON, 1);
}
if (Input.GetButton("Fire1"))
{
Guns[CurrentWeapon].Fire();
}
}
public void ActivateWeapon()
{
CurrentWeapon = syncPropertyAgent.GetPropertyWithName(CURRENT_WEAPON).GetIntValue();
for (int i = 0; i < Guns.Count; i++)
{
if (i == CurrentWeapon)
{
Guns[i].gameObject.SetActive(true);
}
else
{
Guns[i].gameObject.SetActive(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment