Sign in to follow this  

Unity Game state: serializing cooperative threads?

This topic is 3372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

This topic is 3372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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  

  • Similar Content

    • By Strifexxx
      Hi guys, check out our new game about sticks. Feedback welcome!
      Download on Google Play: https://play.google.com/store/apps/details?id=com.cyberpony.Stickman.RacerRoadDraw
      Download on Appstore: https://itunes.apple.com/app/id1334280717
      Youtube: https://www.youtube.com/watch?v=vnJfF0pQr5c
      Stickman Racer Road Draw is a sequel to the legendary game of survival, where to make incredible tricks, driving different transport and getting into different crash! The game is made in the best traditions of simulator games with ragdoll physics elements. Make incredible jumps and tricks and destroy the enemy! Your task is make the finish alive or dead! 










      Website: http://strifexxx.wixsite.com/cyberpony
      Facebook: https://www.facebook.com/groups/577850349043463/
      VK: https://vk.com/club122273704
    • By Redlime
      I am a game development student working on a 2.5D beat 'em up brawler game in Unity as my final year project and I've been working on the ai for the enemies in the game. However, I've been unable to get the enemies to working properly as the enemies would be constantly stuttering and jerking while trying to follow the player. I've created a foreach statement and added some conditions that call for the robots to spread out and stay away from the player if it is not in an engaging enum state. This has been an issue plaguing the ai for a while now, analysing the animator, it seems like the robot's movement is instantaneously stopping and going which might be the reason causing this stuttering. But I'm not sure how to prevent the robot's movement from stopping and going. If anyone knows the reason why this is happening to the robots it would be much appreciated as my project is due soon and I'm still unable to fix such a game breaking bug! I've included a few videos below showing what is happening to my robots and also the stuttering shown inside the animator. I've also included the full script for my robots.
       
      Robots Stuttering
      Robots Stuttering Animator
      Robot.cs
    • By red1m3n
      Hi I'm a hobbyist getting together a team to pursue game creation, we'll do both 2D and 3D games, we would definitely build different types of games, and hopefully have a great time doing it. you dont have to have much experience, just enough that you think you can fill the position. I have plenty of ideas for games that I'd love your help with, and am completely open to fulfilling your game ideas . As of right now this is a for fun project thing, but if me and my team are satisfied with our work it can be published to steam or other platforms. 
      The positions I'm currently looking to fill are:
      2D Pixel artist
      3D Modeler 
      C# programmer
      Musician/Sound engineer
      I am currently working on a project it is a 2D Merchant adventure game with survival elements. It does not have a name yet, that's because I'm bad with names. 
       
      If interested please feel free to leave me a PM or email at dealtavu@gmail.com so we can talk more about it..
    • By Aggrojag
      Hello!
      I'm working on a game that is quite personal to me. It touches on my own depression, obsessions, phobias, etc. It's a narrative driven dark comedy with some small aspects of platforming and puzzle solving. The project is rather small as well. It touches on topics such as suicide, mental illness, family, corruption, free-will, and redemption.
      First, I'm looking for a 2D animator that can compliment the style of the attached image. The player character's animations will be a bit more extensive than a typical platformer. There will be many death animations, and some other strange dealings with the character that will need animation. Other items will include some objects in the world that are in need of animation. All static pieces of art will be handled by the artist that drew the attached image.
      Second, I'm looking for a male voice actor. There will be at minimum two characters that will need to be brought to life through vocals. They are a very core part of the game, though they will never be shown on screen. As a reminder, this is a rather dark comedy, so be prepared for some insanity in your portrayal of these characters. The first voice is that of a game show host's voice, while the other will be a devilish voice (without SFX, I think it cheapens it, does not necessarily need to be a low voice, this is more about the portrayal than how the voice sounds).
      Last, I would love to have an editor on board. More than just avoiding disaster in the writing, I'm looking for someone that can elevate it. There is not a ton of writing that will be in the final project, but it's important enough of an aspect to be trying to bring someone in to help with it.
      To those interested:
      I have a crappy prototype that exists, along with a couple game design docs. More than anything, I'd like to present the project through a voice chat via discord to any interested. Discord - Bobdul Thundercuck#4590
      I do plan to post this on itch.io and wherever else I can put it that makes sense. I fully expect to make nothing off this project, as should you. However, in the case that money is made, a rev share model will be in place.

       
      Edit: Editor position has been filled, title has been changed to reflect this, and crossed out the section detailing this.
    • By William Edmeades
      Hey guys and girls,   Have you ever found yourself trying to apply for a job to be a game developer or a game artist? Maybe a sound engineer or a script writer? Only to not even get an interview due to lack of ‘experience’, teamwork projects or released game titles you have worked on? I have for sure. The game industry is hard and competitive to get into for someone new, especially when you might have a full time job or even have to support a family. So I have come up with an idea and I wanted to throw something out there to see if people could be interested in such an idea.   What if you could be part of a virtual game development company? A place where you can work on projects with a team of people from the internet, soon to be known as your colleagues, which could be from anywhere in the world all working at your own pace when you have time to do as little or as much as you want.   The idea is we can create the power of a company and release titles together. Not only do you then have a released game title you have worked on under your resume, but also work experience, team work skills, and the power of helping each other and learning from each other.    My dream idea is to create a solid place for people to join a party and develop ideas and create games and level up it experience points.   As far as money is concerned, the games would either be released for free or any money would go towards charity, it’s not about making money, it’s about building a career or justness having fun building games and socialising with like minded people.    I want to make something amazing, I think it would benefit me, and I think others who are serious would benefit greatly too.   I await your critasism on the ‘prototype’ idea, I just wanted to see how many people would be interested in such a concept. And if you are very much into the idea, let’s make it happen.   Thanks,   Will. www.feelingterrible.com
  • Popular Now