Custom engine class: won't save to mission file
by Darragh Coy · in Torque Game Engine · 10/02/2007 (4:30 am) · 3 replies
Hi everyone. I'm just after getting the torque engine for the first time and I am currently working on a college project using it where I have to make a full game using the engine. I've chosen to do a simple RPG game and to start off I'm currently working on creating a generic character class that will serve as a basis for all characters controlled by the player and the computer.
The class itself is called 'Character' (its datablock is called 'CharacterData') and is derived directly from the torque player class 'Player'. I've got the engine recognising the class and I've written all the appropriate networking functions to read and write datablock/class state and this all works fine. I can also edit fields I added in to the class from the editor, so that's working too. The only problem I have is when I go to save the mission, the class state (for 'Character') is not kept when I go to reload the mission.
At the moment I am saving only a few simple integer fields but I want to get this basic functionality working before moving on to the next task. As I said I've implmented methods like packUpdate() etc.. in 'Character' and packData() etc. in the 'CharacterData' class.
Might there be anything I'm missing or something I'm doing wrong ? I can send the full source for this class to anyone who is interested in taking a look.
Thanks,
Darragh.
The class itself is called 'Character' (its datablock is called 'CharacterData') and is derived directly from the torque player class 'Player'. I've got the engine recognising the class and I've written all the appropriate networking functions to read and write datablock/class state and this all works fine. I can also edit fields I added in to the class from the editor, so that's working too. The only problem I have is when I go to save the mission, the class state (for 'Character') is not kept when I go to reload the mission.
At the moment I am saving only a few simple integer fields but I want to get this basic functionality working before moving on to the next task. As I said I've implmented methods like packUpdate() etc.. in 'Character' and packData() etc. in the 'CharacterData' class.
Might there be anything I'm missing or something I'm doing wrong ? I can send the full source for this class to anyone who is interested in taking a look.
Thanks,
Darragh.
#2
I'm after examining the save file itself (which I thought would be just binary) and as it turns out the information is being saved. I found this in the .mis file:
new Character() {
canSaveDynamicFields = "1";
Position = "0.895054 -277.538 196.554";
rotation = "1 0 0 0";
scale = "1 1 1";
dataBlock = "Character_Human";
attrib_strenght = "10"; // This was 5 but was changed in the editor to 10: so it was saved
attrib_speed = "5";
attrib_endurance = "5";
attrib_intelligence = "5";
attrib_willpower = "5";
};
It must be something to do which how I'm handling the onNewDataBlock() method. It must be replacing the value of '10' for attrib_strenght with the default value of '5' from the default 'Character_Human' datablock.
Hmm.. I'll have to look into that further.
10/03/2007 (2:08 am)
Yep, I've exposed all the fields in the initPersistFields() method both in the datablock 'CharacterData' and in the class itself ('Character'). I'm after examining the save file itself (which I thought would be just binary) and as it turns out the information is being saved. I found this in the .mis file:
new Character() {
canSaveDynamicFields = "1";
Position = "0.895054 -277.538 196.554";
rotation = "1 0 0 0";
scale = "1 1 1";
dataBlock = "Character_Human";
attrib_strenght = "10"; // This was 5 but was changed in the editor to 10: so it was saved
attrib_speed = "5";
attrib_endurance = "5";
attrib_intelligence = "5";
attrib_willpower = "5";
};
It must be something to do which how I'm handling the onNewDataBlock() method. It must be replacing the value of '10' for attrib_strenght with the default value of '5' from the default 'Character_Human' datablock.
Hmm.. I'll have to look into that further.
#3
(1) The object was being constructed as normal
(2) The object was given the attributes which were saved in the .mis file by the engine.
(3) onNewDataBlock() was then called and the code was overwriting the settings which were saved in the file with the attributes from the 'Character_Human' datablock.
The workaround is quite simple. I added in a new boolean variable readFromFile to the class Character which is set to false when an object is first constructed. After the onNewDataBlock() function is called this property is set to true. If the flag is true when onNewDataBlock() is first called then the values from the class datablock are ignored and it is assumed they were read from a file. The first time an object is made it will use the values from the datablock. When the object is saved the readFromFile flag will also be saved with it so that next time round it will ignore the default datablock settings and the values read from the file will be used. Pretty simple.
But.. It's a hack, and I don't like using hacks unless really neccessary. So if there is some 'proper' way of doing this then I'm all ears...
10/03/2007 (5:23 am)
I've managed to implement a workaround to the problem. What I found was happening is this:(1) The object was being constructed as normal
(2) The object was given the attributes which were saved in the .mis file by the engine.
(3) onNewDataBlock() was then called and the code was overwriting the settings which were saved in the file with the attributes from the 'Character_Human' datablock.
The workaround is quite simple. I added in a new boolean variable readFromFile to the class Character which is set to false when an object is first constructed. After the onNewDataBlock() function is called this property is set to true. If the flag is true when onNewDataBlock() is first called then the values from the class datablock are ignored and it is assumed they were read from a file. The first time an object is made it will use the values from the datablock. When the object is saved the readFromFile flag will also be saved with it so that next time round it will ignore the default datablock settings and the values read from the file will be used. Pretty simple.
But.. It's a hack, and I don't like using hacks unless really neccessary. So if there is some 'proper' way of doing this then I'm all ears...
Torque 3D Owner Robert Blanchet Jr.