Page 1 of 1
Saving and Loading - LUA
Posted: Wed Jul 29, 2009 7:26 pm
by Fiodis
Ok, heavy LUA time!
I was wondering, what exactly does a metagame file save? This is on my mind because I recenty took a look at Zerted's Save and Load scripts I downloaded a while ago and forgot about, and I'm intrigued.
Is it possible to save information like details about a character's ODF (specifically, what weapons he has), or a player's ammo or health, and close BF2 and then have the information reloaded the next time you play the map?
Re: Saving and Loading - LUA
Posted: Wed Jul 29, 2009 7:43 pm
by Frisbeetarian
The data must be contained in a single table. Other then that, its up to the modder to decide the format of that table and what it contains.
Therefore, whatever information you can get through a Lua script, you can save.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 9:18 am
by [RDH]Zerted
Frisbeetarian wrote:...whatever information you can get through a Lua script, you can save.
Exactly right.
I don't know how you could get someone's ammo count.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 12:14 pm
by mswf
[RDH]Zerted wrote:Frisbeetarian wrote:...whatever information you can get through a Lua script, you can save.
Exactly right.
I don't know how you could get someone's ammo count.
Hypothetically speaking: Make a unit's ammo count 500 ingame and 500 in the script. If you fire once, you'll lose ammo ingame (like normal), but the script would count with you. Then, when you close the map, you can save the ammo count that was being recorded in the script.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 12:55 pm
by Fiodis
This has the problem of only being able to detect dispenser-type weapons being fired.
What about the health? Could you record that?
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 2:13 pm
by [RDH]Zerted
You can use GetObjectHealth() on a character's unit to get its health. If you're using pick-up-able weapons, then its easy to track what weapon the player has. If you aren't, then you know the weapon based off of the player's class.
It may even be possible to store the player's position (GetEntityMatrix()) then force spawn them right back where they left off (SetEntityMatrix()) when the level reloads. However, I don't think we ever determined what exactly a matrix is. It is possible that the ingame matrices aren't consistent from one reload to another. No one has tested that before.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 2:31 pm
by Fiodis
Thanks for that health tip. As for the matrices, I suppose I'll be the first....Isn't a matrix location and direction?
As for tracking what weapon the player has, couldn't you do something like FindObjectProperty to find which weapon is currently in the player's ODF? I'm looking at a pick-upable weapons system, and each player has their own unit ODF.
If I could use something along the lines of FindObjectProperty (or FindEntityProperty), then that would allow me to save GeometryName and maybe what skin the unit is using, as well.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 3:07 pm
by [RDH]Zerted
The thing about matrices is that they may be constructs from the C++ code. If what the Lua sees are only pointers to certain C++ objects, then those pointers may not be consistent between map loads. I don't think anyone has managed to create a matrix directly. We can only build one offset from another. This leads to the thinking that they are not directly something we can create/store/reread from the Lua.
Sadly, the developers didn't write a general GetPropertyValue() method. They only created getters/setters for the properties they needed access to. For your weapon system, just store the player's weapon each time they pick up a new one.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 3:11 pm
by Fiodis
There is a SetProperty function. There isn't a FindProperty?
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 3:55 pm
by [RDH]Zerted
FindEntityClass() and FindTimer(), but no FindProperty() or something like it.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 6:20 pm
by Frisbeetarian
[RDH]Zerted wrote:We can only build one offset from another.
Right
http://www.gametoast.com/forums/viewtop ... 89#p297189
But if you are loading in the same map, you can build the matrix off of something that doesn't move. The difference in two matrices is consistent between maps.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 7:25 pm
by Fiodis
So the CreateMatrix function takes your current matrix and makes a new one a distance away from it, and you can control the distance and rotation?
Hmm, that would make a good "warp" weapon, but with the disadvantage of being able to warp into objects....unless you placed a teleport region inside those objects to shove you out....
In any case, I plan on using the same map, I think, but that comment makes me curious. It's possible to load the same metagame file for different maps? If so, that would make things a lot easier for me.
And my original question: what is the definition of a metagame file? I couldn't find it through searching my computer. What file extension does it use?
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 7:48 pm
by Frisbeetarian
Fiodis wrote:Hmm, that would make a good "warp" weapon, but with the disadvantage of being able to warp into objects....unless you placed a teleport region inside those objects to shove you out....
The teleport function already uses matrices to work, what you're describing is just a way to do it without a destination node.
Look to see what type of file is saved when you save a campaign or GC; Zerted's process uses the same method. The file basically just saves a table. There's no reason different maps can't access the same table.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 9:12 pm
by [RDH]Zerted
Just use the same file name when attempting to load the data in the other map. The saved files aren't in a human readable format. I'm going off memory so I could be wrong, but I think their extension is .gc. They are stored in the same folder as your player profiles.
@Frisbeetarian: Yeah, but there is no way to get that matrix offset in order to save it. We can store the result from GetEntityMatrix(), but that just looks like userdata: 059CE698. I don't think loading that into SetEntityMatrix() will work. But, I've never tried it.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 9:34 pm
by Fiodis
Ah, I've found them. You're right, Zerted, it is .gc.
So about the whole saving matrix thing: it's not possible to save a player's location?
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 9:42 pm
by [RDH]Zerted
I've never tried it. However, it is very easy to test. Start a timer. Run around a bit ingame. When the timer is up, grab the player's unit's matrix and save it using the saving script. Edit then reload the map. This time when the timer is up, attempt to load the saved data and teleport the player to the saved matrix. I expect you will get an invalid argument #1 in the debug log.
I don't have swbf2 installed on this computer so I can't test it. If you do test it, post the error message if there is one.
Re: Saving and Loading - LUA
Posted: Thu Jul 30, 2009 9:44 pm
by Frisbeetarian
[RDH]Zerted wrote:Yeah, but there is no way to get that matrix offset in order to save it.
Woops, didn't really think that through.
GetWorldPosition() could be used though. The x, y, and z from that should translate to the same thing in CreateMatrix(). You just get the position of the unit and a stationary object, subtract, and save.
From ifs_freeform_fleet:
Code: Select all
local x, y, z = GetWorldPosition(planet)