Jump to content

AI and saved games


Recommended Posts

I committed some support for saving games with AI players. Testing would be helpful - hopefully it will be able to save and load any game containing AI players without triggering any warnings/errors, and the AI will continue running after loading. (It won't run exactly the same as if you hadn't saved/loaded, though - effectively the AI is restarted from scratch after loading, as if it was starting a new game, so it will forget all of its plans. This also means it won't work when rejoining an in-progress multiplayer match that includes AI players - you'll get an out-of-sync error - but it should be okay in single-player.)

Technical details:

When saving, the engine calls .Serialize() on each AI script object. That method must return some object (consisting of plain (not class-based) objects, arrays, strings, numbers, etc) which will get stored in the saved game file.

When loading, the engine constructs the AI script object like at the start of a game. Then it calls .Deserialize(data), passing it the object that was returned by the call to .Serialize(). Deserialize is responsible for resetting the AI script object based on that data.

Currently BaseAI implements Serialize/Deserialize, so it remembers the important state like _rawEntities (things will break if we don't store that). If a particular AI script wanted to save its own state, instead of restarting from scratch after loading, I guess it might be reasonable for it to just reimplement Serialize to do whatever it fancies (including a BaseAI.prototype.Serialize.call(this) to let BaseAI deal with the _rawEntities stuff), though maybe we ought to extend the serializer so it can cope with more interesting data types instead of forcing you to convert everything into plain objects/arrays/strings/numbers if that's a pain. Hopefully the current state is good enough for the next alpha release, though :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...