copy a pointer to eventreceiver?

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
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

copy a pointer to eventreceiver?

Post by c.h.r.i.s »

Hi! I need to give a pointer to the eventreceiver class. I tried this:

Code: Select all


class MyEventReceiver : public IEventReceiver
{
public:
world *myworld;//my pointer storage
	virtual bool OnEvent(SEvent event)
	{
	}
};

void bla()
{
world myworld;//struct of everysthing
MyEventReceiver receiver;
myworld->device->setEventReceiver(&receiver);
receiver.myworld=myworld;//copy pointer to class
}
I need this because i canot make "myworld" global. But this example makes a copy of "myworld" and dont copys the pointer. Any Ideas?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I wonder why this would compile anyway, you have to take the address of the class: receiver.myworld=&myworld;
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

Hi! if i use "receiver.myworld=&myworld;" instead of "receiver.myworld=myworld;" compiler says: error C2440: '=' : cannot convert from 'world **' to 'world *'
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

i would do it like that:

Code: Select all

class eventRcv
{
public:
  world *myworld;
  getWorld(world *w); { myworld = v }
}

//////////

world myBeautifulWorld;
eventRcv receiver;
receiver.getWorld(&myBeautifulWorld);
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

HI! Its still the same error. And i know why:

world myBeautifulWorld; is already a pointer (world *myBeautifulWorld;)

Thats why world *myBeautifulWorld is defined in the header of a function in my script. And i need to give this pointer to the eventreceiver class from this function.

Error occures at:
receiver.getWorld(&myBeautifulWorld);

Here is the whole script example:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
world *myworld;//pointerstorage
	virtual bool OnEvent(SEvent event)
	{
                //at this point i need a pointer to theworld
                }
}

void init_world(world *myworld)
{ 
//myworld is now a pointer to theworld

MyEventReceiver receiver;//now creating the eventreceiver
myworld->device->setEventReceiver(&receiver);

receiver.myworld=&myworld;//copy the pointer to eventreceiver (do not work)
}

world theworld;
init_world(&theworld);
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Of course you have to use the passing of the pointer value instead of the adress as you already are working with adresses. But your program won't work as your event receiver will die after the init_world method.
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

Hm. Okay. That means i cant use the event receiver in that way. Is there a way to derectly ask for events in the main loop? For example:

Code: Select all

while(device->run)
{
            if(EGET_BUTTON_CLICKED)
            {
                    if(id==123)
                    {...}
             }
             if(KeyInput.Key==KEY_KEY_C)
             {...}
}
is that possible?
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

CEventReciever.cpp

Code: Select all

// Include headers 
#include <CEventReceiver.h> 

// Constructor 
CEventReceiver::CEventReceiver() 
{ 
   // Set the keypress array to false 
   for (s32 i = 0; i < KEY_KEY_CODES_COUNT; i++) 
   { 
     keys[i] = false; 
   } 
} 

// Event handler function 
bool CEventReceiver::OnEvent(SEvent Event) 
{ 
   // If the event type is a key input event 
   if (Event.EventType == EET_KEY_INPUT_EVENT) 
   { 
      // Set the corresponding value in the array to the state of the key 
      keys[Event.KeyInput.Key] = Event.KeyInput.PressedDown; 
   } 

   // Return false - ensures FPS cameras will still work 
   return false; 
} 

// Get key state 
bool CEventReceiver::getKeyState(EKEY_CODE key) 
{ 
   return keys[key]; 
} 
CEventReciever.h

Code: Select all

#ifndef __CEVENTRECEIVER 
#define __CEVENTRECEIVER 

// Include headers 
#include <irrlicht.h> 

// Use namespaces 
using namespace irr; 
using namespace core; 

// Event Receiver Class 
class CEventReceiver : public IEventReceiver 
{ 
   public: 
      CEventReceiver(); 
      virtual bool OnEvent(SEvent Event); 
      bool getKeyState(EKEY_CODE key); 
   private: 
      bool keys[KEY_KEY_CODES_COUNT]; 
}; 

#endif 

USE

Code: Select all

CEventReceiver Receiver; 

while (Device->run()) 
{ 
   if (Receiver.getKeyState(KEY_UP)) 
   { 
       // Do something 
   } 
} 
IDK why hybrid fucks around instead of just showing stuff.
guess he just wants ya to learn it. so do that.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

hybrid wrote:I wonder why this would compile anyway, you have to take the address of the class: receiver.myworld=&myworld;

thats a common mingw problem I believe. but I'm probably wrong about that I'm paying minimul attention really. sry
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

WOW. the solution for this problem is very simple.

I dont have to define, call the eventreceiver, and copy pointer to it from init_world(). I have to do this in the function that takes the main loop!

Example:

Code: Select all

init_world(world myworld)
{
//create world, define variables, load device, load world, etc
//dont define and call the eventreceiver in here
}

run_world(world myworld)
{
//now define the eventreceiver
MyEventReceiver receiver;
myworld->device->setEventReceiver(&receiver);
receiver.myworld=myworld;

	while(myworld->device->run())
	{	
                 //...blabla
                }
}

destroy_world(world myworld)
{
//destroy world
}

world myworld;
init_world(&myworld);
run_world(&myworld);
destroy_world(&myworld);
This will work. Thanks a lot.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Midnight wrote:
hybrid wrote:I wonder why this would compile anyway, you have to take the address of the class: receiver.myworld=&myworld;

thats a common mingw problem I believe. but I'm probably wrong about that I'm paying minimul attention really. sry
No, the variable was defined as a pointer, so it was enough to pass the value (a pointer) instead of taking the addresse of a struct (which it was told to be).
And yeah, I will try to eventually give enough information to solve the problem, but I won't do it myself. Except for those who give me money :P
Post Reply