I have tested with the debug log.
The OnPointerClicks works as intended
Network variable that also work on the OnPointerClicks method
The script derive from NetworkBehaviour and it has NetworkObject scrip in the same game object
Problem:
The server rpc method cannot be called. When I tried without using the rpc stuff, the script work as intended.
Question :
Are there special methods in implementing the Unity event system with Netcode for Gameobjects?
using MIST.Inventory;
using MIST.Item.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ObjectInventorySlot : NetworkBehaviour, IPointerClickHandler
{
#region Fields
[SerializeField] private Image itemIconImage;
[SerializeField] private TextMeshProUGUI quantityText;
[SerializeField] private GameObject emptyVisual;
[SerializeField] private GameObject filledVisual;
#endregion
#region Parameters
NetworkObjectReference networkRef;
ObjectInventorySystem objectInventorySystem;
private ItemModel item;
ulong clientID;
#endregion
#region Network
private NetworkVariable<int> counter = new NetworkVariable<int>();
#endregion
public override void OnNetworkSpawn()
{
if (IsServer)
{
counter.Value = 0;
}
}
public void UpdateItem(NetworkObjectReference networkRef, ObjectInventorySystem inventory, ItemModel item)
{
this.networkRef = networkRef;
this.item = item;
objectInventorySystem = inventory;
UpdateObjectInventoryUI();
}
private void UpdateObjectInventoryUI()
{
emptyVisual.SetActive(item == null);
filledVisual.SetActive(item != null);
if (item != null)
{
//Debug.Log("item.ID = " + item.ID);
ItemBaseSO itemData = objectInventorySystem.GetItemBaseFromID(item.ID);
if (itemData != null)
{
itemIconImage.sprite = itemData.GetItemImage();
}
quantityText.text = item.Quantity.ToString();
}
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("On Pointer CLick");
counter.Value++;
Debug.Log("counter.Value" + counter.Value);
RequestLootItemServerRpc(networkRef);
}
[ServerRpc]
public void RequestLootItemServerRpc(NetworkObjectReference networkRef)
{
GiveLootClientRpc(networkRef);
}
[ClientRpc]
public void GiveLootClientRpc(NetworkObjectReference networkRef)
{
// Check if this is the correct client to respond to
NetworkObject networkObject = networkRef;
clientID = networkObject.OwnerClientId;
Debug.Log("clientID : " + clientID);
if (NetworkManager.Singleton.LocalClientId != clientID)
return;
var result = PlayerInventorySystem.Instance.TryAddItem(networkRef,item, item.Quantity);
bool itemCollected = result.Item1;
int leftoverItems = result.Item2;
if (!itemCollected) return;
if (leftoverItems > 0)
{
item.Quantity = leftoverItems;
UpdateItem(networkObject, objectInventorySystem, item);
return;
}
}
public void Dispose()
{
}
/*
private void CheckUpdateQuantity(ItemModel item, ObjectInventorySystem inventory)
{
if (this.item == item)
{
UpdateItem(item, inventory);
}
}
*/
}
Nanachi88:
I have tested with the debug log.
The OnPointerClicks works as intended
Network variable that also work on the OnPointerClicks method
The script derive from NetworkBehaviour and it has NetworkObject scrip in the same game object
Problem:
The server rpc method cannot be called. When I tried without using the rpc stuff, the script work as intended.
Question :
Are there special methods in implementing the Unity event system with Netcode for Gameobjects?
using MIST.Inventory;
using MIST.Item.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ObjectInventorySlot : NetworkBehaviour, IPointerClickHandler
{
#region Fields
[SerializeField] private Image itemIconImage;
[SerializeField] private TextMeshProUGUI quantityText;
[SerializeField] private GameObject emptyVisual;
[SerializeField] private GameObject filledVisual;
#endregion
#region Parameters
NetworkObjectReference networkRef;
ObjectInventorySystem objectInventorySystem;
private ItemModel item;
ulong clientID;
#endregion
#region Network
private NetworkVariable<int> counter = new NetworkVariable<int>();
#endregion
public override void OnNetworkSpawn()
{
if (IsServer)
{
counter.Value = 0;
}
}
public void UpdateItem(NetworkObjectReference networkRef, ObjectInventorySystem inventory, ItemModel item)
{
this.networkRef = networkRef;
this.item = item;
objectInventorySystem = inventory;
UpdateObjectInventoryUI();
}
private void UpdateObjectInventoryUI()
{
emptyVisual.SetActive(item == null);
filledVisual.SetActive(item != null);
if (item != null)
{
//Debug.Log("item.ID = " + item.ID);
ItemBaseSO itemData = objectInventorySystem.GetItemBaseFromID(item.ID);
if (itemData != null)
{
itemIconImage.sprite = itemData.GetItemImage();
}
quantityText.text = item.Quantity.ToString();
}
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("On Pointer CLick");
counter.Value++;
Debug.Log("counter.Value" + counter.Value);
RequestLootItemServerRpc(networkRef);
}
[ServerRpc]
public void RequestLootItemServerRpc(NetworkObjectReference networkRef)
{
GiveLootClientRpc(networkRef);
}
[ClientRpc]
public void GiveLootClientRpc(NetworkObjectReference networkRef)
{
// Check if this is the correct client to respond to
NetworkObject networkObject = networkRef;
clientID = networkObject.OwnerClientId;
Debug.Log("clientID : " + clientID);
if (NetworkManager.Singleton.LocalClientId != clientID)
return;
var result = PlayerInventorySystem.Instance.TryAddItem(networkRef,item, item.Quantity);
bool itemCollected = result.Item1;
int leftoverItems = result.Item2;
if (!itemCollected) return;
if (leftoverItems > 0)
{
item.Quantity = leftoverItems;
UpdateItem(networkObject, objectInventorySystem, item);
return;
}
}
public void Dispose()
{
}
/*
private void CheckUpdateQuantity(ItemModel item, ObjectInventorySystem inventory)
{
if (this.item == item)
{
UpdateItem(item, inventory);
}
}
*/
}
Was the object spawned before you tried to call the RPC? Also, do:
[ServerRPC (requireOwnership = false)]
If you want the clients to be able to call that RPC.
Hi there,
I problem seems at this line:
[ServerRpc]
public void RequestLootItemServerRpc(NetworkObjectReference networkRef)
{
GiveLootClientRpc(networkRef);
}
All client can call the “RequestLootItemServerRpc ” but, just Host/Server call the “GiveLootClientRpc(networkRef); ”.
This means “GiveLootClientRPC ” runs only on the server/Host side. Because “if (NetworkManager.Singleton.LocalClientId != clientID) ” line only passes on Host/Server side not on others client’s side.
Hello thank you for replying, the reason I did not add
[ServerRPC (requireOwnership = false)]
because I forgot, and normally I test with Server first. I have implemented this, but still, OnClick, either the ServerRpc or ClientRpc method cannot be executed.
It is very frustrating, even testing with empty method with only Debug.Log, it doesnt work.
Mj-Kkaya:
Hi there,
I problem seems at this line:
[ServerRpc]
public void RequestLootItemServerRpc(NetworkObjectReference networkRef)
{
GiveLootClientRpc(networkRef);
}
All client can call the “RequestLootItemServerRpc ” but, just Host/Server call the “GiveLootClientRpc(networkRef); ”.
This means “GiveLootClientRPC ” runs only on the server/Host side. Because “if (NetworkManager.Singleton.LocalClientId != clientID) ” line only passes on Host/Server side not on others client’s side.
I have implemented [ServerRPC (requireOwnership = false)] . Nevertheless, neither Server or Client can execute ServerRpc nor ClientRpc OnClick.
I have tried OnPointerDown as well, it just doesnt work. I have tried using Button, doesnt work.
I have tested this code below, it works. I will try to review and try another solution again related to my code
public class testOnclick : NetworkBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("test 1");
testServerRpc();
}
[ServerRpc]
private void testServerRpc()
{
Debug.Log("test 2");
}
}
The problem has been solved. I deactivated the game object in which this script is attached. I deactivated it in Awake() meaning that the network stuff could not initialize properly.