Class Call (Simple Question)
by Marcus · in Technical Issues · 11/09/2006 (12:44 pm) · 6 replies
How do I reference another class within a class?
My specific example being...
I have bound my movement keys ("a", "s", "w", "d") to the class ("Ship") of my "Plane" object but when pressing left and right ("a" and "d") I want both my "Plane" object in the "Ship" class and my "Earth" object of my "Planet" class to be effected (in this case moving left and right).
Maybe my question should be "how do I reference another Object from within a different class?".
My specific example being...
I have bound my movement keys ("a", "s", "w", "d") to the class ("Ship") of my "Plane" object but when pressing left and right ("a" and "d") I want both my "Plane" object in the "Ship" class and my "Earth" object of my "Planet" class to be effected (in this case moving left and right).
Maybe my question should be "how do I reference another Object from within a different class?".
#2
How to do a global variable that all classes and use and update. ect.
11/09/2006 (2:47 pm)
I guess what I'm really asking is how to communicate between classes.How to do a global variable that all classes and use and update. ect.
#3
if you can logically group said items.
you must define some type of relationship between these common items.
often best to place some type of manager above this so he is the one responsible to forward this move information to each pertinent object in the set.
11/09/2006 (4:04 pm)
SimGroup and SimSet will help with stuff like this.if you can logically group said items.
you must define some type of relationship between these common items.
often best to place some type of manager above this so he is the one responsible to forward this move information to each pertinent object in the set.
#4
This is basically what Badguy is talking about.
- Create a manager class (eg: PlayerControlObjManager)
- This could contain an array of objects that are derived from PlayerControlledObj
- When the player presses a key the PlayerControlObjManager would loop through the array and call the move method of each object.
PS. I haven't looked at the Torque code yet so the KeyCMD class is just made up - but I'm sure Torque has a class that would hold info on what key was pressed.
11/09/2006 (5:50 pm)
I'm not sure if this is the sort of thing you're after - but one way to do this is is to use derived classes (see below).This is basically what Badguy is talking about.
- Create a manager class (eg: PlayerControlObjManager)
- This could contain an array of objects that are derived from PlayerControlledObj
- When the player presses a key the PlayerControlObjManager would loop through the array and call the move method of each object.
class PlayerControlledObj
{
public:
virtual void move(KeyCMD* cmd);
};
class Ship : public PlayerControlledObj
{
public:
virtual void move(KeyCMD* cmd); // you might want to override the movement method
};
class Planet : public PlayerControlledObj
{
// overide move if you want
};
class Plane : public ship
{
// overide move if you want
}
class Earth : public Planet
{
// overide move if you want
}PS. I haven't looked at the Torque code yet so the KeyCMD class is just made up - but I'm sure Torque has a class that would hold info on what key was pressed.
#5
How to do a global variable that all classes and use and update. ect.
11/09/2006 (6:05 pm)
I guess what I'm really asking is how to communicate between classes.How to do a global variable that all classes and use and update. ect.
#6
Thats guys, I'll try it out tomorrow.
11/09/2006 (6:16 pm)
Tripple post? Goodness, thats embarassing.Thats guys, I'll try it out tomorrow.
Torque Owner Marcus
How to do a global variable that all classes and use and update. ect.