Exemple #1
0
IScriptObject* CScriptSystem::GetGlobalObject()
{
    CScriptObject* pObj = CreateScriptObject();
    lua_pushvalue(m_pLS, LUA_GLOBALSINDEX);
    pObj->CreateEmpty();
    pObj->Attach();
    return pObj;
}
Exemple #2
0
IScriptObject* CScriptSystem::CreateGlobalObject(const char* sName)
{
    SCRIPT_STACK_CHECK(m_pLS);
    CScriptObject* pObj = CreateScriptObject();

    if (!pObj->CreateGlobal(sName))
    {
        pObj->Release();
        return NULL;
    };

    return pObj;
}
Exemple #3
0
IScriptObject* CScriptSystem::CreateObject()
{
    SCRIPT_STACK_CHECK(m_pLS);
    CScriptObject* pObj = CreateScriptObject();

    if (!pObj->Create())
    {
        pObj->Release();
        return NULL;
    };

    return pObj;
}
	bool ScriptInstance::Instantiate(const string& path, std::weak_ptr<Entity> entity, shared_ptr<Scripting> scriptEngine)
	{
		if (entity.expired())
			return false;

		m_scriptEngine = scriptEngine;

		// Extract properties from path
		m_scriptPath				= path;
		m_entity					= entity;
		m_className					= FileSystem::GetFileNameNoExtensionFromFilePath(m_scriptPath);
		m_moduleName				= m_className + to_string(m_entity.lock()->GetId());
		m_constructorDeclaration	= m_className + " @" + m_className + "(Entity @)";

		// Instantiate the script
		m_isInstantiated = CreateScriptObject();

		return m_isInstantiated;
	}
bool LuaScriptComponent::Init(TiXmlElement* pData)
{
	LuaStateManager* pStateManager = LuaStateManager::Get();
	CB_ASSERT(pStateManager);

	// load the <LuaScriptObject> tag and validate it
	TiXmlElement* pScriptObjectElement = pData->FirstChildElement("ScriptObject");
	if (!pScriptObjectElement)
	{
		CB_ERROR("No <ScriptObject> tag in XML");
		return true;
	}

	// read in attributes
	const char* temp = nullptr;
	temp = pScriptObjectElement->Attribute("var"); // name of the variable in lua
	if (temp)
		m_ScriptObjectName = temp;

	temp = pScriptObjectElement->Attribute("constructor");
	if (temp)
		m_ConstructorName = temp;

	temp = pScriptObjectElement->Attribute("destructor");
	if (temp)
		m_DestructorName = temp;

	// having a "var" attribute will export this object to that name in lua
	if (!m_ScriptObjectName.empty())
	{
		m_ScriptObject = pStateManager->CreatePath(m_ScriptObjectName.c_str());
		if (!m_ScriptObject.IsNil())
		{
			// create the object in lua
			CreateScriptObject();
		}
	}

	// if there was a constructor, bind the constructor callback to that function in lua
	if (!m_ConstructorName.empty())
	{
		m_ScriptConstructor = pStateManager->GetGlobalVars().Lookup(m_ConstructorName.c_str());
		if (m_ScriptConstructor.IsFunction())
		{
			// if no "var" was given, create the script object here
			if (m_ScriptObject.IsNil())
			{
				m_ScriptObject.AssignNewTable(pStateManager->GetLuaState());
				CreateScriptObject();
			}
		}
	}

	// if there was a destructor, bind the destructor callback to that function in lua
	if (!m_DestructorName.empty())
	{
		m_ScriptDestructor = pStateManager->GetGlobalVars().Lookup(m_DestructorName.c_str());
	}

	// read the <LuaScriptData> tag
	TiXmlElement* pScriptDataElement = pData->FirstChildElement("ScriptData");
	if (pScriptDataElement)
	{
		if (m_ScriptObject.IsNil())
		{
			CB_ERROR("Error, m_ScriptObject should not be nil");
			return false;
		}

		// set up key/value pairs in lua
		for (TiXmlAttribute* pAttribute = pScriptDataElement->FirstAttribute(); pAttribute != nullptr; pAttribute = pAttribute->Next())
		{
			m_ScriptObject.SetString(pAttribute->Name(), pAttribute->Value());
		}
	}

	return true;
}
bool BaseScriptComponent::VInit(TiXmlElement* pData)
{
    LuaStateManager* pStateMgr = LuaStateManager::Get();
    AC_ASSERT(pStateMgr);

    // load the <ScriptObject> tag and validate it
    TiXmlElement* pScriptObjectElement = pData->FirstChildElement("ScriptObject");
    if (!pScriptObjectElement)
    {
        AC_ERROR("No <ScriptObject> tag in XML.  This won't be a very useful script component.");
        return true;  // technically it succeeded even though it won't be accessible
    }

    // read all the attributes
    const char* temp = NULL;
    temp = pScriptObjectElement->Attribute("var");
    if (temp)
        m_scriptObjectName = temp;

    temp = pScriptObjectElement->Attribute("constructor");
    if (temp)
        m_constructorName = temp;

    temp = pScriptObjectElement->Attribute("destructor");
    if (temp)
        m_destructorName = temp;

    // Having a var attribute will export the instance of this object to that name.
    if (!m_scriptObjectName.empty())
    {
        m_scriptObject = pStateMgr->CreatePath(m_scriptObjectName.c_str());

        if (!m_scriptObject.IsNil())
        {
            CreateScriptObject();
        }
	}

    // The scriptConstructor attribute will also cause a Lua object to be created if one wasn't created in 
    // the previous step.  The scriptConstructor string is treated as a function of the form f(scriptObject) 
    // and is called.
    if (!m_constructorName.empty())
    {
        m_scriptConstructor = pStateMgr->GetGlobalVars().Lookup(m_constructorName.c_str());
        if (m_scriptConstructor.IsFunction())
        {
			// m_scriptObject could be nil if there was no scriptObject attribute.  If this is the case, 
            // the Lua object is created here.
			if (m_scriptObject.IsNil())
			{
				m_scriptObject.AssignNewTable(pStateMgr->GetLuaState());
				CreateScriptObject();
			}
		}
    }

    // The scriptDestructor attribute is treated as a function in the form of f(scriptObject) and is called
    // when the C++ ScriptObject instance is destroyed.
    if (!m_destructorName.empty())
    {
        m_scriptDestructor = pStateMgr->GetGlobalVars().Lookup(m_destructorName.c_str());
	}

    // read the <ScriptData> tag
    TiXmlElement* pScriptDataElement = pData->FirstChildElement("ScriptData");
    if (pScriptDataElement)
    {
        if (m_scriptObject.IsNil())
        {
            AC_ERROR("m_scriptObject cannot be nil when ScriptData has been defined");
            return false;
        }

        for (TiXmlAttribute* pAttribute = pScriptDataElement->FirstAttribute(); pAttribute != NULL; pAttribute = pAttribute->Next())
        {
            m_scriptObject.SetString(pAttribute->Name(), pAttribute->Value());
        }
    }
	return true;
}