Author Topic: [TNet3] Persistent TNObject and changing channels  (Read 2936 times)

Ninja

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 21
    • View Profile
[TNet3] Persistent TNObject and changing channels
« on: February 08, 2016, 01:28:13 AM »
I'm having an issue with persistent network objects and joining different channels.

I have a TNObject with DontDestroyOnLoad so it can move between scenes. Everything works fine on Channel 0, but if you change channels (and leave channel 0) it will no longer receive any RFCs.

I've tried to make the TNObject change channels with setting the channelID manually, and also by calling TNObject.TransferToChannel(channelID), but nothing seems to be working.

How do I make the object change channels with the user correctly?

Attaching a small example project that shows what I'm trying to explain.
- Import the attached package into an empty project
- Import TNet3
- Add the three included scenes to the build settings
- Build it and run it along with a server
- While connected, pressing Space will send a RFC to all, and in channel 0 it is received. In any other channel it isn't received.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [TNet3] Persistent TNObject and changing channels
« Reply #1 on: February 08, 2016, 01:54:10 AM »
Did you check the multi-channel example that comes with TNet 3? The script it uses -- ExampleTransfer -- shows how to transfer objects between channels.

Only dynamically instantiated objects can be transferred between channels properly. Looking at your example you also seem to be leaving the channel before joining a new one... not sure why you are doing this.

Ninja

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: [TNet3] Persistent TNObject and changing channels
« Reply #2 on: February 08, 2016, 02:33:45 AM »
I've been converting one of my projects over from PUN to TNet.

For PUN, the way I had it handling changing maps was by walking onto a trigger which told the player what map to move to, disconnects them from the channel, and in the OnLeftChannel notification it would join the previously set channel specified from the trigger. In PUN you can only be connected to one channel at a time and have to leave before attempting to connect to the next one.

I have a static TNObject that has some RFCs on it that persists through scenes and worked fine in PUN but not TNet.

It's been a while since I've played around with TNet and I haven't worked with multiple channels before, is there a better way to do what I want?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [TNet3] Persistent TNObject and changing channels
« Reply #3 on: February 08, 2016, 03:14:25 AM »
You can (and should) be in multiple channels at once. Split your nearby world into a virtual grid with each cell using some specific channel ID. When you're in the world, join the 9 closest regions. For example:
  1. . . . . .
  2. . X X X .
  3. . X O X .
  4. . X X X .
  5. . . . . .
The player would be in "O". You join all 9 at the same time. As you travel around the world, transfer your player avatar between channels. Simple. The only key here is to make sure that your player avatar is instantiated using TNManager.Instantiate. It can't be a static scene object.

Put your player avatar into a prefab, move it into the Resources folder so it can be loaded. Now create an empty game object in your scene where the avatar used to be. Put TNAutoCreate script on it and tell it to instantiate your newly created prefab. Now as soon as you load the scene, your player avatar will be instantiated and you will be able to transfer it between channels.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [TNet3] Persistent TNObject and changing channels
« Reply #4 on: February 08, 2016, 08:11:20 AM »
I've gone ahead and created a better multi-channel example showing both simultaneous multiple channel subscriptions as well as a player vehicle that travels between channels. Fixed a few bugs too. If you don't have Pro access, I recommend getting in touch for an updated package.

Ninja

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: [TNet3] Persistent TNObject and changing channels
« Reply #5 on: February 08, 2016, 08:36:09 AM »
Ok awesome, I'll try that out.

Thanks! Keep up the great work!