Example #1
0
void CScriptProxy::CreateScriptTable( SEntitySpawnParams* pSpawnParams )
{
	m_pThis = m_pScript->GetScriptSystem()->CreateTable();
	m_pThis->AddRef();
	//m_pThis->Clone( m_pScript->GetScriptTable() );

	CEntitySystem* pEntitySystem = (CEntitySystem*)m_pEntity->GetEntitySystem();
	if (pEntitySystem)
	{
		//pEntitySystem->GetScriptBindEntity()->DelegateCalls( m_pThis );
		m_pThis->Delegate(m_pScript->GetScriptTable());
	}

	// Clone Properties table recursively.
	if (m_pScript->GetPropertiesTable())
	{
		// If entity have an archetype use shared property table.
		IEntityArchetype* pArchetype = m_pEntity->GetArchetype();
		if (!pArchetype)
		{
			// Custom properties table passed
			if (pSpawnParams && pSpawnParams->pPropertiesTable)
			{
				m_pThis->SetValue( SCRIPT_PROPERTIES_TABLE, pSpawnParams->pPropertiesTable);
			}
			else
			{
				SmartScriptTable pProps;
				pProps.Create( m_pScript->GetScriptSystem());
				pProps->Clone( m_pScript->GetPropertiesTable(),true,true );
				m_pThis->SetValue( SCRIPT_PROPERTIES_TABLE,pProps );
			}
		}
		else
		{
			IScriptTable* pPropertiesTable = pArchetype->GetProperties();
			if (pPropertiesTable)
				m_pThis->SetValue( SCRIPT_PROPERTIES_TABLE,pPropertiesTable );
		}

		SmartScriptTable pEntityPropertiesInstance;
		SmartScriptTable pPropsInst;
		if (m_pThis->GetValue("PropertiesInstance",pEntityPropertiesInstance))
		{
			pPropsInst.Create( m_pScript->GetScriptSystem());
			pPropsInst->Clone( pEntityPropertiesInstance, true,true );
			m_pThis->SetValue("PropertiesInstance", pPropsInst);
		}
	}

	// Set self.__this to this pointer of CScriptProxy
	ScriptHandle handle;
	handle.ptr = m_pEntity;
	m_pThis->SetValue( "__this",handle );
	handle.n = m_pEntity->GetId();
	m_pThis->SetValue( "id",handle );
	m_pThis->SetValue( "class",m_pEntity->GetClass()->GetName());
}