Simulating Classes
by Jack Reed · in Torque Game Builder · 11/28/2006 (7:55 pm) · 13 replies
I am having one heck of a time trying to find some way simulate classes as used in C++.
Basically I have a game where a party of characters is represented in the game world by one object until combat is initiated, then there is an object for each member of the party (basic RPG stuff).
What I want to do is have a class with all the properties of a character so I can create an instance of this class for each character in the party, so I don't have to redo all the properties for each one. I have attempted to use datablocks and ScriptOjbect, but I am used to C++ and I just don't know where to look for documentation that explains Torque Script enough for me to utiliize it effectively.
My attempts would create the object, but none of the properites or their default values were available.
Can someone please give me an example of how to accomplish this or point me in the right direction? I have seen a few threads on classes, but they don't really seem to do the trick. I am not needed to make them part of a sprite. I just need to be able to use the character objects in script for loading the correct images and base stats in my character screen GUI.
Thanks in advance!
Basically I have a game where a party of characters is represented in the game world by one object until combat is initiated, then there is an object for each member of the party (basic RPG stuff).
What I want to do is have a class with all the properties of a character so I can create an instance of this class for each character in the party, so I don't have to redo all the properties for each one. I have attempted to use datablocks and ScriptOjbect, but I am used to C++ and I just don't know where to look for documentation that explains Torque Script enough for me to utiliize it effectively.
My attempts would create the object, but none of the properites or their default values were available.
Can someone please give me an example of how to accomplish this or point me in the right direction? I have seen a few threads on classes, but they don't really seem to do the trick. I am not needed to make them part of a sprite. I just need to be able to use the character objects in script for loading the correct images and base stats in my character screen GUI.
Thanks in advance!
About the author
#2
However, I must be doing something wrong. I tried to create objects of my character class which was created using ScriptObject, but the default values I set do not come across.
I then try to echo the properites of the object in the console and they are all empty.
11/29/2006 (7:13 am)
I appreciate your response. I followed that link and that did shed a little light on the subject or at least confirm that I was thinking along the correct line. However, I must be doing something wrong. I tried to create objects of my character class which was created using ScriptObject, but the default values I set do not come across.
// create an instance of character
$char1 = new ScriptObject()
{
Class = "Character";
};I then try to echo the properites of the object in the console and they are all empty.
#3
11/29/2006 (8:10 am)
That code snippet you posted has no default values. To use default values you'll have to specify a config field which points at a config datablock. It should be noted that a class in TS is not a class at all but a namespace trick. All your echoed properties are blank because you haven't specified any. Do some research on config datablocks. Those will get you the behavior you're looking for.
#4
I actually had properites setup elsewhere in code, I just didn't include it in the snippet. I will try using a config datablock with the default properites.
11/29/2006 (10:14 am)
Thanks Ben.I actually had properites setup elsewhere in code, I just didn't include it in the snippet. I will try using a config datablock with the default properites.
#5
However, I still get empty fields when I try and echo. What am I doing wrong here other than not switching my thinking from C++ to Torque Script?
Here is what I have in the log:
No default values...
11/29/2006 (11:11 am)
Ok, I did a little more research and found some examples that seemed like they would work. Here is what I ended up with:/// Config properties for the base character config.
datablock t2dSceneObjectDatablock( CharacterConfig )
{
// class
class = "Character";
// character information
charName = "";
charType = 0;
status = "Good";
crntHP = 30;
crntSP = 15;
charLevel = 1;
// attributes
strength = 0;
HP = 30;
pAttPower = 5;
agility = 0;
pDefense = 5;
speed = 0;
intelligence = 0;
SP = 15;
mAttPower = 0;
mDefPower = 0;
// equipment
wpnBase = 10;
headSlot = 0;
chestSlot = 0;
offHandSlot = 0;
legSlot = 0;
neckSlot = 0;
ringSlot1 = 0;
ringSlot2 = 0;
// combos
crntCombo = 0;
};
/// Config properties for the player characters
datablock t2dSceneObjectDatablock( PlayerCharacterConfig : CharacterConfig )
{
// class
class = "Character";
// order the character is in the party
partyOrder = 0;
// experienced earned and experience needed to reach next experience level
totalExp = 0;
expNext = 35;
// equipment type allowed
equipType = "LIGHT";
};
/// Config properties for the enemy characters
datablock t2dSceneObjectDatablock( EnemyCharacterConfig : CharacterConfig )
{
// class
class = "Character";
// special attack
specAttack = 0;
// item drop type
itemDrpType = 0;
// exp awarded
expAwarded = 0;
// money drop
moneyDrop = 4;
};
/// function to intialize characters and party
function initCharacters()
{
// create an instance of character
$char1 = new ScriptObject()
{
class = "Character";
config = PlayerCharacterConfig;
};
// set player specific properites
---
}However, I still get empty fields when I try and echo. What am I doing wrong here other than not switching my thinking from C++ to Torque Script?
Here is what I have in the log:
==>$char1.dump(); Member Fields: class = "Character" superclass = "" Tagged Fields: config = "PlayerCharacterConfig" Methods: delete() - obj.delete() dump() - obj.dump() dumpClassHierarchy() - obj.dumpClassHierarchy() getClassName() - obj.getClassName() getDynamicField() - obj.getDynamicField(index) getDynamicFieldCount() - obj.getDynamicFieldCount() getFieldValue() - obj.getFieldValue(fieldName); getGroup() - obj.getGroup() getId() - obj.getId() getInternalName() - getInternalName returns the objects internal name getName() - obj.getName() getType() - obj.getType() isChildOfGroup() - returns true, if we are in the specified simgroup - or a subgroup thereof isMemberOfClass() - isMemberOfClass(string classname) -- returns true if this object is a member of the specified class isMethod() - obj.isMethod(string method name) onChanged() - save() - obj.save(fileName, <selectedOnly>) schedule() - object.schedule(time, command, <arg1...argN>); setFieldValue() - obj.setFieldValue(fieldName,value); setInternalName() - string InternalName setName() - obj.setName(newName)
No default values...
#6
11/29/2006 (5:21 pm)
I don't think ScriptObject types have a config datablock. At least not a t2dSceneObjectDatablock which is for using on scene objects (which script objects are not). You can tell because "config" is a tagged field instead of a member field.
#7
So basically your saying I have to use another object just to get the behavior I want even though it is just a dummy object. I am not going to be associating these property sets to actual sprites or objects right now just for the fact that the characters (party members) are only individual sprites in combat. I primary need the characer information now for loading info in my various GUI screens based on the currently selected party member (character).
Now with datablocks after sifting through the documentation with TGB I have only seen 3 datablock type mentioned. I have done a search for datablocks on this site, but have yet to find a direct link to reference material or resources covering all that is available to me in Torque Script. I think that fact in itself is what is holding me back and the fact that time isn't on my side.
Again thank you for your input.
11/29/2006 (7:34 pm)
I thought that might be the case with the ScriptObject, but being that Torque Script doesn't actually support classes, I can't just give it a class and get the behavior I need. Which is having properties with default values. It might just be that I am having trouble leaving behind the whole OOP concept and reverting to scripting.So basically your saying I have to use another object just to get the behavior I want even though it is just a dummy object. I am not going to be associating these property sets to actual sprites or objects right now just for the fact that the characters (party members) are only individual sprites in combat. I primary need the characer information now for loading info in my various GUI screens based on the currently selected party member (character).
Now with datablocks after sifting through the documentation with TGB I have only seen 3 datablock type mentioned. I have done a search for datablocks on this site, but have yet to find a direct link to reference material or resources covering all that is available to me in Torque Script. I think that fact in itself is what is holding me back and the fact that time isn't on my side.
Again thank you for your input.
#8
11/29/2006 (8:10 pm)
ScriptObject is not going to be able to use a config datablock no matter what type of datablock you uncover. What I do to get ScriptObject with default values is to code a factory method that builds and returns a ScriptObject with all the default initializations. The cool thing about factory methods is you can also pass in arguments to customize the assembled object to your purpose. There are a number of ways to implement this. If you are stumped for ideas post here and I'll sketch out a few possibilities.
#9
Anyway, if you would like to post a couple ideas that would be great. I always find outside input and multiple methods beneficial.
Thanks
11/30/2006 (6:08 am)
I have an idea of how to create the methods I need. I have just been having to cut a lot of corners and change a lot of things from my original design to try and get things done on time. Not researching TGB fully when beginning the project didn't help either. I thought I could use classes and my design was all about classes, which is what I am used to, but that wasn't the case with TGB.Anyway, if you would like to post a couple ideas that would be great. I always find outside input and multiple methods beneficial.
Thanks
#10
Some code for what you're trying to do:
I think it should be easy to see from that example how you would then write the constructor for the enemy and do lots of other OO-like things. You can then write methods in the Character, PlayerCharacter, and EnemyCharacter namespaces and your objects will call to the proper versions based on what type they actually are.
11/30/2006 (8:11 am)
Actually, TS supports just about every aspect of OO design, including what you're trying to do. You're just not approaching the problem in a TS friendly way. About the only crucial feature of more official OO languages that I find missing from TS is access levels. Everything else can pretty much be done.Some code for what you're trying to do:
/// @access Private
/// Builds a new Character and returns it as a script object.
/// The Character will be the superclass of the returned object
/// so you may extend it with a more specific type passed as
/// an arg.
/// @param %class (Class) The child class for this Character.
/// @return (Character) A new Character superclass.
function Character_getCharacter(%class)
{
%character = new ScriptObject
{
superclass = "Character";
class = %class;
characterLevel = 1;
// do more static initialization here
};
%character.inventory = new SimSet();
// do more non-static init here
return %character;
}
/// @access Public
/// Builds a new Character with the given name.
/// @param %name (String) The name for your character.
/// @return (PlayerCharacter) A new PlayerCharacter as a
/// script object with superclass Character.
function Player_getPlayerCharacter(%name)
{
%character = Character_getCharacter("PlayerCharacter");
%character.name = %name;
// do more init here
}I think it should be easy to see from that example how you would then write the constructor for the enemy and do lots of other OO-like things. You can then write methods in the Character, PlayerCharacter, and EnemyCharacter namespaces and your objects will call to the proper versions based on what type they actually are.
#11
11/30/2006 (10:31 pm)
Ok, that really sheds some light on things. I just need to change my view a bit. I really appreciate your help. Do you know of a comprehensive guide to TS that I can get access to, that might help me get into all things TS?
#12
For me it was a combination of Ken Finney's books, Ed Maurina's book (most highly recommended for TS related stuff), going through tutorials and really absorbing what they had in them, and hanging out at the forums every day. At one point after getting TGB I decided I would learn how to use TGB/TS by helping other learn. Every day I looked for questions people were asking and tried to answer them. If I didn't know the answer I went looking for it. I educated myself by educating others. It's a technique I really believe in. The best advice I can give is to be a sponge, sorry if that's a little vague!
I really do recommend Maurina's book.
Tag: TGB in GPGT
12/01/2006 (12:11 am)
It can be a leap from thinking straight, abstract OO principals to thinking in terms of namespaces.For me it was a combination of Ken Finney's books, Ed Maurina's book (most highly recommended for TS related stuff), going through tutorials and really absorbing what they had in them, and hanging out at the forums every day. At one point after getting TGB I decided I would learn how to use TGB/TS by helping other learn. Every day I looked for questions people were asking and tried to answer them. If I didn't know the answer I went looking for it. I educated myself by educating others. It's a technique I really believe in. The best advice I can give is to be a sponge, sorry if that's a little vague!
I really do recommend Maurina's book.
Tag: TGB in GPGT
#13
I will see what I can find and definately look at getting Maurina's book.
12/01/2006 (11:34 am)
Hey thanks again. This is what I wanted to see when I called dump on my object:==>$char1.dump(); Member Fields: class = "PlayerCharacter" superclass = "Character" Tagged Fields: actionSpeed = "3" activeCombo = "0" agility = "2" characterLevel = "1" characterName = "Talen" characterStatus = "Good" characterType = "0" combos = "3790" currentHP = "30" currentSP = "15" equipmentType = "Light" expNext = "35" HP = "30" intelligence = "2" inventory = "3789" magAttPower = "1" magDefPower = "1" partyOrder = "0" physAttPower = "5" physDefense = "5" SP = "15" strength = "3" talents = "3788" totalExp = "0" wpnBaseDmg = "6"
I will see what I can find and definately look at getting Maurina's book.
Torque Owner Very Interactive Person
www.garagegames.com/mg/forums/result.thread.php?qt=52945