0

So I have a factory for creating objects based on RakNet's NetworkIDObjects. If it's the authority then it will just create a new object, otherwise it will create the object then set the Net ID:

std::shared_ptr<CNetObject> CNetObjectFactory::Create(int netObjectType, bool IsAuthority, RakNet::NetworkID networkID) const
{
    auto entry = m_RegisteredObjects.find(netObjectType);  
    auto clone = std::shared_ptr<CNetObject>(entry->second->Clone());
    clone->SetNetworkIDManager(m_NetworkIDManager.get());
    if (IsAuthority)
    {
        clone->SetAuthority();
    }
    else if (networkID != 0)
    {
        clone->SetNetworkID(networkID);
    }

    return clone;
}

This works fine for the first couple of NetObjects spawned on the client, but the third one always crashes inside SetNetworkID:

Assertion Failed! nio->GetNetworkID()!=rawId

With the following callstack:

MyApp.exe!RakNet::NetworkIDManager::TrackNetworkIDObject(class RakNet::NetworkIDObject *)   Unknown
MyApp.exe!RakNet::NetworkIDObject::SetNetworkID(unsigned __int64)   Unknown
MyApp.exe!CNetObjectFactory::Create(int netObjectType, bool IsAuthority, unsigned __int64 networkID) Line 42    C++

I can't find any information about this error anywhere else, nor can I figure out what is different about this particular object. The NetworkIDs seem no different to the previous objects (except it's increased by one). As far as I can tell there's nothing obvious that can be causing this crash.

Bok McDonagh
  • 1,367
  • 10
  • 27

1 Answers1

0

So I figured out the problem - the error is thrown because I am trying to add an object with the same NetworkID as one that has already been added.

It stemmed from further up the call stack:

    auto netObject = m_NetworkIDManager->GET_OBJECT_FROM_ID<CNetObject *>(networkId);
    if (netObject == nullptr)
    {
        netObject = m_NetObjectFactory->Create(netObjectType, false, networkId).get();
    }

I check to see if the object exists, then if it doesn't I go ahead and create it. The problem was that the NetworkIDManager referenced in this piece of code was a completely different instance, meaning the check would always fail and it would try to create a new object every time.

Bok McDonagh
  • 1,367
  • 10
  • 27