• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua onEquipItem + script (TFS 1.2)

Aeronx

Intermediate OT User
Joined
Dec 17, 2015
Messages
746
Solutions
9
Reaction score
125
Hello everybody!

I've been trying to fix this issue for some time now, and im totally unaware if its even possible >.<!
Its TFS 1.2
The thing is i've got this:

Code:
  <movevent event="Equip" itemid="24852" slot="shield" level="500" function="onEquipItem" script="config.lua">
    <vocation name="Knight" />
    <vocation name="Elite Knight" showInDescription="0" />
  </movevent>
  <movevent event="DeEquip" itemid="24852" slot="shield" function="onDeEquipItem" script="config.lua"/>

Well, the thing is the script works perfectly, but the prerequisite of being a knight or elite knight doesnt work at all, so anybody can equip the item. Any way to work around this?

Im really sorry if the question is dumb or already done. Be searching a lot at the forum and did not find it.

Thanks in advance.
 
Hello everybody!

I've been trying to fix this issue for some time now, and im totally unaware if its even possible >.<!
Its TFS 1.2
The thing is i've got this:

Code:
  <movevent event="Equip" itemid="24852" slot="shield" level="500" function="onEquipItem" script="config.lua">
    <vocation name="Knight" />
    <vocation name="Elite Knight" showInDescription="0" />
  </movevent>
  <movevent event="DeEquip" itemid="24852" slot="shield" function="onDeEquipItem" script="config.lua"/>

Well, the thing is the script works perfectly, but the prerequisite of being a knight or elite knight doesnt work at all, so anybody can equip the item. Any way to work around this?

Im really sorry if the question is dumb or already done. Be searching a lot at the forum and did not find it.

Thanks in advance.
You can't have a script and use the vocation thing, unfortunately.
I do have a modification in source that allows you to do that if you want.

Changes to how movement and events work · Mkalo/forgottenserver@e59092e · GitHub

Only this modification, the other is not necessary for you, unless you wanna use actionId's in moveevent tags for equip/deequip.
 
I've compiled it already, and i've been doing some testing.

Code:
  <movevent event="Equip" itemid="24852" slot="shield" level="500"  script="config.lua" function="onEquipItem">
    <vocation name="Knight" />
    <vocation name="Elite Knight" showInDescription="0" />
  </movevent>
  <movevent event="DeEquip" itemid="24852" slot="shield" script="config.lua" function="onDeEquipItem"/>

Now if you have this, the need to be knight or elite knight works (Only knights o EK can equip the item), but the script "config.lua" doesnt work at all. If i erase
function="onEquipItem" then the script works perfectly.

Before compiling if there was a script, it will work no mattar what function but function wouldnt work. Now its the contrary, if there's a function, the function will
always work no matter if you have the script.

Compiled without errors and compiled it twice just in case.

Btw, just in case it mattered, the script im using is this.
MoveEvent - TFS 1.0 Advanced Item/Set Item Management
 
I've checked the baseevents.ccp and there's few diferent things from mine.
Here's mine.
Code:
#include "otpch.h"

#include "baseevents.h"

#include "pugicast.h"
#include "tools.h"

extern LuaEnvironment g_luaEnvironment;

BaseEvents::BaseEvents()
{
    m_loaded = false;
}

bool BaseEvents::loadFromXml()
{
    if (m_loaded) {
        std::cout << "[Error - BaseEvents::loadFromXml] It's already loaded." << std::endl;
        return false;
    }

    std::string scriptsName = getScriptBaseName();
    std::string basePath = "data/" + scriptsName + "/";
    if (getScriptInterface().loadFile(basePath + "lib/" + scriptsName + ".lua") == -1) {
        std::cout << "[Warning - BaseEvents::loadFromXml] Can not load " << scriptsName << " lib/" << scriptsName << ".lua" << std::endl;
    }

    std::string filename = basePath + scriptsName + ".xml";

    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file(filename.c_str());
    if (!result) {
        printXMLError("Error - BaseEvents::loadFromXml", filename, result);
        return false;
    }

    m_loaded = true;

    for (auto node : doc.child(scriptsName.c_str()).children()) {
        Event* event = getEvent(node.name());
        if (!event) {
            continue;
        }

        if (!event->configureEvent(node)) {
            std::cout << "[Warning - BaseEvents::loadFromXml] Failed to configure event" << std::endl;
            delete event;
            continue;
        }

        bool success = true;

        pugi::xml_attribute scriptAttribute = node.attribute("script");
        pugi::xml_attribute function = node.attribute("function");
        if (scriptAttribute) {
            std::string scriptFile = "scripts/" + std::string(scriptAttribute.as_string());
            success = event->checkScript(basePath, scriptsName, scriptFile) && event->loadScript(basePath + scriptFile);
        }

        if(success && function) {
            success = event->loadFunction(function);
        }

        if (!success || !registerEvent(event, node)) {
            delete event;
        }
    }
    return true;
}

bool BaseEvents::reload()
{
    m_loaded = false;
    clear();
    return loadFromXml();
}

Event::Event(LuaScriptInterface* _interface)
{
    m_scriptInterface = _interface;
    m_scriptId = 0;
    m_scripted = false;
}

Event::Event(const Event* copy)
{
    m_scriptInterface = copy->m_scriptInterface;
    m_scriptId = copy->m_scriptId;
    m_scripted = copy->m_scripted;
}

bool Event::checkScript(const std::string& basePath, const std::string& scriptsName, const std::string& scriptFile) const
{
    LuaScriptInterface* testInterface = g_luaEnvironment.getTestInterface();
    testInterface->reInitState();

    if (testInterface->loadFile(std::string(basePath + "lib/" + scriptsName + ".lua")) == -1) {
        std::cout << "[Warning - Event::checkScript] Can not load " << scriptsName << " lib/" << scriptsName << ".lua" << std::endl;
    }

    if (m_scriptId != 0) {
        std::cout << "[Failure - Event::checkScript] scriptid = " << m_scriptId << std::endl;
        return false;
    }

    if (testInterface->loadFile(basePath + scriptFile) == -1) {
        std::cout << "[Warning - Event::checkScript] Can not load script: " << scriptFile << std::endl;
        std::cout << testInterface->getLastLuaError() << std::endl;
        return false;
    }

    int32_t id = testInterface->getEvent(getScriptEventName());
    if (id == -1) {
        std::cout << "[Warning - Event::checkScript] Event " << getScriptEventName() << " not found. " << scriptFile << std::endl;
        return false;
    }
    return true;
}

bool Event::loadScript(const std::string& scriptFile)
{
    if (!m_scriptInterface || m_scriptId != 0) {
        std::cout << "Failure: [Event::loadScript] m_scriptInterface == nullptr. scriptid = " << m_scriptId << std::endl;
        return false;
    }

    if (m_scriptInterface->loadFile(scriptFile) == -1) {
        std::cout << "[Warning - Event::loadScript] Can not load script. " << scriptFile << std::endl;
        std::cout << m_scriptInterface->getLastLuaError() << std::endl;
        return false;
    }

    int32_t id = m_scriptInterface->getEvent(getScriptEventName());
    if (id == -1) {
        std::cout << "[Warning - Event::loadScript] Event " << getScriptEventName() << " not found. " << scriptFile << std::endl;
        return false;
    }

    m_scripted = true;
    m_scriptId = id;
    return true;
}

CallBack::CallBack()
{
    m_scriptId = 0;
    m_scriptInterface = nullptr;
    m_loaded = false;


bool CallBack::loadCallBack(LuaScriptInterface* _interface, const std::string& name)
{
    if (!_interface) {
        std::cout << "Failure: [CallBack::loadCallBack] m_scriptInterface == nullptr" << std::endl;
        return false;
    }

    m_scriptInterface = _interface;

    int32_t id = m_scriptInterface->getEvent(name.c_str());
    if (id == -1) {
        std::cout << "[Warning - CallBack::loadCallBack] Event " << name << " not found." << std::endl;
        return false;
    }

    m_callbackName = name;
    m_scriptId = id;
    m_loaded = true;
    return true;
}
 
Back
Top