MyEventReceiver not working :(

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.
Post Reply
Magnus
Posts: 79
Joined: Sun Jan 16, 2005 5:53 pm
Location: Georgia, USA
Contact:

MyEventReceiver not working :(

Post by Magnus »

:( Working on my game's menu. eventreciever's thing doesnt work it says "my device isn't declared" or something.... but at the bottom it is... whats wrong with this code???

Code: Select all

  /*
	Comment Header
	Source Generated by DracSoft Irrlicht GUI Designer
	http://www.dracsoft.com/
	support@dracsoft.com
*/

#include <irrlicht.h>

using namespace irr;
using namespace gui;
using namespace video;
using namespace scene;
using namespace core;
using namespace io;


//********************
//EVENT RECEIVER CLASS
//********************
class MyEventReceiver : public IEventReceiver
{
    public:
    	virtual bool OnEvent(SEvent event)
    	{
    		//GUI EVENT
			if (event.EventType == EET_GUI_EVENT)
    		{
    			s32 id = event.GUIEvent.Caller->getID();

    			switch(event.GUIEvent.EventType)
    			{//MMMMMMM buttons start here
        			case EGET_BUTTON_CLICKED:
				if (id == 101)              
              {  
              //handle first button code here  
                mydevice->closeDevice();
                 return true;
                 
                 //*****************************//
                 //THIS IS WHERE IT DOESNT WORK//
              }
        				break;
    			}
			}

    		return false;
    	}
};


//******************
//IRRLICHT VARIABLES
//******************
IrrlichtDevice *mydevice;
IVideoDriver *mydriver;
ISceneManager *myscene;
IGUIEnvironment *mygui;


//****************
//GLOBAL FUNCTIONS
//****************
bool InitializeVideo()
{
	//INITIALIZE VIDEO DEVICE
 {       //OpenGL didn't work, try software
		mydevice = irr::createDevice(EDT_SOFTWARE, dimension2d<s32>(700,480),32,false,true,false);
		if (mydevice==0)       //Software didn't work, the computer blows so quit
			return false;
	}
	mydevice->setResizeAble(false);
	return true;
}

void RenderAll()
{
	mydriver->beginScene(true, true, SColor(255,255,255,255));
	myscene->drawAll();
	mygui->drawAll();
	mydriver->endScene();
}


//*********************
//MAIN PROGRAM FUNCTION
//*********************
int main()
{
    //start video
    if (!InitializeVideo())
		return 1;

	//get video driver and scene manager
	mydriver = mydevice->getVideoDriver();
	myscene = mydevice->getSceneManager();
	mygui = mydevice->getGUIEnvironment();

	//declare event reciever
    MyEventReceiver receiver;
	mydevice->setEventReceiver(&receiver);
	mydevice->setWindowCaption(L"Slayer- One Strike. One kill.");



	//create the GUI
mygui->addStaticText(L"Version alpha-0.1", rect<s32>(272, 200, 368, 220), false, false, 0, -1);
mygui->addButton(rect<s32>(72, 344, 168, 384), 0, -101, L"Exit");
mygui->addButton(rect<s32>(72, 296, 168, 336), 0, -102, L"Credits");
mygui->addButton(rect<s32>(72, 248, 168, 288), 0, -103, L"Controls");
mygui->addButton(rect<s32>(72, 200, 168, 240), 0, -104, L"Play Game");
IGUIImage *img501 = mygui->addImage(rect<s32>(72, 40, 372, 190), 0, -1, L"");
img501->setImage(mygui->getVideoDriver()->getTexture("C:/irrlicht-0.7/media/Slayerlogo.jpg"));


	//start updating the screen
	while(mydevice->run())
	{
		RenderAll();
	}

	//do cleanup
	mydevice->drop();

	return 0;
}
My error
Image
Image
Guest

Post by Guest »

declare mydevice before the class
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

Post by databandit »

declare the class inside the main function
Guest

Post by Guest »

Now this is scope.

if you want to access a variable it needs to be in scope.
Declare it at the top, or as a member variable of the eventreceiver.

Do NOT declare it in the event receiver. Because in the main function you'll initialise the global version and in the event receiver you'll access a local, uninitialised version.
Magnus
Posts: 79
Joined: Sun Jan 16, 2005 5:53 pm
Location: Georgia, USA
Contact:

Post by Magnus »

but the thing is,, the code is exactly the same in that gui tut... and it works for them? Ill try I hope it works
Image
Image
Magnus
Posts: 79
Joined: Sun Jan 16, 2005 5:53 pm
Location: Georgia, USA
Contact:

Post by Magnus »

/*
Comment Header
Source Generated by DracSoft Irrlicht GUI Designer
http://www.dracsoft.com/
support@dracsoft.com
*/

#include <irrlicht.h>

using namespace irr;
using namespace gui;
using namespace video;
using namespace scene;
using namespace core;
using namespace io;
//********************
//EVENT RECEIVER CLASS
//********************
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
//GUI EVENT
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();

switch(event.GUIEvent.EventType)
{//MMMMMMM buttons start here
case EGET_BUTTON_CLICKED:
if (id == 101)
{
//handle first button code here

mydevice->closeDevice();
return true;
//*****************************//
//THIS IS WHERE IT DOESNT WORK//
}
break;
}
}

return false;
}
};

//******************
//IRRLICHT VARIABLES
//******************
IrrlichtDevice *mydevice;
IVideoDriver *mydriver;
ISceneManager *myscene;
IGUIEnvironment *mygui;


//****************
//GLOBAL FUNCTIONS
//****************
bool InitializeVideo()
{
//INITIALIZE VIDEO DEVICE
{ //OpenGL didn't work, try software
mydevice = irr::createDevice(EDT_SOFTWARE, dimension2d<s32>(700,480),32,false,true,false);
if (mydevice==0) //Software didn't work, the computer blows so quit
return false;
}
mydevice->setResizeAble(false);
return true;
}

void RenderAll()
{
mydriver->beginScene(true, true, SColor(255,255,255,255));
myscene->drawAll();
mygui->drawAll();
mydriver->endScene();
}


//*********************
//MAIN PROGRAM FUNCTION
//*********************
int main()
{
MyEventReceiver receiver;
mydevice->setEventReceiver(&receiver);
mydevice->setWindowCaption(L"Slayer- One Strike. One kill.");

//start video
if (!InitializeVideo())
return 1;

//get video driver and scene manager
mydriver = mydevice->getVideoDriver();
myscene = mydevice->getSceneManager();
mygui = mydevice->getGUIEnvironment();

//declare event reciever
//don't.....




//create the GUI
mygui->addStaticText(L"Version alpha-0.1", rect<s32>(272, 200, 368, 220), false, false, 0, -1);
mygui->addButton(rect<s32>(72, 344, 168, 384), 0, -101, L"Exit");
mygui->addButton(rect<s32>(72, 296, 168, 336), 0, -102, L"Credits");
mygui->addButton(rect<s32>(72, 248, 168, 288), 0, -103, L"Controls");
mygui->addButton(rect<s32>(72, 200, 168, 240), 0, -104, L"Play Game");
IGUIImage *img501 = mygui->addImage(rect<s32>(72, 40, 372, 190), 0, -1, L"");
img501->setImage(mygui->getVideoDriver()->getTexture("C:/irrlicht-0.7/media/Slayerlogo.jpg"));


//start updating the screen
while(mydevice->run())
{
RenderAll();
}

//do cleanup
mydevice->drop();

return 0;
}




my new code... it still gives me error :(
Image
Image
Guest

Post by Guest »

That's because it is NOT exactly the same as that GUI tutorial:

right below the "using namespace" stuff,
there's a line in that one that says:

Code: Select all

IrrlichtDevice *device = 0;
In your case you need to call that thing mydevice
Guest

Post by Guest »

... and I see you already call it that - but you've placed the variable declaration BELOW the code that uses it (the event receiver).
You must place it ABOVE the event receiver.
Magnus
Posts: 79
Joined: Sun Jan 16, 2005 5:53 pm
Location: Georgia, USA
Contact:

Post by Magnus »

I tried that got "myeventreciver being used as type not declared as type"

can someone tell me exactly where to put this code:

MyEventReceiver receiver;
mydevice->setEventReceiver(&receiver);
mydevice->setWindowCaption(L"Slayer- One Strike. One kill.");

i'd be very greatful :lol: :D
Image
Image
Guest

Post by Guest »

Code: Select all

/*
Comment Header
Source Generated by DracSoft Irrlicht GUI Designer
http://www.dracsoft.com/
support@dracsoft.com
*/

#include <irrlicht.h>

using namespace irr;
using namespace gui;
using namespace video;
using namespace scene;
using namespace core;
using namespace io;

// <<<<<<<<<<<<<<<<< LOOK, THIS IS WHERE IT SHOULD BE >>>>>>>>>>>>>>>>
//******************
//IRRLICHT VARIABLES
//******************
IrrlichtDevice *mydevice;
IVideoDriver *mydriver;
ISceneManager *myscene;
IGUIEnvironment *mygui; 

//********************
//EVENT RECEIVER CLASS
//********************
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
//GUI EVENT
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();

switch(event.GUIEvent.EventType)
{//MMMMMMM buttons start here
case EGET_BUTTON_CLICKED:
if (id == 101)
{
//handle first button code here

mydevice->closeDevice();
return true;
}
break;
}
}
... etc, etc
Understand now?
Guest

Post by Guest »

As an aside, the only global variable you absolutely need to declare above the eventreceiver is the device - but it's probably better to keep them together from a maintenance point of view.
Magnus
Posts: 79
Joined: Sun Jan 16, 2005 5:53 pm
Location: Georgia, USA
Contact:

Post by Magnus »

thx ill test this later today :D
Image
Image
Post Reply