Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Function that runs post object creation to assign a pointer

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
ulysse31
Master Cheater
Reputation: 2

Joined: 19 Mar 2015
Posts: 324
Location: Paris

PostPosted: Wed Dec 23, 2015 8:25 pm    Post subject: Function that runs post object creation to assign a pointer Reply with quote

Hi,

I receive a Packet that initiates a Monster class, I have designed it this way :
I got 20 monster pointers as global variables that I assign to Monster object upon creation, the code is as follows :
Code:
ptrMobKn = boost::shared_ptr <Monsters>(new Monsters(vectArgument, nAnalyzedSize, ptrMobKn));


The Monster class constructor is :
Code:
class Monsters
{
public:
   std::vector <unsigned char> m_Id;
   long long m_llId;
   int m_X;
   int m_Y;
   int m_Z;
   bool m_LifeState;
   Monsters::Monsters() {

   }
   Monsters::Monsters(std::vector <unsigned char> &vectArg, int &nAnalyzedSize, boost::shared_ptr < Monsters > & MonsterPtr) : m_LifeState(1)
   {
      m_X = TurnVectInto32_int(vectArg, 26, nAnalyzedSize);
      m_Y = TurnVectInto32_int(vectArg, 30, nAnalyzedSize);
      m_Z = TurnVectInto32_int(vectArg, 34, nAnalyzedSize);
      for (int uuu = 10; uuu < 18; ++uuu)
      {
         m_Id.push_back(vectArg[uuu + nAnalyzedSize]);
      }
      m_llId = TurnVecInto64_int(vectArg, 10, nAnalyzedSize);
      MonstersMap[m_llId] = MonsterPtr;
   }
   void updatePtrValue(boost::shared_ptr <Monsters> Mobptr);
   void GetHandle();
   Monsters & GetAHandle();
};


my map and my function to update it :

Quote:
std::map<long long, boost::shared_ptr <Monsters>> MonstersMap;
void Monsters::updatePtrValue(boost::shared_ptr <Monsters> MonsterPtr) {
MonstersMap[m_llId] = MonsterPtr;
}


My problem is : the way it's coded I pass the object its own smart pointer and the object stores it into a map within which they key value is the monster ID.
The problem is when I pass the pointer at object creation time it's not yet pointing to the object, it has to wait the return of the object constructor to get assigned to the object therefore when the constructor stores the ptr, its basicly storing an empty pointer. My work around this has been to then use an 'update' ft that just updates the ptr stored into map, i run this function right after constructor.

I am looking for a neater way to do this that doesn't involve me using a function after the constructor IE something that would create a thread that waits constructor to end and then by itself updates the ptr in the map, that thread would be initiated by the constructor. I was thinking this is a callback but after further reading it seems not, I am wondering if there is a simple way to do this such as using pre defined c++ features ?
Thanks !
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Dec 23, 2015 9:06 pm    Post subject: Reply with quote

You could look into using async callbacks to fire a callback when the object is constructed.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
ulysse31
Master Cheater
Reputation: 2

Joined: 19 Mar 2015
Posts: 324
Location: Paris

PostPosted: Thu Dec 24, 2015 3:21 am    Post subject: Reply with quote

Ah yes i didn't know there was an std::async, that ought to do it i am guessing with some mutex lock, thanks !
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites