Saving and Loading - LUA

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Saving and Loading - LUA

Post by Fiodis »

Ok, heavy LUA time! :D

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?
User avatar
Frisbeetarian
Jedi
Jedi
Posts: 1233
Joined: Wed Sep 12, 2007 3:13 pm

Re: Saving and Loading - LUA

Post 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.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Saving and Loading - LUA

Post 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.
mswf
Master Bounty Hunter
Master Bounty Hunter
Posts: 1674
Joined: Tue Mar 31, 2009 3:40 pm
Location: Twello, The Netherlands
Contact:

Re: Saving and Loading - LUA

Post 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.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: Saving and Loading - LUA

Post 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?
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Saving and Loading - LUA

Post 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.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: Saving and Loading - LUA

Post 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.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Saving and Loading - LUA

Post 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.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: Saving and Loading - LUA

Post by Fiodis »

There is a SetProperty function. There isn't a FindProperty?
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Saving and Loading - LUA

Post by [RDH]Zerted »

FindEntityClass() and FindTimer(), but no FindProperty() or something like it.
User avatar
Frisbeetarian
Jedi
Jedi
Posts: 1233
Joined: Wed Sep 12, 2007 3:13 pm

Re: Saving and Loading - LUA

Post 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.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: Saving and Loading - LUA

Post 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?
User avatar
Frisbeetarian
Jedi
Jedi
Posts: 1233
Joined: Wed Sep 12, 2007 3:13 pm

Re: Saving and Loading - LUA

Post 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.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Saving and Loading - LUA

Post 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.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: Saving and Loading - LUA

Post 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?
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Saving and Loading - LUA

Post 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.
User avatar
Frisbeetarian
Jedi
Jedi
Posts: 1233
Joined: Wed Sep 12, 2007 3:13 pm

Re: Saving and Loading - LUA

Post 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)
Post Reply