+ Reply to Thread
Results 1 to 5 of 5

Thread: [Unity3D] Listening For Two Plugins - DatabasePlugin & AvatarChatPlugin

  1. #1
    Member
    Join Date
    Apr 2012
    Location
    USA | Chicago, IL
    Posts
    35
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question [Unity3D] Listening For Two Plugins - DatabasePlugin & AvatarChatPlugin

    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.
    Code:
    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!!!

  2. #2
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,284
    Thanks
    81
    Thanked 1,093 Times in 1,082 Posts
    In your NetworkController.onPluginMessageEvent, the first IF statement is throwing away the DatabasePlugin plugin events:
    Code:
            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.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  3. #3
    Member
    Join Date
    Apr 2012
    Location
    USA | Chicago, IL
    Posts
    35
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tcarr View Post
    In your NetworkController.onPluginMessageEvent, the first IF statement is throwing away the DatabasePlugin plugin events:
    Code:
            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?

  4. #4
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,284
    Thanks
    81
    Thanked 1,093 Times in 1,082 Posts
    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:
    Code:
            if (e.PluginName != PLUGIN_NAME)
            {
                if (e.PluginName == DB_PLUGIN_NAME)
                {
                      handleDatabasePluginEvent(e);
                 }
                return;
            }
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  5. The Following User Says Thank You to tcarr For This Useful Post:

    Xerosigma (04-20-2012)

  6. #5
    Member
    Join Date
    Apr 2012
    Location
    USA | Chicago, IL
    Posts
    35
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Problem solved! I just separated the communication of each plugin into two separate files to make it less confusing xD

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts