Page 1 of 1

copy a pointer to eventreceiver?

Posted: Sun Mar 18, 2007 10:56 am
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?

Posted: Sun Mar 18, 2007 11:02 am
by hybrid
I wonder why this would compile anyway, you have to take the address of the class: receiver.myworld=&myworld;

Posted: Sun Mar 18, 2007 11:28 am
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 *'

Posted: Sun Mar 18, 2007 12:42 pm
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);

Posted: Sun Mar 18, 2007 2:12 pm
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);

Posted: Sun Mar 18, 2007 2:25 pm
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.

Posted: Mon Mar 19, 2007 1:41 pm
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?

Posted: Mon Mar 19, 2007 1:52 pm
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.

Posted: Mon Mar 19, 2007 1:58 pm
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

Posted: Mon Mar 19, 2007 3:07 pm
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.

Posted: Mon Mar 19, 2007 3:13 pm
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