Skip to content

Instantly share code, notes, and snippets.

@shubhank008
Created December 10, 2022 09:55
Show Gist options
  • Save shubhank008/2874a29c3d25574cfacf91828730f36e to your computer and use it in GitHub Desktop.
Save shubhank008/2874a29c3d25574cfacf91828730f36e to your computer and use it in GitHub Desktop.
Delete Building Entity from server
using System.Collections.Generic;
using UnityEngine;
using LiteNetLib;
using LiteNetLibManager;
using LiteNetLib.Utils;
using Cysharp.Threading.Tasks;
using UnityEngine.Networking;
using System;
namespace MultiplayerARPG.MMO
{
public partial class CentralNetworkManager : LiteNetLibManager.LiteNetLibManager
{
#if UNITY_STANDALONE && !CLIENT_BUILD
[DevExtMethods("RegisterMessages")]
protected void DevExtRegisterBuildingEntityDeleteMessages()
{
RegisterRequestToServer<RequestBuildingEntityDeleteMessage, ResponseBuildingEntityDeleteMessage>(MMORequestTypes.RequestBuildingEntityDelete, HandleRequestBuildingEntityDelete);
}
#endif
}
public static partial class MMORequestTypes
{
public const ushort RequestBuildingEntityDelete = 6010;
}
public struct ResponseBuildingEntityDeleteMessage : INetSerializable
{
public string response;
public UITextKeys message;
public void Deserialize(NetDataReader reader)
{
response = reader.GetString();
message = (UITextKeys)reader.GetPackedUShort();
}
public void Serialize(NetDataWriter writer)
{
writer.Put(response);
writer.PutPackedUShort((ushort)message);
}
}
public struct RequestBuildingEntityDeleteMessage : INetSerializable
{
public int dataId;
public void Deserialize(NetDataReader reader)
{
dataId = reader.GetInt();
}
public void Serialize(NetDataWriter writer)
{
writer.Put(dataId);
}
}
public partial class CentralNetworkManager
{
#if UNITY_STANDALONE && !CLIENT_BUILD
public bool RequestBuildingEntityDelete(int dataId, ResponseDelegate<ResponseBuildingEntityDeleteMessage> callback)
{
return ClientSendRequest(MMORequestTypes.RequestBuildingEntityDelete, new RequestBuildingEntityDeleteMessage()
{
dataId = dataId
}, responseDelegate: callback);
}
protected async UniTaskVoid HandleRequestBuildingEntityDelete(
RequestHandlerData requestHandler,
RequestBuildingEntityDeleteMessage request,
RequestProceedResultDelegate<ResponseBuildingEntityDeleteMessage> result)
{
int dataId = request.dataId;
Debug.Log("Trying to delete building id " + dataId);
foreach(BuildingEntity _entity in GameInstance.BuildingEntities.Values)
{
if(_entity.EntityId == dataId)
{
Debug.Log("Found building");
_entity.NetworkDestroy();
break;
}
}
}
#endif
}
public partial class MMOClientInstance : MonoBehaviour
{
public void RequestBuildingEntityDelete(BuildingEntity entity, ResponseDelegate<ResponseBuildingEntityDeleteMessage> callback)
{
centralNetworkManager.RequestBuildingEntityDelete(entity.EntityId, callback);
//CentralNetworkManager.RequestFirebaseLogin(username, password, (responseHandler, responseCode, response) => OnRequestFirebaseLogin(responseHandler, responseCode, response, callback).Forget());
}
private async UniTaskVoid OnRequestBuildingEntityDelete(ResponseHandlerData responseHandler, AckResponseCode responseCode, ResponseBuildingEntityDeleteMessage response, ResponseDelegate<ResponseBuildingEntityDeleteMessage> callback)
{
await UniTask.Yield();
if (callback != null)
callback.Invoke(responseHandler, responseCode, response);
if (responseCode == AckResponseCode.Success)
{
//Building Entity Deleted
}
}
}
}
@shubhank008
Copy link
Author

Call it using
MMOClientInstance.Singleton.RequestBuildingEntityDelete(buildingEntityReference, null);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment