Sign in to follow this  
MrAccident

Unity Game state: serializing cooperative threads?

Recommended Posts

I've been thinking lately about ways to serialize game state, and I began thinking about how this would work in a game using a script interface that supports cooperative threads. (Coroutines, or generators, or what have you.) Let's take a simple example and suppose we have a map with an object moving around it whose motion is controlled by a coroutine. Then say we want to save the game at some point, and be able to resume it later with the object picking up exactly where it left off. It seems to me that one option is to build a system in which cooperative threads can be serialized while they are suspended, and then revived at a later time with the same locals and resume point. The game could have a scheduler that manages all cooperative threads, and can serialize them to and from persistent memory along with all the other data that makes up the game's current state. In this way, the behavior of objects within the game can be scripted with cooperative threads without worrying about what happens if a save-and-reload occurs in the middle of their lifetimes; as long as the all the objects are brought back in the correct state, and the scheduler restores itself to the same state as before, things will work smoothly. Another option would be to just serialize your entire VM state, though that seems like an unnecessarily heavy operation. My question is, is this a feasible solution in general? Does anybody actually do this, or is it generally better to restructure your game so that all cooperative threads will terminate before game state is saved? (Or else make script writers operate under the assumption that their threads are not part of the game's persistent state.) I did a bit of searching, and found this thread, which suggests that Lua is capable of doing what I describe, but I'm still curious as to whether this seems like a good idea or not. It seems like it would have a few obvious benefits, including making it possible to write simple, natural entity scripts while basically hiding most serialization from the script interface. It would involve a bit more work on the back end, but I'm not seeing any significant reasons that this wouldn't be a worthwhile option. Any thoughts?

Share this post


Link to post
Share on other sites
Yes, it's entirely possible (and quite easy, actually) to do all this with Stackless Python.

The only real drawback, as far as I'm concerned, is performance. If you have some kind of simulation with thousands of agents, creating a microthread for each one is not a practical approach. But if you're only dealing with hundreds at a time, it's not an issue.

Share this post


Link to post
Share on other sites
Quote:

The only real drawback, as far as I'm concerned, is performance. If you have some kind of simulation with thousands of agents, creating a microthread for each one is not a practical approach. But if you're only dealing with hundreds at a time, it's not an issue.


Tell that to eve online. They use it because it is the most performance friendly way for them to manage all the thousands of agents in their game.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Tell that to eve online. They use it because it is the most performance friendly way for them to manage all the thousands of agents in their game.


No, it really isn't the most efficient. I posted benchmarks on this forum a while ago showing the overhead of running Stackless tasklets compared to plain function calls. I expect Project EVE, like any MMOG, is able to divide the workload between many servers.

Share this post


Link to post
Share on other sites
Quote:
Original post by drakostar
Yes, it's entirely possible (and quite easy, actually) to do all this with Stackless Python.

Do you have a reference for how to go about serialising each of the tasklet states?
Quote:
Original post by drakostar
No, it really isn't the most efficient. I posted benchmarks on this forum a while ago showing the overhead of running Stackless tasklets compared to plain function calls.

And do you have a link for this? I'm sure that cooperative threading in Python isn't ever going to be the most efficient way to do anything in performance terms, but it would be interesting to see how much you gain in simplicity compared to a more efficient approach. EVE appears to scale far better than other MMOGs so Stackless is doing something right.

Share this post


Link to post
Share on other sites
I researched this a little bit - since the whole cooperative thread thing feels like such a powerful programming metaphor - individual entities that are in fact sequential programs. Being able to save them off as pure state would be a really nice bonus.

These are links that I found related to the ocaml programming language. I can't really comment with very much insight unfortunately, since I am still only learning ocaml (I spend most of my time looking at the ffi and trying to work out the best way to map c++ types with ocaml types instead of profitably exploring ocaml).

There is a library extension that supports serializable delimited-continuations in ocaml. The delimiters operate as a check on the depth of the bytecode-stack frame that gets copied. Unfortunately it only works with ocaml bytecode and not native code (you would have to make use of native OS support for creating multiple os-based stack frame objects in order to support a true native code implementation). However being a statically typed language, you could probably expect that the ocaml bytecode will run faster than stackless python. I got as far as compiling and installing it - but have never really played with it -

links :-
continuation framework on top of which you could build coroutines

using serialization (to disk) of the continuations in a cgi application

Perhaps more interesting - since it is more lightweight, and implemented purely using ocaml language features , is lwt . It is a cooperative thread library built upon a monadic framework. I suspect that you might be able to use ocaml marshalling and channels with this, since ocaml marshalling supports serilization of closures and functions i believe. I've only installed it - and a basic contrived example works fine, using either the native or byte code compilers. I dont really have a good grasp on monads however and so cant pursue it as I would like to, or comment informatively.

I think i read that someone did make a serializing scheme for lua and coroutines - but it was an individual effort that didnt attract much interest. I've lost the link now i am afraid.

More generally, I would be woried about state that is bound up in the os layer (and which therefore cannot be easily be captured) and serialized since it doesnt percolate up to the language level. I am thinking things like network connection state etc. I suspect that these types of considerations which would need to be factored out of the serialization scheme would weigh heavily and dictate a lot of the application design.

Disclaimer - I am a bit new to all this - so if it sounds like i got something wrong - then I probably did!!
[edit]for clarity hopefully

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
Quote:
Original post by drakostar
Yes, it's entirely possible (and quite easy, actually) to do all this with Stackless Python.

Do you have a reference for how to go about serialising each of the tasklet states?

Unless I'm misunderstanding the question, this should cover everything:
Quote:
One of the main features of Stackless is its ability to pickle and unpickle tasklets. That means that a running program inside a tasklet can be persistently stored to a file or string. Later, it can be restored again and can continue to run at the point where it was previously halted.


Quote:
Quote:
Original post by drakostar
No, it really isn't the most efficient. I posted benchmarks on this forum a while ago showing the overhead of running Stackless tasklets compared to plain function calls.

And do you have a link for this?

Unfortunately I can't find it now (I'm *sure* I posted it on this forum), but my recollection is that not counting initialization time, running a series of 1000 or so tasklets (which just generated a few random numbers to waste CPU cycles) took 25% longer than calling a function 1000 times. Feel free to write your own benchmark and see. A more realistic test case would probably reduce that difference somewhat.

Quote:
I'm sure that cooperative threading in Python isn't ever going to be the most efficient way to do anything in performance terms, but it would be interesting to see how much you gain in simplicity compared to a more efficient approach. EVE appears to scale far better than other MMOGs so Stackless is doing something right.

Oh sure, Stackless would let you do really cool things like serialize an AI-controlled object and pass it off to another server with no interruption. I just happen to be in exactly the wrong situation where I can't use it, which is the only reason I complain about performance: developing a single-player game with a huge real-time simulation that should run decently on older hardware.

Share this post


Link to post
Share on other sites
Quote:
Original post by drakostar
Quote:
Original post by KulSeran
Tell that to eve online. They use it because it is the most performance friendly way for them to manage all the thousands of agents in their game.


No, it really isn't the most efficient. I posted benchmarks on this forum a while ago showing the overhead of running Stackless tasklets compared to plain function calls. I expect Project EVE, like any MMOG, is able to divide the workload between many servers.


Why would you compare tasklets to pure function calls? The first lets you do things that the second doesn't. A good performance comparison would be tasklets versus some other threading implementation.

Share this post


Link to post
Share on other sites
Tasklets aren't threads in the conventional sense. They're just a convenient method of maintaining an execution state. The *purpose* of tasklets in game AI is nothing like that of real OS threads, which of course would be absurdly inefficient.

Now that I go back and re-read that OGRE forum thread from two years ago, I believe my intention was to demonstrate that "context switching" between tasklets was somewhat expensive. Certainly more so than iterating through all your game objects and calling an AI routine would be.

Again, anyone's welcome to craft a more realistic benchmark based on different approaches to game/simulation AI. In Python, comparing Stackless tasklets to standard generators might be interesting.

Share this post


Link to post
Share on other sites
See I would think the tasklets would shine in the case of objects that don't need updates all the time. If the scheduler is even remotely smart, it could store a list of tasklets sorted by "time to wake up". (this would be wakes up at 1:23:41.023 instead of storing wakes up in 6.023 seconds)
Then all of a sudden all the random AI crap of something like eve (wich has TONNES of things that cycle in the 4-10sec range) becomes a much smaller compute problem because you aren't iterating over anything but the (hopefully) few items that need updates at this time.

I agree that tasklets have overhead much more so than just calling functions. But hopefully the wait-on-event semantics of a tasklet allow you to save a lot of compute time. (now you could roll your own signals/slots setup and manually set up a possibly faster event system, but from what I remember about stackless you can code normally and any function that is a tasklet interface automagically incorporates waits on event)

Share this post


Link to post
Share on other sites
A belated update, now that I've had time to test some ideas:

Generators in standard CPython are, most unfortunately, not pickle-able and therefore are probably unsuitable for microthread-like AI in games, because saving/loading would be nontrivial.

Anyway, I'm starting serious work on a game that will have a couple hundred agents to begin with, and I'm going to try to create multiple AI interfaces using Stackless Python 2.6: maybe one with tasklets, one with generators (which Stackless *can* pickle), and a more typical one that would be fully compatible with CPython. Hopefully I'll have some interesting results in the nearish future.

I was somewhat discouraged by this thoroughly unimaginative example, which sends every tasklet a "WORLD_STATE" tick and handles logic just as you'd expect. I'll try to do something more like Robocode.

Share this post


Link to post
Share on other sites
drakostar, I'm also looking into Stackless for agent cooperative threading. Have you any ideas on how to effectively take arbitrary files of Python code and attach them to individual agent instances? Examples like the Olsen link you posted assume you're writing everything in Python and that the agent script code is part of the same program as the Stackless system you use to manage them. I'd quite like to have a C++ host app which sets up the Stackless scheduler, and then loads in Python scripts, associating channels with them etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Quote:

The only real drawback, as far as I'm concerned, is performance. If you have some kind of simulation with thousands of agents, creating a microthread for each one is not a practical approach. But if you're only dealing with hundreds at a time, it's not an issue.


Tell that to eve online. They use it because it is the most performance friendly way for them to manage all the thousands of agents in their game.


Using EVE as a benchmark for how efficient Stackless is, is a terrible idea. Having been a long time EVE player, I can tell that EVE basically chokes when more than 200-300 players are in a system, attempting to fight. I've literally been in fights where the client to server lag was measured in hours, because the servers would choke because of all the Stackless microthreads they spawned.

Share this post


Link to post
Share on other sites
They recently changed their IO system however, so that problem may have alleviated somewhat.

I'm still hoping for a response from drakostar though. :) Turning arbitrary strings of Python into functions that can be run on a per-instance basis seems a bit tricky in Python (and trivial in Lua, irritatingly).

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
They recently changed their IO system however, so that problem may have alleviated somewhat.

I'm still hoping for a response from drakostar though. :) Turning arbitrary strings of Python into functions that can be run on a per-instance basis seems a bit tricky in Python (and trivial in Lua, irritatingly).


Yah, it simply moved the lag around. Instead of 45 minute load times, you get more consistent 3-5 minute load times, which, I guess is better overall, but it really just moved the problem around, it didn't alleviate it.

To be fair, EVE has much deeper problems than just Stackless Python. For instance, their network I/O is based entirely on TCP, and they send huge 8 meg mega-packets that are just stupid. If even a byte of that mega-packet gets lost, the whole thing is sent again. So you have to take the whole system in to account, but Stackless Python IS an issue in that environment.

Now, for a single player game, where you're not going to be managing 500 players, plus 5000 AI drones/fighters, in a single system, it's probably fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by drakostar
Woops, it was a different forum. Here is my test code.


Just ran your test on stackless 2.5 I get 17.1 for stackless and 14.4 for iterative

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627759
    • Total Posts
      2978961
  • Similar Content

    • By KARTHI
      Free software
       
      1. Lumberyard (Game engine) open-source and free tool
      Amazon Lumberyard is a free cross-platform triple-A game engine developed by Amazon and based on the architecture of Cry Engine, which was licensed from Crytek in 2015.
       
      2. Sculptris (sculpture tool) open-source and free tool
      Sculptris is a virtual sculpting software program, with a primary focus on the concept of modeling clay. It entered active development in early December 2009, and the most recent release was in 2011.
       
      3. Make human (game model creator) open-source and free tool
      Make human is an open-source 3D computer graphics software middleware designed for the prototyping of photorealistic humanoids. It is developed by a community of programmers, artists, and academics interested in 3D modeling of characters.
       
      4. Ipi soft (motion capture software) not free tool
      iPi Motion Capture is a scalable markerless motion capture software tool that supports 1 or 2 Kinect cameras or 3 to 6 Sony PlayStation Eye cameras to track a human action and convert it into a motion capture file
       
       5. Blender (Complete tool) for modeling, texturing and so on (open-source and free tool)
       Blender is a professional, free and open-source 3D computer graphics software toolset used for creating animated films, visual effects, art, 3D printed models, interactive 3D applications and video games.
                 
      6. Audacity (music editor) open-source and free tool
      Audacity is a free open source digital audio editor and recording computer software application, available for Windows, OS X, Linux and other operating systems.
                 
      7. Awesome bump (bump map editor) open-source and free tool (optional)
      Awesome Bump is a free program written using Qt library designed to generate normal, height, specular or ambient occlusion textures from a single image.
       
      8. Faceware (facial animation) not free tool
      Faceware Technologies is an American company that designs facial animation and motion capture technology. The company was established under Image Metrics and became its own company at the beginning of 2012.
       
      9. GIMP (image editing) open-source and free tool
      GIMP is a free and open-source raster graphics editor used for image retouching and editing, free-form drawing, converting between different image formats, and more specialized tasks. Through this you can also create bump maps
       
      10. Meshlab (mesh repair) open-source and free tool (Optional)
      MeshLab is an advanced 3D mesh processing software system that is oriented to the management and processing of unstructured large meshes and provides a set of tools for editing, cleaning, healing.
       
      11. LibreOffice (create documents) open-source and free tool
      LibreOffice is a free and open source office suite, a project of The Document Foundation. It was forked from OpenOffice.org in 2010, which was an open-sourced version of the earlier StarOffice.
                 
      12. Atom (coding software) open-source and free tool
      Atom is a free and open-source text and source code editor for macOS, Linux, and Microsoft Windows with support for plug-ins written in Node.js, and embedded Git Control, developed by GitHub.
                 
       
      Useful websites
      free image
                 
      Reference images will be found on Pinterest
       
      Free Sounds
       
      1.      Freesound.org
      2.      99Sounds.org
      3.      NoiseForFun.com
      4.      Incompetech.com
      5.      OpenGameArt.org
      6.      RaisedBeaches.com
      7.      Musopen.org
      8.      PlayonLoop.com
      9.      Bensound.com
      10.   SoundJay.com
      11.   Dig.ccmixter.org
      12.   Soundgator.com
      13.   Pacdv.com
      14.   Freesfx.co.uk
      15.   Soundtrack.imphenzia.com
      16.   Bxfr.net
       
      Download the free music tracks from these websites
       
      1. http://incompetech.com/music/
      2. http://dig.ccmixter.org/
      3. http://www.joshwoodward.com
      4. http://www.youtube.com/audiolibrary
       
      I hope this information will help full to you. I am got so stress to collect this data so don't waste it 
      🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
       
       
    • By rXpSwiss
      Hello,
      I am looking for a team to stop developing project alone and start doing bigger things.  I would like a team that is organized and serious even of it is just a hobby. I don't want a project to be dropped every week and the team to disband after 2 months. Even though it is a hobby the quality and complexity of the game should be on par with a game that would be released on a major platform.

      I have experience with UE4, Unity and making my own engine with OpenGL. I prefer UE4 for 3D and Unity for 2D but I am willing to work with both.
      Every one of the games I worked on (personal project) involve multiplayer and netcode, I used what UE4 provided but for unity I used Forge Remastered which gave me a bigger control on how to optimize everything.
      I play several instruments, know about game design and am able to do small modeling task but I am before everything a programmer.
      I play mostly competitive fighting games these days (SFV, MvCi, GG, T7) but before I started getting serious into these types of games it was mostly FPS (CS1.6, UT2k4, Quake 3) and Dota-like. As you can see I love competitive multiplayer games but I play solo games too. I have a preference for darker theme games(F.E.A.R, HL, Shin Megami Tensei/Persona, Last of Us, SH2, RE7) even though I play everything offering me a challenge or some peace (Don't Starve, Stardew Valley, Darkest Dungeon, Zelda etc).
      I do have several ideas for games with a very clear direction gameplay, marketing and art but I am not looking for a team to work on my ideas, I just have them if needed.
      Feel free to ask questions here or PM me if you prefer.
      Best regards,
      rXp
    • By KARTHI
      Please reply me about this
    • By KARTHI
      Currently I using makehuman to make a 3d models but I am not satisfied with that model. If any use this please reply about the quality and details of models. 
      🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
    • By KARTHI
      I planned to make a space game so I need a knowledge about 3d planet making so anyone help me to make it possible
      🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
  • Popular Now