basic save/load code
<div class="IPBDescription">for testing or use in your singleplayer mod</div>As you may know I'm currently trying to write a recording plugin for NS2. Since you have to save multiple world states for that, practically as a by-product I now completed the code that allowes you to save and load the state of the game. You may simply try it out or try to merge it into your own code. I'm especially thinking about the guy who proposed writing a singleplayer mod here. If you happen to find any bugs while testing this, please tell me! If your game happens to crash when trying to load, please don't send the crash report as it will be no use for UWE. (But have no fear: my code didn't make the game crash in the last few weeks, that was more like a child illness)
<a href="http://www.file-upload.net/download-3559166/record.zip.html" target="_blank">download link</a>
<b><!--coloro:#FFFF00--><span style="color:#FFFF00"><!--/coloro-->install for testing<!--colorc--></span><!--/colorc--></b>
Simply extract the content of the downloaded .zip file into your Natural Selection 2 main folder. By default it is found in
C:\Program Files (x86)\Steam\steamapps\common\natural selection 2
It doesn't overwrite any of your game files, so for uninstalling simply delete the folder "record" in your NS2 main folder.
To launch the game with my code, add "-game record" (without the quotes) at the end of the launch command. There are 2 ways to accomplish that:
1. create a desktop shortcut for NS2, right-click-->properties and add "-game record" (without the quotes) at the end of the path field.
2. In Steam do right-click on Natural Selection 2-->properties-->set launch options, write "-game record" (without the quotes) in the text field, click OK
<b><!--coloro:#FFFF00--><span style="color:#FFFF00"><!--/coloro-->how to use<!--colorc--></span><!--/colorc--></b>
First you have to create a local game. It adds 2 console commands to the game:
<i>save <name></i> saves the current game state to into record\<name>.sav
<i>load <name></i> loads the game state from record\<name>.sav. I didn't code to load the map the game was saved on automatically, so the command will only work if you created a game on the same map the state was saved in first. There is already a test.sav in the zip file. It has been saved on rockdown and contains some structures and a fellow bot player.
Theoretically, it will work with more than one player, too. However I based the "Who takes control over which player after loading?" on the client-index (a number counting up from 1 that is assigned to each client), so you can load a game another guy gave you, which would not be possible if I would have based that on Steam ids. The point is that if you have people connecting and disconnecting between saving and loading, clients might take the control over a different player after loading. So I don't recommend trying it out in multiplayer, if you do it anyway expect the unexpected. As long as you do it with only you on the server, there should be no problem as you would always have the index 1.
You could test it on a dedicated server, too, but I would recommend that even less because whoever connects first on the empty server would have access to the save and load command unless you edit the ConsoleCommands_Server.lua to not allow that for client 1, only nil client. (would mean that the command was put in the admin web interface).
On loading the game will add a bot for each player that was there but doesn't have a client to controll him.
<b><!--coloro:#FFFF00--><span style="color:#FFFF00"><!--/coloro-->how to merge into your mod code<!--colorc--></span><!--/colorc--></b>
You can do so by
1. copy all files that don't exist in your mod folder from the record folder
2. with a text editor (or Decoda) copy the code for files that already exist in your mod folder from the lua files in the record folder and paste at the end of your own files. You should leave out the Script.Load('../ns2/<original NS2 file>.lua') lines, as you will either already have these lines in your own code or you don't need them because you copy-pasted or rewrote original NS2 code or whatever.
The only exception is my Shared.lua: The code in there MUST be run before anything else is loaded, so you have to paste the code in there at the beginning of your own Shared.lua. Don't leave out the Script.Load(...) lines in there except the one that is pointing to the original NS2 Shared.lua.
That's how it should work in theory but I can't be sure because I didn't test this with any mod. And it may have to be edited after merging in several cases. E.g. if your mod doesn't contain techtrees or resources, you should delete the lines refering to that. And you may have to put in code for things you introduced in your mod that may require a special treatment like some entities in NS2.
<a href="http://www.file-upload.net/download-3559166/record.zip.html" target="_blank">download link</a>
<b><!--coloro:#FFFF00--><span style="color:#FFFF00"><!--/coloro-->install for testing<!--colorc--></span><!--/colorc--></b>
Simply extract the content of the downloaded .zip file into your Natural Selection 2 main folder. By default it is found in
C:\Program Files (x86)\Steam\steamapps\common\natural selection 2
It doesn't overwrite any of your game files, so for uninstalling simply delete the folder "record" in your NS2 main folder.
To launch the game with my code, add "-game record" (without the quotes) at the end of the launch command. There are 2 ways to accomplish that:
1. create a desktop shortcut for NS2, right-click-->properties and add "-game record" (without the quotes) at the end of the path field.
2. In Steam do right-click on Natural Selection 2-->properties-->set launch options, write "-game record" (without the quotes) in the text field, click OK
<b><!--coloro:#FFFF00--><span style="color:#FFFF00"><!--/coloro-->how to use<!--colorc--></span><!--/colorc--></b>
First you have to create a local game. It adds 2 console commands to the game:
<i>save <name></i> saves the current game state to into record\<name>.sav
<i>load <name></i> loads the game state from record\<name>.sav. I didn't code to load the map the game was saved on automatically, so the command will only work if you created a game on the same map the state was saved in first. There is already a test.sav in the zip file. It has been saved on rockdown and contains some structures and a fellow bot player.
Theoretically, it will work with more than one player, too. However I based the "Who takes control over which player after loading?" on the client-index (a number counting up from 1 that is assigned to each client), so you can load a game another guy gave you, which would not be possible if I would have based that on Steam ids. The point is that if you have people connecting and disconnecting between saving and loading, clients might take the control over a different player after loading. So I don't recommend trying it out in multiplayer, if you do it anyway expect the unexpected. As long as you do it with only you on the server, there should be no problem as you would always have the index 1.
You could test it on a dedicated server, too, but I would recommend that even less because whoever connects first on the empty server would have access to the save and load command unless you edit the ConsoleCommands_Server.lua to not allow that for client 1, only nil client. (would mean that the command was put in the admin web interface).
On loading the game will add a bot for each player that was there but doesn't have a client to controll him.
<b><!--coloro:#FFFF00--><span style="color:#FFFF00"><!--/coloro-->how to merge into your mod code<!--colorc--></span><!--/colorc--></b>
You can do so by
1. copy all files that don't exist in your mod folder from the record folder
2. with a text editor (or Decoda) copy the code for files that already exist in your mod folder from the lua files in the record folder and paste at the end of your own files. You should leave out the Script.Load('../ns2/<original NS2 file>.lua') lines, as you will either already have these lines in your own code or you don't need them because you copy-pasted or rewrote original NS2 code or whatever.
The only exception is my Shared.lua: The code in there MUST be run before anything else is loaded, so you have to paste the code in there at the beginning of your own Shared.lua. Don't leave out the Script.Load(...) lines in there except the one that is pointing to the original NS2 Shared.lua.
That's how it should work in theory but I can't be sure because I didn't test this with any mod. And it may have to be edited after merging in several cases. E.g. if your mod doesn't contain techtrees or resources, you should delete the lines refering to that. And you may have to put in code for things you introduced in your mod that may require a special treatment like some entities in NS2.
Comments
I dont know how good you plan to make it, but I want to suggest that when playing a recording, the person should be able to rewind and pause, and maybe also speed up everything. Pretty important for a recorder that you got control over how to play the recording imho :P
Good luck on your project! xD