Files
project-reset/Assets/Scripts/Core/Graph Tasks/SendEventToUnit.cs
2025-10-20 13:45:36 -04:00

66 lines
1.8 KiB
C#

using System;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using Unity.Netcode;
using UnityEngine;
namespace Reset.Units {
[Category("Reset")]
[Description("Send a string as an event to another unit. Works online as well")]
public class SendEventToUnit : ActionTask<Unit>{
public BBParameter<GameObject> target;
public string eventToSend;
protected override string info{
get{
return $"Send event <b>{eventToSend}</b> to <b>{target}</b>";
}
}
//Use for initialization. This is called only once in the lifetime of the task.
//Return null if init was successfull. Return an error string otherwise
protected override string OnInit() {
return null;
}
//This is called once each time the task is enabled.
//Call EndAction() to mark the action as finished, either in success or failure.
//EndAction can be called from anywhere.
protected override void OnExecute() {
EndAction(true);
}
//Called once per frame while the action is active.
protected override void OnUpdate(){
try {
if (target.value.GetComponent<Unit>().UnitIsNetworked()) {
ulong targetID = (target.value.GetComponent<NetworkObject>()).NetworkObjectId;
SendEventRpc(agent.RpcTarget.Single(targetID, RpcTargetUse.Temp));
} else {
target.value.GetComponent<GraphOwner>().SendEvent("eventToSend");
}
} catch (Exception e) {
Debug.LogError($"Failed to send event {eventToSend} to {target}: {e.Message}");
}
}
[Rpc(SendTo.SpecifiedInParams)]
void SendEventRpc(RpcParams rpcParams = default){
target.value.GetComponent<GraphOwner>().SendEvent("eventToSend");
Debug.Log("");
}
//Called when the task is disabled.
protected override void OnStop() {
}
//Called when the task is paused.
protected override void OnPause() {
}
}
}