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

Setting a simulation tick rate

Discussion in 'Scripting' started by Staples, Dec 5, 2012.

  1. Staples

    Staples

    Joined:
    Jan 14, 2009
    Posts:
    224
    Hi all,

    I'm going to give a *very* basic multiplayer rts lockstep a try. No physics, limited to integers for everything. I'm mostly interested in how to synchronize the simulations in unity at this stage. (http://www.gamasutra.com/view/feature/3094/1500_archers_on_a_288_network_.php)

    Just not sure how to go about implementing the simulation frames at a specified rate. Do I use fixed update, do I use coroutines, do I just run a while loop somewhere?

    Basically I want each tick to happen every 200 ms. So when a client performs some input it is sent to the server, the server then queues it up somehow to be sent to all clients to be performed 2 ticks later than the current tick.

    Any ideas?
     
  2. Staples

    Staples

    Joined:
    Jan 14, 2009
    Posts:
    224
    Anyone have any experience with this? :(

    I'll get it working without multiplayer first, so even just lockstep for a single player RTS would be helpful.
     
  3. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Well, whenever you catch yourself saying 'every X seconds' then you want to do this:

    Code (csharp):
    1. while ( !done ) {
    2.   DoIt();
    3.   yield WaitForSeconds( X );
    4. }
    If there's no physics, you technically CAN use fixedUpdate and just change the physics time step, as long as you clearly document to yourself that that's what's going on, since it's a little less straightforward than the normal usage.

    I imagine it'd be easiest to use an actual queue on the server using a List.<whatevertype>.
     
  4. Staples

    Staples

    Joined:
    Jan 14, 2009
    Posts:
    224
    Thanks. There might be physics eventually but it will be for client side only stuff that doesn't effect any determinism (maybe something explodes somewhere, doesn't matter if it's different on each client).

    So I'll avoid using fixed update.

    List is probably okay for the queue since the queue probably isn't a big performance hit, worst case I can change to built-ins though.

    So a simple while loop, and just keep track of the simulation ticks and process them as required in the queue I suppose!
     
  5. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
  6. Staples

    Staples

    Joined:
    Jan 14, 2009
    Posts:
    224
    Hmm true.

    Is there any advantage or difference between them?

    InvokeRepeating probably does something like the while loop internally?
     
  7. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    I would imagine that it does something similar, but using the yield WaitForSeconds is creating a new WaitForSeconds YieldInstruction every time it hits it.
    The limitation is that there cannot be any parameters passed. But you also don't have to declare the invoked method as IEnumerator either.
    This is the go to way of making a repeating event. You can also cancel invocations by using CancelInvoke.
     
  8. Staples

    Staples

    Joined:
    Jan 14, 2009
    Posts:
    224
    Thanks, I'll look into it a bit more.

    In the grand scheme of things I guess either method would be fine.