-1

We would like to run the Unity Game Loop in sync with external hardware and applications running on a different computer. We want to arrive at something that work similar (but does not have to be identical to this).

  1. Receive remote TCP packet with a list of new locations (etc.) for GameObjects [For low latency, would we put this in a FixedUpdate loop, set at a high Hz?] [Is this best done in a [Manager Singleton]1?]
  2. Send these updates from this "singleton" to all game objects [One way might be to just have the object pick up the data in the Update cycle, which would happen after the fixedUpdate is done]. [Is a better idea to forget about the gameloop, and just use C# events to broadcast the updates to the gameobject attached scripts?] Each GameObject will then update its location
  3. Once all is stable (step #2 is completely done by all objects), we need to do measurements (scene read only) (with Raycast and LineCast) from each object to each other to see what is occluded and what is visible [Can we rely on LateUpdate to run afterwards, or should we avoid the game loop] [How do we know that all gameobjects have finished updating?]
  4. Finally we need to post the visibility results back via a TCP packet.

The overall goal here is to make all four steps run as fast (back-to-back) as possible, Best would be to be able to speed up the frame rate (or slow it down) based on when each phase is done. But we can live with a fixed 60Hz if needed.

Other questions, have to do with the Async nature of TCP (or UDP) clients inside of Unity, and the issues of not being able to use the API from a separate thread (e.g. the async results of the TCP receive]. Has anyone pointers to solutions to that?

We need no help writing the code, but the internal execution model of Unity3D at this level of detail is not clear in the manuals.

We think this has wide applicability for VR, AR, and various robot co-sync with games.

Dr.YSG
  • 179
  • 1
  • 8
  • It doesn't sound like you need to control the update loop for this at all. Just fire off this sequence of events after receiving the appropriate network event. By calling these yourself, you don't have to worry much about Unity's loop. – DMGregory Jun 08 '16 at 01:55
  • @DMGregory if we do "go out of the update loop" don't we run the risk of running a a seperate thread and having the UI locked against us? Can we still get a jerky (not smooth) animation (it might be nice for debug purposes). I interspered a few questions in with ways one can avoid the game loop, but not sure if that will workk – Dr.YSG Jun 08 '16 at 14:29
  • 1
    You can't manipulate most aspects of the game state from a separate thread in Unity anyway, so you should probably consider that off the table. You can use coroutines to split up heavy work to avoid framerate drops or UI issues. This is really sounding like an XY problem though. What you really want to ask is how to build a line of sight checking service. Trying to rearchitect Unity's game loop is unlikely to be a good solution to this problem. – DMGregory Jun 08 '16 at 14:37
  • There are other parts of the problems we are solving other than line of sight, and there are lots of things in Unity we cannot find in packages that at first glance made more sense (DARPA Gazebo, Cesium, Panda, SimulLink, etc) – Dr.YSG Jun 08 '16 at 14:47
  • @DMGregory a lot of people struggle with repeating co-routines. Do you have a better example than this: http://answers.unity3d.com/questions/876960/how-to-make-waitforseconds-work-as-a-repeatingloop.html – Dr.YSG Jun 08 '16 at 14:50
  • With everything in Unity, it's easy to find examples of people doing things hilariously badly and blaming the engine. ;) The coroutine system actually works quite well though when you use it correctly - see the second part of this answer for an example. – DMGregory Jun 08 '16 at 15:01
  • I think I came of with a good solution, that does not violate Unity3D philosophy. I am getting up to 1500fps of results. @DMGregory, thanks for the points to coroutines. I would write up my solution, but what the point when hilariously simple-minded folks down-vote posts that have problems that their little minds can't get their heads around. – Dr.YSG Jun 09 '16 at 19:08
  • Oh yes, I found this artlce about fixedFrame rates very useful: http://forum.unity3d.com/threads/the-truth-about-fixedupdate.231637/ – Dr.YSG Jun 09 '16 at 19:11
  • Getting too hung up about upvotes and downvotes on StackExchange is a recipe for a not-fun time. ;) My experience is people typically don't downvote for no reason - it's easy in games for a post that's clear to its author to be unclear even to other skilled gamedevs (games are just that weird and varied), so usually downvotes are an honest signal that clarifications could improve the post. Our motivation should be about sharing information that will help one another, not about maximizing an arbitrary numeric score. – DMGregory Jun 09 '16 at 19:41

2 Answers2

2

That's not really how Unity works, you're not intended to directly control the FixedUpdate or Update or LateUpdate methods (but you can write your own methods). Also, FixedUpdate is named the way it is because it's supposed to run at a fixed timestep. Running it once whenever a network message comes in defeats the purpose of it being a fixed update.

Additionally you can't really run 20 or so methods "in parallel". Even spawning threads for each method is only going to have some wait while others are processed. If you're just looking for a on-command line-of-sight tester, Unity probably isn't the best technology choice. You'd be better off writing something without the overhead of an entire engine.

House
  • 73,224
  • 17
  • 184
  • 273
  • you are correct @Byte56, and I really did not like the way it was written. I rewrote the question from scratch. But we really like things we can get in Unity3D we cannot get in a robot sim framework such as DARPA Gazebo. – Dr.YSG Jun 08 '16 at 14:26
0

As @DMGregory points out, there are some very elegant ways to chain Coroutines to guarantee syncrhornization points at phases of computation (even with a large swarm of "virtually seperate threads" of execution running in each gameObject.

I encorage folks to read: http://forum.unity3d.com/threads/the-truth-about-fixedupdate.231637/

as well as: How to not freeze the main thread in Unity?

By using Update() and waitForEndOfFrame() for my tcp work and putting the rest in co-routines and lastDone, I can effecting seperate the 4 phases the computation and get about 1500FPS of externally clocked simulation, SWIL work done. I am impressed.

public class Tools
{
    public static string timeStamp()
    {
        return string.Format("{0} - {1:H:mm:ss.ffffff}", Time.time, System.DateTime.Now);
    }
}

public class TargetScript : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(CoDo());
    }
    void Update()
    {
        Debug.Log(string.Format("{0}: {1} update: {2}", Time.frameCount, gameObject.name, Tools.timeStamp()));
    }
    IEnumerator CoDo()
    {
        while (true)
        {
            yield return null;
            Debug.Log(string.Format("{0}: {1} codo: {2}", Time.frameCount, gameObject.name, Tools.timeStamp()));
        }

    }
    void LateUpdate()
    {
        Debug.Log(string.Format("{0}: {1} late:   {2}", Time.frameCount, gameObject.name, Tools.timeStamp()));
    }
    IEnumerator Done()
    {
        while (true)
        {
            yield return new WaitForEndOfFrame();
            Debug.Log(string.Format("{0}: {1} done: {2}", Time.frameCount, gameObject.name, Tools.timeStamp()));
        }
    }
}
Dr.YSG
  • 179
  • 1
  • 8