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;
}
Пример #2
0
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;
}