kosmo111 Posted March 14, 2016 Share Posted March 14, 2016 (edited) 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 March 14, 2016 by kosmo111 Link to comment Share on other sites More sharing options...
davidlallen Posted March 14, 2016 Share Posted March 14, 2016 I am not an expert, but I am surprised that your ints and floats got serialized. Most people have used this approach linked from the main pinned resource thread:http://forums.nexusmods.com/index.php?/topic/3820875-tutorial-using-gamestate-components/Be sure to read the subsequent linked article about how to free them also. Link to comment Share on other sites More sharing options...
kosmo111 Posted March 14, 2016 Author Share Posted March 14, 2016 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 More sharing options...
Lucubration Posted March 14, 2016 Share Posted March 14, 2016 Interesting, and not something I've had to try yet. Thanks for pointing it out. Link to comment Share on other sites More sharing options...
CaveRat Posted March 14, 2016 Share Posted March 14, 2016 Did you create instance of Lifetimestats class inside XcomGameState_Lifetimestats_Unit? (i.e. using "new" keyword) Link to comment Share on other sites More sharing options...
kosmo111 Posted March 14, 2016 Author Share Posted March 14, 2016 Correct. During initialization I would do the following: function XComGameState_LifetimeStats_Unit InitComponent() { stats = new class'LifetimeStats'; stats.initComponent("Main"); return self; } Link to comment Share on other sites More sharing options...
CaveRat Posted March 14, 2016 Share Posted March 14, 2016 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 More sharing options...
Recommended Posts