GetDevice function

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

GetDevice function

Post by Robomaniac »

I know this is a stupid c++ and Irrlicht question, but is there a getDevice function? I want to have a CGameClass, and a CWeaponsClass, is there a way to get the device for my CWeaponsClass to use?

Thanks in advance

--The Robomaniac
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

there is not a global function like that, no. however, if you store your device pointer as global, or static, you can write your own global (or static) function to return it. (though if its global, you dont need a function to get it)

personally, i prefer to stay away from this sort of thing. my m_Device pointer belongs to my 'GameClass', and since all objects that are created do so from underneath this class, I simply pass down the m_Device pointer as neccesary.

for instance, if I had an FPS, my m_device pointer would be passed down thru:
game_engine->gs_main->weapon_mgr->weapon

why does your weapon need the device pointer anyway? that sounds like a bad design to me. a weapon should only need to keep track of its state, and its model, and be able to create projectiles.. the model should worry about device drawing, and the weapon mgr would worry about weapon creation/deletion etc. and gs_main would worry about displaying ammo to the GUI, etc.
a screen cap is worth 0x100000 DWORDS
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

I need to pass down the device in order to get the scene manager in order to shoot the projectile, right now, i'm just using the tech demo code, but i might modify it a bit. If i already declare my smgr in my render function, do i need to reget it, or does it automatically pass down?

--The Robomaniac
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

what do you mean 'declared' your scene manager in your render function? (maybe some code would help me understand)
a screen cap is worth 0x100000 DWORDS
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

Sorry, i've been pretty busy with school, and havn't really had time to code. to clarify a bit, in my CGameClass::render() function, i have

Code: Select all

smgr = device->getSceneManager
and in my CShotgun::shoot() function, i have a local variable smgr, which i reget using

Code: Select all

smgr = device->getSceneManager()
i was wondering whether, if i already have smgr declared in one class, whether it would carry off into other classes if i don't set it to 0. If not, is there another way to pass on the device variable, as in a getDevice() function?

Thanks

--The Robomaniac
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

when you create the device, it creates a scene manager somewhere in memory.

all getSceneManager() does is return the address of that manager. it doesnt matter if you call it in class X and send that address to someone else, or if that someone else calls device->getSceneManager() themselves-- they'll get the same address to the same object.

so to answer your question directly, yes. you can pass a smgr* around if you dont want to have to give your CShotgun a device*
a screen cap is worth 0x100000 DWORDS
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

Give us an example. . . :?
Programmer of the Irrlicht Scene Editor:
Homepage - Forum Thread
Version 2 on its way!!
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

Post by Boogle »

Outside of the shotgun (perhaps in your main class)

Code: Select all

shotgun->setSceneManager(device->getSceneManager);
and in the shotgun:

Code: Select all

ISceneManager *Manager;
setSceneManager(ISceneManager *mgr) { Manager = mgr; }
Or, pass it in the Shotgun's constructor (which is probably the best idea).
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

Ok, i've heard the pass it throught the constructor before, but i'm confused about that, what does that do excactly, and how do you do it. (Sorry, i'm just getting started learning oop)

--The Robomaniac
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

class CShotgun {
smgr* m_smgr;

CShotgun(smgr*) {
m_smgr = smgr;
}

};


CApp::Init{
CShotgun* shotty;
shotty = new CShotgun(this->smgr);

shotty->blowshitup();
}
a screen cap is worth 0x100000 DWORDS
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

Sorry for late responce, but i just got around to trying that, but i still get a access violation/ segmentation fault. Thanks

--The Robomaniac
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

I keep getting access violation errors too. I need to be able to access the same device in two classes. (drawing to that device and all. . .) Any help? :?:
Programmer of the Irrlicht Scene Editor:
Homepage - Forum Thread
Version 2 on its way!!
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

bump

Post by Robomaniac »

Anyone have an answer, i kinda need it so that i can proceed w/ my game

*bump*

-- The Robomaniac
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

Lets thank keless for this. Also take a look'c at ICE for Irrlicht. :D
okay.

say you have a main class. something like CMainApp.
it has an IrrLicht Device pointer. now, you're going to want to use that device while you're in your 'intro' state, and while you're in your 'main game' state, etc. Or, in your case, while you're in your 'scene1' state, and while you're in your 'scene2' state.

Since different states can get complicated, its good practice to put them into their own class. So we can have a Scene1 class, and a Scene2 class. CMainApp will take care of creating the IrrLicht device, and it will keep the pointer to it. Then when its neccesary, it will create a Scene1 object, and pass the device pointer to that. And when its neccesary, it will delete that object, and create a Scene2 object, passing the pointer ot that instead.

Before we go too far, we should realize that Scene1 and Scene2 are going to have some properties that are similar. For instance, they both happen to need to use IrrLicht! So, we should start by making a base class that both of them will derive from.

This base class will be something like:
class game_state {
IrrLichtDevice * m_device
game_state(device*) { m_device = device; }
virtual draw() = 0;
};

So, CMainApp will have a game_state*, which will either be pointing at a Scene1 object, or a Scene2 object (because they both derive from game_state, you can point to either of them using a game_state*).

Now we create our Scene1/2 classes:
class scene1 : public game_state {
scene1(device*) : game_state(device); //this will call game_state's constructor and set m_device there
draw() { //put scene1 drawing code here, using the m_device pointer }
};

class scene2 : public game_state {
scene2(device*) : game_state(device); //this will call game_state's constructor and set m_device there
draw() { //put scene2 drawing code here, using the m_device pointer }
};

now, all your CMainApp has to do is keep track of when to create which scene. It doesnt even need to wory about calling the right Draw() function-- because it has a game_state pointer, it can call draw() on the game_state class, but since draw() is VIRTUAL in the game_state class, it will know to call the draw() function of the actuall object. So whether you're pointing at a Scene1 object, or a scene2 object, the right draw() function will be called.

and thats that.
Programmer of the Irrlicht Scene Editor:
Homepage - Forum Thread
Version 2 on its way!!
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

Thanks, hadn't looked @ ICE, i'm was going to later after i made my main game work, them work on gamestates like pause and menu and other stuff

-- The Robomaniac
Post Reply