Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiaion LAg vs using CoRoutine

Discussion in 'Scripting' started by pjenness, Dec 5, 2016.

  1. pjenness

    pjenness

    Joined:
    Mar 3, 2013
    Posts:
    52
    Hiya

    Im making a networked app. Part of the situation is on changing avatar I have to instantiate the Avatar selected on both the local client (for the player) and remote clients (other players) so everyone sees the same avatar.

    I cant pre instantiate and disable in advance, as playuers could end up using the same avatar if they want, which I cant forsee ahead of time.

    This instantiate causes a 1sec lag on all clients. Now multiply this when any player changes avatar it is not playable.

    What Im hoping to do is place the instantiae part in a CoRoutine so it happens separately from the main thread and not cause a lag.

    Does this make sense to do? If so ..what would that look like.

    From reading up its a combination of yield, IEnumarator and coRoutine I think..but this is new to me.

    Thanks for the help or example
    Cheer

    -Paul
     
  2. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    The trouble is that the co-routine just pauses the main thread. Unity doesn't do multithreading so easily (Unless things have changed in the past 2 months) and everything takes place in the same CPU thread.

    The better question would be: Why does player instantiation cause such a long pause?
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Or just pool the players.
     
  4. pjenness

    pjenness

    Joined:
    Mar 3, 2013
    Posts:
    52
    Unfortunately as mentioned I cant pool them as I have no idea before hand what avatars wil be chosen.

    This is an online VR social app, so 20+ people willl have selection of many avatars (as I add their availability)

    The instantiation is from the using the Morph3d Player. Must be a lot happening on instantiate.

    I thought the whole purpose of Coroutines is that is DOESNT pause the main thread, but allows that thread to move on while the coroutine splits off on another thread?

    EDIT: Reading more threads I see this is not the case.
    I will try Corountine and yield, but I think it wont help the lag



    -P
     
    Last edited: Dec 6, 2016
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    It's easy to confuse coroutines with threads, but coroutines are definitely not threads. The couroutine code executes in the main thread, as any other functions (Start, Update, FixedUpdate, LateUpdate, ...).
    The Unity API is not thread safe. So, if you want to do multi-threading, you can, but with the limitation that you can not use anything from Unity in other threads than main.
     
  6. pjenness

    pjenness

    Joined:
    Mar 3, 2013
    Posts:
    52
    Could I potentially put my instatiation and avatar setup into a IEnumerator coroutine.

    and do something like

    DOSTUFF
    yield
    DOSTUFF
    yield
    DOSTUFF
    yield

    so it split the function over a few frames instead trying all at once? Maybe not perfect but will help i think

    Is that what Thread Ninja does?
     
  7. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @pjenness
    I don't know about Thread Ninja and what it does exactly. But you can perfectly split a cpu intensive work over several frames to avoid lags. You would need to define how much work you could do per frame, the Profiler can help you with that.

    Note that you can do this with or without coroutines.