Return-To-The-Roots / s25client

Return To The Roots (Settlers II(R) Clone)
http://www.rttr.info
GNU General Public License v2.0
478 stars 77 forks source link

Add LUA save/load #421

Closed Flamefire closed 8 years ago

Flamefire commented 8 years ago

Out from #36:

Saving and loading of lua states should be possible.

Research shows, that automatic save/load is really problematic. So I'd add Save/Load callins to lua and make the script save its own state as required. Instead of OnStart, OnContinue can be called or a param be added to OnStart.

Details: Pass lua an object to Save/Load that can be used to Write/Read integers and strings. This is serialized via serializer and appended to the savegame. Should be pretty easy and keeps the savegame small (LUA VM state might be rather large...)

Spikeone commented 8 years ago

I had the idea that one could save variables and they are loaded, on start shouldn't be called on load. But objects that are placed might be a problem.

Flamefire commented 8 years ago

You need an onStart event to do some initializations (maybe set up some events or so) even on load Placed objects are not a problem. Those are already saved to the savegame

Flamefire commented 8 years ago

In preparation for this I rework the lua handling a bit to make it easier to extent. Very helpful is a template based C++ Wrapper: https://github.com/satoren/kaguya (One of the few that is very fast and supports C++98 and C++11)

Naturally, function calls should be calls to objects. So for example in C:

GameWorld::setVisibility(x, y, isVisible){ getNode(x,y).visible=isVisible; }

and lua would be:

rttr:setVisibility(x,y,1)

Note that this ain't static functions anymore. Everything is wrapped in a typesafe manner and error messages are quite helpful (printed stacktrace with type errors and suggestions for fixes) Further note: it is rttr:setVisibility(x,y,1) and not rttr.setVisibility(x,y,1) I'd even would do: rttr:world:setVisibility(x,y,1)/rttr:getWorld():setVisibility(x,y,1) (and rttr:getPlayer(1):disableBuilding(BLD_X)

This would obviously break the current API in favor for a much better flexibility. Would this be a huge problem? Especially asking @Spikeone and @Flow86

Spikeone commented 8 years ago

None of my scripts will work anymore but... that's the price one should pay for using unstable features.

So, it's just new work for me but that's okay (I hope I can just add a function variable or however it's called in Lua)

Flamefire commented 8 years ago

This is why I'm asking. I think the changes required are straight forward and fast to do at this point. When we implement the additions first and than change the interface it will probably be much more complicated. When the number of functions increases, the current API just seems not reasonable. Player-related stuff should be done on a player instance for example. Putting all of that into the main rttr static object would require lots of duplicate code to get the player each time such a function is called.

I'd update the wiki and add a short site about how functions need to be renamed.

Spikeone commented 8 years ago

I don't think we need a new page, just tell me what I have to change and update the Lua scripting page.