PDA

View Full Version : [Solved] [Unity3D] Listening For Two Plugins - DatabasePlugin & AvatarChatPlugin



Xerosigma
04-20-2012, 12:35 AM
To be frank, I basically combined the DatabasePlugin & AvatarChatPlugin to achieve what I wanted.

Sending and receiving works for the AvatarChatPlugin but only SENDING works for the Database.

The issue is clear, there is no listener specific to the DatabasePlugin OR from a different perspective, the current listener doesn't know how to handle a message from the DatabasePlugin.



NetworkController.cs | NOTE: You can clearly see the Database Plugin Functions at the bottom. Stripped most of the code due to the post limit. Left the important parts though. Important blocks are color coded.


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using System;
using Electrotank.Electroserver5.Api;
using Electrotank.Electroserver5.Core;

public class NetworkController : MonoBehaviour
{
// Database Plugin Name
public static string DB_PLUGIN_NAME = "DatabasePlugin";

public static string PLUGIN_NAME = "OplexOrigins";
public static string EXTENSION_NAME = "OplexOriginsExtension";

public Transform localplayer;
private ElectroServer _es;
private Room room = null;
private string _userName;
private bool started = false;
private int as3_x = -1;
private int as3_y = -1;
private PlayerSpawnController playerSpawnController = null;


// Sends Formatted ESObjects To CORE Plugin
public void sendToPlugin(EsObject esob)
{
if (room != null && _es != null)
{
// Build Request
PluginRequest pr = new PluginRequest();
pr.Parameters = esob;
pr.RoomId = room.Id;
pr.ZoneId = room.ZoneId;
pr.PluginName = PLUGIN_NAME;

// Send It
_es.Engine.Send(pr);
}
}

// Sends a position update message to the plugin.
public void sendPositionUpdate(EsObject esob)
{
esob.setBoolean(PluginTags.USE_UDP, useUDP);
sendToPlugin(esob);
}

void FixedUpdate()
{
if (started)
{
/*
* Dispatch events from the Electroserver instance internal event queue.
* Being in fixed update ensures it occurs in a timely fashion independent
* of frame rate.
*/
_es.Engine.Dispatch();
}

}

void Update()
{
if (Input.GetKeyDown (KeyCode.Mouse0))
{
SendMessage("UpdateStatusMessage", _userName + " has fired a shot!!!");
localplayerfired();
}
if(Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") !=0)
{
if(Input.GetKey (KeyCode.F))
{
fls ();
}
}
}


public void SendPublicMessage(string message)
{
PublicMessageRequest request = new PublicMessageRequest();
request.Message = message;
request.RoomId = room.Id;
request.ZoneId = room.ZoneId;

_es.Engine.Send(request);
}

// Called When Plugin Message Arrives
private void onPluginMessageEvent(PluginMessageEvent e)
{
if (e.PluginName != PLUGIN_NAME)
{
// we aren't interested, don't know how to process it
return;
}

EsObject esob = e.Parameters;
//trace the EsObject payload; comment this out after debugging finishes!
//Log("Plugin event: " + esob.ToString());

//get the action which determines what we do next
string action = esob.getString(PluginTags.ACTION);
if (action == PluginTags.POSITION_UPDATE_EVENT)
{
handlePositionUpdateEvent(esob);
}
else if (action == PluginTags.AVATAR_STATE_EVENT)
{
SendAnimationMessageToRemotePlayerObject(esob);
}
else if (action == PluginTags.USER_LIST_RESPONSE)
{
handleUserListResponse(esob);
}
else if (action == PluginTags.USER_ENTER_EVENT)
{
handleUserEnterEvent(esob);
}
else if (action == PluginTags.USER_EXIT_EVENT)
{
handleUserExitEvent(esob);
}
else if (action == PluginTags.ADD_TO_RANK || action == PluginTags.GET_RANK)
{
updateDisplay(esob);
}
else if (action == "Fire Out")
{
FireRecieved(esob);
}
else if (action == "Flash_Light_Update")
{
flashlight_recieve(esob);
}
else
{
Log("Action not handled: " + action);
}
}



//================================//
//= Database Functions
//================================//

// Called When Database Plugin Message Arrives
/*private void onPluginMessageEvent(PluginMessageEvent e)
{
if (e.PluginName != DB_PLUGIN_NAME)
{
// we aren't interested, don't know how to process it
return;
}

EsObject esob = e.Parameters;
//trace the EsObject payload; comment this out after debugging finishes!
//Log("Plugin event: " + esob.ToString());

//get the action which determines what we do next
string action = esob.getString(PluginTags.ACTION);
if (action == PluginTags.ADD_TO_RANK || action == PluginTags.GET_RANK)
{
updateDisplay(esob);
}
else
{
Log("Action not handled: " + action);
}
}*/

// Sends Formatted ESObjects To DATABASE Plugin
public void sendToDatabase(EsObject esob)
{
Log("Database Request Fired!!!");
if (room != null && _es != null)
{
// Build Request
PluginRequest pr = new PluginRequest();
pr.Parameters = esob;

// This Is A Server Level Plugin! RoomId/ZoneId Is A No Go!
pr.PluginName = DB_PLUGIN_NAME;

// Send It
_es.Engine.Send(pr);
}
}

// Displays Database Elements When Called
private void updateDisplay(EsObject esob)
{
Log("updateDisplay Fired!!!");
int rank = esob.getInteger(PluginTags.GET_RANK);

SendMessage("onRankChange", "" + rank);

GameObject gObj = GameObject.Find("CharGUI");
gObj.SendMessage("onRankChange", "" + rank);
//GameScreen gameScreen = (GameScreen)gObj.GetComponent<GameScreen>();
//gameScreen.rank = "" + rank;
}

// Append Health
public void doAddToRank(string amount)
{
Log("doAddToRank Fired!!!");
int delta = Convert.ToInt32(amount);
EsObject esob = new EsObject();
esob.setString(PluginTags.ACTION, PluginTags.ADD_TO_RANK);
esob.setInteger(PluginTags.ADD_TO_RANK, delta);

sendToDatabase(esob);
}

// Get Current Health
public void doGetRank()
{
Log("doGetRank Fired!!!");
EsObject esob = new EsObject();
esob.setString(PluginTags.ACTION, PluginTags.GET_RANK);

sendToDatabase(esob);
}
}



So what can I do to get this to work?

Thanks!!!

tcarr
04-20-2012, 12:50 AM
In your NetworkController.onPluginMessageEvent, the first IF statement is throwing away the DatabasePlugin plugin events:


if (e.PluginName != PLUGIN_NAME)
{
// we aren't interested, don't know how to process it
return;
}


Just change this so that if the message is from DatabasePlugin it is processed correctly instead of ignored.

Xerosigma
04-20-2012, 01:51 AM
In your NetworkController.onPluginMessageEvent, the first IF statement is throwing away the DatabasePlugin plugin events:


if (e.PluginName != PLUGIN_NAME)
{
// we aren't interested, don't know how to process it
return;
}


Just change this so that if the message is from DatabasePlugin it is processed correctly instead of ignored.

How would I check the identifier of the DatabasePlugin? And where is this Identifier set?

if (e.PluginName != PLUGIN_NAME || e.PluginName != DB_PLUGIN_NAME)

e.PluginName I assume is it. Where do I find this in the extension?

tcarr
04-20-2012, 02:02 AM
I'd separate the two. Make a function for handling the case where e.PluginName == DB_PLUGIN_NAME. You said that you were sending plugin messages to the DatabasePlugin; the plugin name you want is the one you use for sending: "DatabasePlugin".

edit: so your original IF would become:


if (e.PluginName != PLUGIN_NAME)
{
if (e.PluginName == DB_PLUGIN_NAME)
{
handleDatabasePluginEvent(e);
}
return;
}

Xerosigma
04-20-2012, 05:47 AM
Problem solved! I just separated the communication of each plugin into two separate files to make it less confusing xD