void VScriptComponent::Serialize( VArchive &ar )
{
  char iLocalVersion = VERSION_CURRENT;
  IVObjectComponent::Serialize(ar);

  if (ar.IsLoading())
  {
    ar >> iLocalVersion;
    if (iLocalVersion<VERSION_INITIAL||iLocalVersion>VERSION_CURRENT)
      hkvLog::FatalError("Invalid script serialization version - please re-export scene.");

    // This is a workaround for [#21287] : This component must be immediately added to the owner's component list,
    // otherwise script function calls on it create a new script component which results in one object having two script components
    // [8.1: need to serialize it here]
    VisTypedEngineObject_cl *pOwner = ar.ReadObject<VisTypedEngineObject_cl>();
    if (pOwner != NULL && !pOwner->Components().Contains(this))
      ((VObjectComponentCollection &)pOwner->Components()).Add(this);
    // additionally we have to temporarily pretend the owner is set:
    m_pOwner = pOwner;

    m_iScriptRefID = LUA_REFNIL;
    VScriptInstance *pInstance = NULL;
    ar >> pInstance;
    // we shouldn't call SetScriptInstance since it calls the Lua OnCreate function, which may try to access the object
    //SetScriptInstance(pInstance);
    m_spInstance = pInstance;

    if(iLocalVersion >= VERSION_CUSTOM_EXPOSED_MEMBERS)
    {
      int iNumOfSerializedMembers;
      ar >> iNumOfSerializedMembers;

      m_CustomExposeVars.Clear();

      // read all members storead as Name+Type
      for(int i = 0;i < iNumOfSerializedMembers; i++)
      {
        char szBuffer1[64];
        char szBuffer2[64];
        bool bAllocated1 = false;
        bool bAllocated2 = false;
        const char *szName = ar.ReadEncryptedString(szBuffer1, 64, bAllocated1);
        const char *szValue = ar.ReadEncryptedString(szBuffer2, 64, bAllocated2);

        m_CustomExposeVars.Add(VScriptMember(szName, szValue));

        if(bAllocated1) V_SAFE_DELETE(szName);
        if(bAllocated2) V_SAFE_DELETE(szValue);
      }
    }