- Home /
Convert class of int vars into an array
I need to save a few instances of classes of int variables to PlayerPrefs, and was thinking a more efficient way would be to save each class instance as an array through using PlayerPrefsX (which facilitates saving of arrays).
Is there some way to convert a class to an array and vice versa? What I need is some way to step through a class one variable at a time.
Example:
class StatsClass
{
var accuracy = 0;
var clipSize = 0;
var reloadSpeed = 0;
var strength = 0;
var accuracyTotal = 0;
var clipSizeTotal = 0;
var reloadSpeedTotal = 0;
var strengthTotal = 0;
}
var pistol : StatsClass;
function Awake()
{
var pistolArray = PlayerPrefsX.GetIntArray ( "pistolStats" );
// Convert saved array into class values
for ( var i = 0; i < StatsClass.length; i++; )
{
pistol.StatsClass[i] = pistolArray[i];
}
}
Comment
I don't think there is anything that does exactly what you're asking.
It should be easy enough to create a pair of methods to create the array and repopulate the class from the array.
Seems to work alright. Thanks $$anonymous$$ark. Post it as an answer so I can choose it.