Jump to content

PSA: Don't use normal classes inside XComGameState_BaseObject subclass


kosmo111

Recommended Posts

So I just ran into a very strange bug and I figured I would share it here so others might avoid the issue.

 

The TLDR is that regular class member variables of GameState objects will persist fine during gameplay-- but will not survive being persisted to the save file. Loading a game does not load the object.

=============================================

 

My mod LifetimeStats created a new XComGameState_BaseObject: XComGameState_LifetimeStats_Unit which was added as a component to XComGameState_Unit for all XCom soldiers. In a recent update, i switched from storing the stats of the soldier from primitives (ints and floats) to a helper class: LifetimeStats.uc

 

LifetimeStats was a regular UnrealScript class. I had assumed that any fields of a XComGameState_BaseObject subclass would persist across saves and while I was testing it everything seemed to work. I would take shots and the shown values would update. I never bothered to test taking some shots, saving and then loading the game-- which in this case causes the data to be lost.

 

I have since changed the way I am storing the data to instead have even the LifetimeStats object extend XComGameState_BaseObject. Like many other base classes however, I do not store the actual objects-- I store StateObjectReference's to them.

 

To show this in a bit more detail, this is how the class used to be (and it worked fine)

class XComGameState_LifetimeStats_Unit extends XComGameState_BaseObject config (LifetimeStats);

var int numShots;
var int numHits;
var int numMisses;
...

Here is how it was when it was broken:

class XComGameState_LifetimeStats_Unit extends XComGameState_BaseObject config (LifetimeStats);

var LifetimeStats stats;
...

//In LifetimeStats.uc
class LifetimeStats extends Object;

var int numShots;
var int numHits;
var int numMisses;

And here is how it is now:

class XComGameState_LifetimeStats_Unit extends XComGameState_BaseObject config (LifetimeStats);

var StateObjectReference mainStatsRef;
...

//In LifetimeStats.uc
class LifetimeStats_Stats extends XComGameState_BaseObject;

var int numShots;
var int numHits;
var int numMisses;
...

This was difficult to write and I'm sure its a bit confusing but I hope I got my point across. Let me know if you want me to describe the issue further or if anyone knows specifically why this failed.

 

Thanks

Edited by kosmo111
Link to comment
Share on other sites

I think you misunderstand-- the original object is a XComGameState_BaseObject and is added to XComGameState_Unit as a component object. What I'm trying to show here is that when using Component objects, it is not safe to use normal UnrealScript classes inside of the Component object.

Link to comment
Share on other sites

Hmm.. well, I guess maybe you can't serialize object variable inside a game state object. Thanks for letting us know.

 

Firaxis code does seem to use a lot of StateObjectReference links through, so I guess it's how its suppose to be done then.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...