TwinFan / XPMP2

Multiplayer library for X-Plane 11/12 with instancing, TCAS override, and sound
https://twinfan.github.io/XPMP2/
Other
24 stars 16 forks source link

Implementation Help #1

Closed CommandBuffer closed 4 years ago

CommandBuffer commented 4 years ago

Hello,

I'm having some troubles with this implementation. I have sub-classed the XPMP2::Aircraft class through MyAirplane class.

class MyAirplane : public Aircraft
{
public:
    MyAirplane(const std::string& _icaoType,
        const std::string& _icaoAirline,
        const std::string& _livery,
        const std::string& _modelName = "") :
    Aircraft(_icaoType, _icaoAirline, _livery, _modelName)
    {

    }

    virtual void UpdatePosition()
    {
        SetLocation(Position.lat, Position.lon, Position.elevation);

        drawInfo.heading = Position.heading;
        drawInfo.pitch = Position.pitch;
        drawInfo.roll = Position.roll;
    }

    XPMPPlanePosition_t Position;
};

Currently, I'm using a std::map to hold the MyAirplane objects. The issue I'm having is that any object that is inserted into my map is almost immediately destructed through the within XPMP2::Aircraft. I am able to see this because I implemented the CBPlaneNotifier and each aircraft object is created and then destroyed.

Am I going about this the wrong way? If so, what is the best way to keep a collection of planes so that I can update them?

std::map<std::string, MyAirplane*> m_Planes;

MyAirplane *aircraft = new MyAirplane(typeCode, airlineIcao, liveryStr, modelStr);
m_Planes.emplace(key, aircraft); // key is the aircraft's callsign/tailnumber
TwinFan commented 4 years ago

So why don't you just place a breakpoint in your CBPlaneNotifier and find out what causes the object to be destructed by looking at the call stack?

Under normal circumstance, a plane would be destroyed when the object is destroyed. So you are basically asking: Why got my object destroyed?

I cannot provide implementation support at this basic level of how to use C++ containers, sorry. There are good tutorials out there on object creation, destruction, container usage and emplacing object in them. Just google a bit.

A few hints to start with, though:

Good luck!