Jump to content

Tracing Entities Back To Their Parent Class


Shard
 Share

Recommended Posts

I've been watching the Raycasting tutorial video and looking over the article and its lead to me a critical question: how do you trace back the entity to a programmer created class?

 

For example, my player controller is inside my Player class, which has his health, jump height, shields, etc in it. When I do a raycast it will give me an entity pointer.

 

Using this, how would I check to see if the entity pointer is my player class or just a random crate?

 

 

One possible solution would be to compare all my player objects pointers with the pointer result from the raycast to see if they point to the same block of memory but this seems like an awful lot of processing to do.

 

Is there another way?

  • Upvote 1
  • Downvote 1

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Will the user data safely hold a pointer t a C++ structure and cast back?

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Close but not exactly. SetKey() only takes a string. You have to use SetEntityUserData() and pass in your pointer to your class. You have to cast it though.

 

I'm new to casting (never done it before). Could you give some example code please?

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Some of you don't use the entity's user data unsure.gif

 

It's such an awesome feature!!

 

 

Maybe this example will help you wink.gif

It's an example i pasted together from my code, so don't expect it to work

via copy&paste.

In the ProccessScene function you can call

CPlayer* NewPlayer = new CPlayer (_Entity, _RootEntity);

assuming that _Entity is the currently processed entity and _RootEntity is some

global entity for message handling.

After executing that line, the player has a pointer to it's own CPlayer class instance

as it's entity user data. It can access that user data (CPlayer class) on every callback.

 

Actually this is why i love Leadwerks biggrin.gif

 

 

/////////////////////////////////////////////////////////////////////////////
/// This class represents the user data (UD) of the player.
/////////////////////////////////////////////////////////////////////////////
class CPlayer
{
public:
/////////////////////////////////////////////////////////////////////////
/// Do everything needed to get the entity ready.
/// 
/// \param _Entity The entity the user data corresponds to
/// \param _RootEntity The main entity of the game
/////////////////////////////////////////////////////////////////////////
CPlayer (TEntity _Entity, TEntity _RootEntity)
{
 this->Init ();

 // Remember the entity, this class belongs to
 this->m_Entity = _Entity; 

 // Remember the game global root entity
 this->m_RootEntity = _RootEntity;

 // Set this class as the user data of the entity
 SetEntityUserData (this->m_Entity, (byte*)this);

 // Enable callbacks, which will further control the entity and it's 
 // behaviour. ENTITYCALLBACK_FREE should always be used, to free
 // dynamically allocated memory. Just comment out he ones not needed, or
 // leave them as they are.

 // Called when the entity is freed for some reason.
 SetEntityCallback (this->m_Entity,
   (byte*)GECPlayer::CBFree, ENTITYCALLBACK_FREE);

 // Called once per frame.
 SetEntityCallback (this->m_Entity, 
   (byte*)GECPlayer::CBUpdate, ENTITYCALLBACK_UPDATE);

 // Called whenever the entity moves or rotates.
 SetEntityCallback (this->m_Entity,
   (byte*)GECPlayer::CBUpdateMatrix, ENTITYCALLBACK_UPDATEMATRIX);

 // Called 60 times per second, independent of the framerate.
 SetEntityCallback (this->m_Entity,
  (byte*)GECPlayer::CBUpdatePhysics, ENTITYCALLBACK_UPDATEPHYSICS);

 // Called whenever a collision occurs. 
 SetEntityCallback (this->m_Entity, 
   (byte*)GECPlayer::CBCollision, ENTITYCALLBACK_COLLISION);

 // Called when the entity Receives a message.
 SetEntityCallback (this->m_Entity,
 (byte*)GECPlayer::CBMessageReceive, ENTITYCALLBACK_MESSAGERECEIVE);

 // Do whatever needed to initialize
}


/////////////////////////////////////////////////////////////////////////
/// Standard destructor.
/////////////////////////////////////////////////////////////////////////
~CPlayer ();

private:
/////////////////////////////////////////////////////////////////////////
/// Called when the entity is freed for some reason.
/////////////////////////////////////////////////////////////////////////
static void _stdcall CBFree (TEntity entity)
{
 GECPlayer* pUD = (GECPlayer*)GetEntityUserData (entity);

 if (NULL != pUD)
 {
  delete pUD;
 }
}

/////////////////////////////////////////////////////////////////////////
/// Called once per frame.
/////////////////////////////////////////////////////////////////////////
static void _stdcall CBUpdate (TEntity entity)
{ 
 CPlayer* pUD = (CPlayer*)GetEntityUserData (entity);

 if (NULL != pUD)
 {

 }
}

/////////////////////////////////////////////////////////////////////////
/// Called whenever the entity moves or rotates.
/////////////////////////////////////////////////////////////////////////
static void _stdcall CBUpdateMatrix (TEntity entity)
{
 CPlayer* pUD = (CPlayer*)GetEntityUserData (entity);

 if (NULL != pUD)
 {

 }
}

/////////////////////////////////////////////////////////////////////////
/// Called 60 times per second, independent of the framerate.
/////////////////////////////////////////////////////////////////////////
static void _stdcall CBUpdatePhysics (TEntity entity)
{
 CPlayer* pUD = (CPlayer*)GetEntityUserData (entity);

 if (NULL != pUD)
 {
 }
}

/////////////////////////////////////////////////////////////////////////
/// Called whenever a colision occurs. 
/////////////////////////////////////////////////////////////////////////
static void _stdcall CBCollision ( TEntity entity0,
         TEntity entity1, 
         byte* position, 
         byte* normal, 
         byte* force, 
         flt speed )
{
 CPlayer* pUD = (CPlayer*)GetEntityUserData (entity0);

 if (NULL != pUD)
 {
  TVec3 VecPosition; memcpy (&VecPosition, position, sizeof (TVec3));
  TVec3 VecForce;  memcpy (&VecForce, force, sizeof (TVec3));
  TVec3 VecNormal; memcpy (&VecNormal, normal, sizeof (TVec3));
 }
}

/////////////////////////////////////////////////////////////////////////
/// Called when the entity Receives a message.
/////////////////////////////////////////////////////////////////////////
static void _stdcall CBMessageReceive ( TEntity entity, 
           char* message,
           byte* extra)
{
 CPlayer* pUD = (CPlayer*)GetEntityUserData (entity);

 if (NULL != pUD)
 {

 }
}

/////////////////////////////////////////////////////////////////////////
/// Initializes all member variables with default values.
/////////////////////////////////////////////////////////////////////////
void Init ();

/// The entity this user data class belongs to.
TEntity m_Entity;

/// The game global root entity.
TEntity m_RootEntity;
}; 



(Win7 64bit) && (i7 3770K @ 3,5ghz) && (16gb DDR3 @ 1600mhz) && (Geforce660TI)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...