Userdata problems

bigd
bigd
edited July 2015 in Photon Bolt
Hi guys,

Long time listener, second time poster. Huzzah.

I've had various conversations with folks around this topic in Jabbr chat and everyone has been extremely helpful. Unfortunately, I am still coming up short on making this work.

Basically I'm trying to reference the client's Player entity from the StreamDataReceived() callback that occurs after you have successfully streamed data to the client. This callback passes a BoltConnection and a Data variable, so naturally, I've been looking at what information I can pull from the BoltConnection.

Typically, how I understand this to work is that I can store an object reference within the BoltConnection using the userdata object. Once it's set, I can access it by using a "connection.GetPlayer()" reference and we're good to go. In reality, GetPlayer() get a null-pointer when I try to access it from the PlayerCallbacks class.

In essence, I want to be able to set a state variable on the entity I'm sending this data to so when after it's received, I can access that variable and do something with that data. The problem is, I can't access that entity from the connection that's passed in the StreamDataReceived() callback.

The relevant code chunks:
Assigning Player object to Userdata
[BoltGlobalBehaviour(BoltNetworkModes.Host, "Level2")]
public class ServerCallbacks : Bolt.GlobalEventListener {

public override void SceneLoadRemoteDone(BoltConnection c){
c.UserData = new Player();
c.GetPlayer().connection = c;
c.GetPlayer().name = "fart";
c.GetPlayer().InstantiateEntity();
}
Sending stream to connection
BoltConnection myConnection = client.controller;
myConnection.StreamBytes(myStream, mybyteArray);
Stream Received, error when trying to reference GetPlayer()

[BoltGlobalBehaviour("Level2")]
public class PlayerCallbacks : Bolt.GlobalEventListener {

public override void StreamDataReceived(BoltConnection connection, UdpStreamData data)
{
string name = connection.GetPlayer().name;
}
}
ERROR : Could not reference the object.

Any ideas?


Comments

  • Do you have this from the tutorial?
    using UnityEngine;
    using System.Collections;
    
    public static class ExtensionMethods
    {
    	public static Player GetPlayer (this BoltConnection connection)
    	{
    		if (connection == null) { return Player.serverPlayer; }
    		return (Player)connection.UserData;
    	}
    }
  • Yup. Still doesn't work. Thanks for the feedback though.

    I'm not sure if this is a bug or just my inability to get this work. Anyone else want to give it a go?

    I ended up saving the entity that's passed the the Control Gained callback in the Playercallbacks class and using that to get the player's entity..

    Would still be interested in getting this to work although banging my head at it for another couple days doesn't sound like fun.
  • ashoulson
    ashoulson
    edited July 2015
    UserData is only stored locally, it isn't synced over the network. If you store something to a connection's UserData on the server, it will still be a null reference on the client, and vice versa.
  • I tried storing this locally as well and was unsuccessful. :-(