Save function behaving oddly...
by Jonathan R Hopkins · in Technical Issues · 05/27/2009 (11:02 am) · 2 replies
I've searched these forums Google, the tutorials, TDN, and the references throughly and cant seem to find decent coverage on this problem - maybe someone can help me out.
I am using the most recent version of Torque Game Builder.
I am trying to get my game to save and load from a file, and I'm not sure what I'm doing wrong here. I have an object stored to a global variable called $controller that stores some of the key pieces of data needed for the game, I use the following script to save the game:
The resulting file stores a whole bunch of data about physics and what not, but what I really care about are the last 5 lines. BGM, bgm override, camPosition, and so forth. This is what the save file looks like:
Then it loads the save file using this script:
The problem is, when the script executes to load the game, it doesn't recognize the BGM (which should be a reference to an audio datablock), and it doesn't recognize any of the data in that load file. Now I have a feeling that the problem is being caused partially by the fact that there is no object name assigned in the save file, when it should be assigned to $controller. I'm not sure how to get it to do that.
Also, I'm guessing I incorrectly saved the BGM (audio datablock), do I have to save it as a string or something?
The game reads the camPosition as: b#p3 (where the p is actually the backwards "paragraph" p. Though I'm guessing that it doesn't actually read it from the $controller object, because the object never really gets assigned.
So... I have two questions:
1. What am I doing wrong here? How can I make it so I can save variables, objects, and datablocks to a file and then reload them without these weird errors and,
2. Is there any way I could do a similar operation by writing to and reading from a text file my self? (I.e. write "BGM" and then later "read line, BGM" - which would then be assigned a value.
Thank you for your time.
I am using the most recent version of Torque Game Builder.
I am trying to get my game to save and load from a file, and I'm not sure what I'm doing wrong here. I have an object stored to a global variable called $controller that stores some of the key pieces of data needed for the game, I use the following script to save the game:
function saveGame(%saveName)
{
if (checkDatablock(%saveName) == true)
{
$controller.saveName = %saveName;
}
%savefilepath = "./" @ $controller.saveName @ ".cs";
$controller.save(%savefilepath);
echo("Game Saved as: " @ %savefilepath);
}The resulting file stores a whole bunch of data about physics and what not, but what I really care about are the last 5 lines. BGM, bgm override, camPosition, and so forth. This is what the save file looks like:
//--- OBJECT WRITE BEGIN ---
new t2dSceneObject() {
Enabled = "1";
canSaveDynamicFields = "1";
useMouseEvents = "0";
Paused = "0";
Visible = "1";
Lifetime = "0";
Position = "0.000 0.000";
size = "10.000 10.000";
Rotation = "0";
AutoRotation = "0";
FlipX = "0";
FlipY = "0";
SortPoint = "0.000 0.000";
Layer = "0";
GraphGroup = "0";
MountRotation = "0";
AutoMountRotation = "0";
WorldLimitMode = "OFF";
WorldLimitMin = "0.000 0.000";
WorldLimitMax = "0.000 0.000";
WorldLimitCallback = "0";
CollisionActiveSend = "0";
CollisionActiveReceive = "0";
CollisionPhysicsSend = "1";
CollisionPhysicsReceive = "1";
CollisionGroups = "-1";
CollisionLayers = "-1";
CollisionCallback = "0";
CollisionPolyScale = "1.000 1.000";
CollisionCircleScale = "1";
CollisionMaxIterations = "3";
CollisionDetectionMode = "POLYGON";
CollisionResponseMode = "CLAMP";
CollisionCircleSuperscribed = "1";
CollisionPolyList = "-1.000 -1.000 1.000 -1.000 1.000 1.000 -1.000 1.000";
BlendingEnabled = "1";
SrcBlendFactor = "SRC_ALPHA";
DstBlendFactor = "ONE_MINUS_SRC_ALPHA";
BlendColor = "1 1 1 1";
ConstantForce = "0.000 0.000";
ConstantForceGravitic = "0";
ForceScale = "1";
Immovable = "0";
ForwardOnly = "0";
AutoMassInertia = "1";
Density = "0.01";
Friction = "0.3";
Restitution = "1";
damping = "0";
LinearVelocity = "0.000 0.000";
AngularVelocity = "0";
MinLinearVelocity = "0";
MaxLinearVelocity = "10000";
MinAngularVelocity = "0";
MaxAngularVelocity = "10000";
BGM = "-2147483646";
bgmOverride = "0";
camPosition = "2";
saveName = "defaultSave";
vocalOverride = "0";
};
//--- OBJECT WRITE END ---Then it loads the save file using this script:
function loadGame(%saveName)
{
if (checkDatablock(%saveName) == true)
{
$controller.saveName = %saveName;
}
%loadfilepath = "./" @ "defaultSave" @ ".cs";
echo("Loading file: " @ expandFilename(%loadfilepath));
exec(%loadfilepath);
echo("Loaded game: " @ %savefilepath);
}The problem is, when the script executes to load the game, it doesn't recognize the BGM (which should be a reference to an audio datablock), and it doesn't recognize any of the data in that load file. Now I have a feeling that the problem is being caused partially by the fact that there is no object name assigned in the save file, when it should be assigned to $controller. I'm not sure how to get it to do that.
Also, I'm guessing I incorrectly saved the BGM (audio datablock), do I have to save it as a string or something?
The game reads the camPosition as: b#p3 (where the p is actually the backwards "paragraph" p. Though I'm guessing that it doesn't actually read it from the $controller object, because the object never really gets assigned.
So... I have two questions:
1. What am I doing wrong here? How can I make it so I can save variables, objects, and datablocks to a file and then reload them without these weird errors and,
2. Is there any way I could do a similar operation by writing to and reading from a text file my self? (I.e. write "BGM" and then later "read line, BGM" - which would then be assigned a value.
Thank you for your time.
#2
Then, when I loaded it from a saved game, I have to assign the ID to the variable it was supposed to be attached to:
Thank you!
05/31/2009 (7:15 am)
Thanks for the advice! The problem was solved by something along those lines. I had to assign the object an actual name (because, as I found out, the variable is now actually the object name, but a container holding its id) by adding the following to the initialization of the object:$controller.setName(controller);
Then, when I loaded it from a saved game, I have to assign the ID to the variable it was supposed to be attached to:
$controller = controller.getID();
Thank you!
Associate Rene Damm
For the loading to work, you need to have object references to resolve by name. Your dynamic field BGM simply has its string contents dumped which is the audio datablock's transient numeric ID. Either make sure to use named-based resolving by having BGM contain the object name rather than its ID or add a second field BGMName that contains the audio datablock's name and then resolve the name in a post-load step.
2.
Sure, look at FileObject.