function processSaveXML(event:DataPass):void {
var outputString;
for each (var dataField.XML in event.datPass) {
outputString = dataField.dataTitle;
InGameSettings.sfSaveData.data[outputString] = dataField.dataContent;
// the trick is here. You need to refer "data" of the SO, and then add
// what's in brackets to make a dynamic property.
InGameSettings.sfSaveData.flush();
} // for each loop
} // function processSaveXML
Assigning Data to a Shared Object using a Variable's content as the identifier
-
04-04-2022 - |
Question
I have been musing over this problem for the past few days, and whilst I can find other people that have had issues that kind of revolve around the use of dynamic variable names, none of the solutions quite work for what I am trying to do.
I have been using a shared object (for this purposed name sfSaveData) to create a local save file. This same system has been used quite happily across several projects, but the code isn't overly portable. That is to say that for each project, I have to completely change the class file to incorporate each of the settings/data to be stored. So I have this brainwave - rather than manually changing the class that creates/updates the local save file, I will put the necessary changes in an XML file, and have a class that loops through the XML and creates the necessary data within the shared object.
I have sussed out the XML side of things, my problem is getting the results to correctly reference the shared object. Here's what I have that is working properly:
For completeness, this is an extract from the XML file, at the moment to get this working I am only working with this one entry in the file (no point in adding in more data until it's working)
<dataField label = "dataField">
<dataTitle label = "dataTitle">locationArray</dataTitle>
<dataType label = "dataType">Array</dataLabel>
<dataContent label = "dataContent">[true, true, true, true, true]</dataContent>
</dataField>
NOTES: DataPass is a custom event which allows the passing of data with the event. In this instance it passes a variable containing the XML content. The data is accessible via the variable event.datPass. This is transferring correctly.
This is in a class file (SaveDataHandler) and the file which contains the shared object is InGameSettings. SaveDataHandler is created as an instance. InGameSettings is a Public Class filled with Public Static Vars (including sfSaveData:SharedObject). And for what it is worth, the folder structure is /data/SaveDataHandler and /setting/InGameSettings.
function processSaveXML(event:DataPass):void {
var outputString;
for each (var dataField.XML in event.datPass) {
outputString = (InGameSettings.sfSaveData.data." + dataField.dataTitle);
/*
* This is where it all goes wrong ;)
* To access outputString's content as the variable name, I use "this".
* I realise that this causes outputString to relate to the SaveDataHandler
* class rather than the InGameSettings class - which is part of the problem
*/
this[outputString] = dataField.dataContent;
InGameSettings.sfSaveData.flush();
} // for each loop
} // function processSaveXML
Now if I manually type in one of the variable names - for instance
InGameSettings.sfSaveData.LocationArray = dataField.dataContent
instead of
this[outputString] = dataField.dataContent
It works fine. My problem is that I don't know how to get the content of outputString to be used as the identifier for the variable on a different class. What I get is the error message:
ReferenceError: Error #1056: Cannot create property InGameSettings.sfSaveData.data.locationArray on data.SaveDataHandler.
So, if you can point me in the right direction, I'd be most appreciative. I've lost a lot of hair this week because of this, but I really want to get this working to neaten up a nasty hack in my code where all of this is hard coded into the SaveDataHandler class.
Solution