Author Topic: "Trying to create an object while switching scenes"?  (Read 1821 times)

scornflake

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 15
    • View Profile
"Trying to create an object while switching scenes"?
« on: April 02, 2017, 07:02:46 AM »
I'm trying to create an object to represent the connected player.
The idea is that I instantiate a prefab to represent the connected user (this is a small test project, I expect 2-5 users max).

I have scenes:
- Connection
- Lobby
- Game

I'm stuck on "Lobby", where I try to create the LobbyPlayer prefab using RCC.
i.e: Connection (Scene) -> takes player name -> TNAutoJoin -> Lobby (Scene) -> "my own LobbyController"
Here's a video showing what I am doing:

https://www.dropbox.com/s/vkl9ec2t9g8z4fd/Instantiation%20Problem.mp4?dl=0

feel like I'm missing something obvious in my understanding. Any help appreciated!


scornflake

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 15
    • View Profile
Re: "Trying to create an object while switching scenes"?
« Reply #1 on: April 02, 2017, 07:02:19 PM »
I've been digging a little further, and it seems that TN thinks I'm still joining the channel (see attachment) at the time I perform the instantiation.

The IsJoiningChannel method checks mLoadingLevel and also mClient.IsJoiningChannel.
Doesn't this mean that I can't instantiate any objects within the Start() of a script that is being run as part of a level load?

The flow I'm observing:

- Connect to server
- TN JoinChannel method loads the level as part of response to successful channel join
- TN calls LoadLevelCoroutine to load the level
- My Start() called
- End of load level  (handled by async coroutine in TN)

The clearing of mLoadingLevel is only done at the end of the load level coroutine.
If what I'm seeing is true, what's the recommended way then to instantiate network objects, upon a level start? 

or have I completely barked up the wrong tree here?


cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: "Trying to create an object while switching scenes"?
« Reply #2 on: April 02, 2017, 07:26:19 PM »
The TNAutoCreate script is what you're looking for, I think. In short, make your Start() function return an IEnumerator and just yield until TNManager.isJoiningChannel is false. Kind of a lesser-known feature of Unity, I guess, you can turn most (all?) Unity message functions (Awake, Start, Update, etc) into IEnumerator and yield within them.

scornflake

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 1
  • Posts: 15
    • View Profile
Re: "Trying to create an object while switching scenes"?
« Reply #3 on: April 03, 2017, 01:17:25 AM »
Ahhhhh......
THAT is useful. Thank you.