Advertisement

Cycling through the People-Objects

Started by February 21, 2000 09:53 AM
5 comments, last by ZoomBoy 24 years, 6 months ago
I''ve a linked list of Actor objects that I had hoped to access each frame and have them do a function or test if they need to do a function each frame Below is the more technical stuff about why this doesn''t work. How do other people cycle through all their objects each frame to move them? Especially if you have an unknown number of objects(i.e. in Pong you lknow there''s 2 paddles and a ball) ==================================================================== class Actor : public totplist { public: char Name[20]; .... char *pathfound; short XHex, YHex; short frame; /// FUNCTIONS Actor(char *PCFile, int LoadType, Actor* NewActor, FILE *LoadStream); ~Actor(); int MoveActorToNextHex(int); int StartPath( int DestX, int DestY); void Walking(int ); void MovingOnScreen(int ); void Waiting(int); void VisualCheck(int); void SoundCheck(int); void BuddyCheck(int); // Function pointer void (*pAction)(int ms_flags); } --------------------------------------------------------------- At each frame I''d flip through the list of pActor = GetFirstActorOnList(); while (pActor != NULL) { pActor->pAction(ms_flags); pActor = GetNextActorOnList(); } For the actions --------------------------------------------------------------- void Actor::Waiting(int ms_flags) { frame++; if (frame == BUDDYCHECK) pAction = BuddyCheck; if (frame == VISUALCHECK) pAction = VisualCheck; if (ms_flags == LEFTBUTTON) pAction = BeginPath; .... } void Actor::BeginPath(int ms_flags) { int TestPath(XHex,YHex, EndX, EndY, pathfound); if (TestPath == OK) pAction = Walking; } --------------------------------------------------------------- what I usually get for-> if (frame == BUDDYCHECK) pAction = BuddyCheck; error C2440: ''='' : cannot convert from ''void (__thiscall Actor: :*)(int)'' // the RightHandSide BuddyCheck to ''void (__cdecl *)(int)'' // pAction BuddyCheck I know that this can be done with functions that are not in classes (I''ve done it) but how to do it this functions that are in classes ZoomBoy A 2D RPG with skills, weapons, and adventure. See my character editor, Tile editor and diary at http://www.geocities.com/Area51/Atlantis/7739/
Try declaring your pAction variable like:
void (__thiscall Actor::*pAction)(int);
The pointer types for functions inside objects are different.
Advertisement
My usual routine when I get that type of error( ''='' cannot convert) is to check if I can recast. But I''ve too litlle experience with ''this''s(self-referential object items to trust what I''m doing. After some noshing and some debugging of my sprite movements, I pull out the books on ''this'' and it should also help me in my constructors.

ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
http://www.geocities.com/Area51/Atlantis/7739/

I found a meeting in 1996 that discussed pointers to member functions
>>>>
There several varieties of pointers in the C++ Language. This "feature" is independent of the CPU architecture. "Regular" pointers are well known. A special "member function pointer" allows access to class based methods.
Since a member function (method) is part of a class, most C++ compilers compute the method pointer as a small offset to the class pointer. They do this because offsets can be smaller in size than a regular pointer, and thus more efficient. However, since it may be different in size, it must be treated differently by code that expects a full size pointer.

User written source code can be written to accomodate this. But many class libraries use function Call-backs to tailor a routine to user needs. The call-back argument is expected to be a regular size pointer. One problem is how to pass a member function pointer instead.
>>>>>

I''ll keep searching

ZoomBoy
Hi John,

Try defining your function pointer as:

void (Actor::*pAction)(int ms_flags);

This says that pAction is not just a pointer to
any old void function taking a single int parameter
but specifically one that is a member function of the
class Actor.

Alastair Mackintosh

Your answer was correct
It compiled and I could assign the Actor functions:
pAction = Waiting;
or
pAction = MoveActorToNextHex;

but when I create the execution
int ms_flags;
Actor *pCurrentObj = pActorList->GetActor();

pCurrentObj->pAction(ms_flags);

in the compiler it gives me the error
error C2064: term does not evaluate to a function

when I leave that line at just:
pCurrentObj->pAction

and debug to that line I see in the Watch window
pCurrentObj->pAction 0x00401208 Actor::MoveActorToNextHex(int)

It''s a bit hilarious
Now I''m trying to access it
I''ll keep searching on the net; I''ve found some abstracts and error messages on the Internet

ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
http://www.geocities.com/Area51/Atlantis/7739/
Advertisement
. I set up a pointer to a function but have had a difficult accessing it because the functions are part of the class Actor
pCurrentActor->pfnActionFn = MoveActorToNextHex;
you cant just access directly it directly as pCurrentActor->pfnActionFn
what I did with help from pingz and Alistair Mackintosh
typedef void (Actor::*pfnActionFn)(Actor *pActor, int ms_flags);
pfnActionFn - This defines a pointer not to a function but to a member function of Actor with the format of
void Actor::SomeFunction(Actor* pActor, int ms_flags)
I can't call it directly but with a public general function
void Actor::DoAction(Actor *pActor, int ms_flags)
{
(this->*pAction)(pActor, ms_flags); // Note ->* operator
}
You have to think of the brackets around=> (this->*pAction) as being there to evaluate rather rather to just "protect". The '*' is indirection that points not to the pointer itself but to evaluating the contents of the function pointer. The Actor pointer passed to the function does nothing as yet. I might use it for inter-action with other players or just dump it out-right. So each frame I call
pCurrentActor->DoAction(NULL, ms_flags);
And in the function itself I can change from one action function to another
// StartPath() - given a destination hex(DestX,DestY)
// it finds the current path from the Actors current position
int Actor::StartPath(Actor* pActor, int DestX, int DestY)
{
pAction = MoveActorToNextHex;
return AImove(XHex, YHex, DestX, DestY, pathfound);
}


ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
http://www.geocities.com/Area51/Atlantis/7739/


Edited by - ZoomBoy on 2/25/00 9:04:20 PM

This topic is closed to new replies.

Advertisement