• 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 changegold.lua

zxzxzx

New Member
Joined
Mar 12, 2011
Messages
334
Reaction score
3
Hello! I have " Table index is nil stack tradeback: (Line 5)

CJ in function "new___index"

my script:
Code:
local config = {
    [ITEM_GOLD_COIN] = {changeTo = ITEM_PLATINUM_COIN},
    [ITEM_PLATINUM_COIN] = {changeBack = ITEM_GOLD_COIN, changeTo = ITEM_CRYSTAL_COIN},
    [ITEM_CRYSTAL_COIN] = {changeBack = ITEM_PLATINUM_COIN, changeTo = ITEM_GOLD_NUGGET},
    [ITEM_GOLD_NUGGET] = {changeBack = ITEM_CRYSTAL_COIN, changeTo = ITEM_GOLD_INGOT},
    [ITEM_GOLD_INGOT] = {changeBack = ITEM_GOLD_NUGGET}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local coin = config[item:getId()]
    if coin.changeTo and item.type == 100 then
        item:remove()
        player:addItem(coin.changeTo, 1)
    elseif coin.changeBack then
        item:remove(1)
        player:addItem(coin.changeBack, 100)
    else
        return false
    end
    return true
end
 
Ok Friend Im trying to compile but I have this errors:

http://paste.ofcode.org/pJ8RXEy2CMSnGE8SKQePhw

My game.cpp - now trying to make new value of cash (gold ingot and bar of gold) the code:
Code:
void Game::addMoney(Cylinder* cylinder, uint64_t money, uint32_t flags /*= 0*/)
{
    {
    uint32_t baro = money / 100000000;
    money -= baro * 100000000;
    while (baro > 0) {
        Item* remaindItem = Item::CreateItem(ITEM_BAR_OF_GOLD, std::min<int32_t>(100, baro));

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        baro -= std::min<int32_t>(100, baro);
    }
    {
    uint32_t ingo = money / 1000000;
    money -= ingo * 1000000;
    while (ingo > 0) {
        Item* remaindItem = Item::CreateItem(ITEM_GOLD_INGOT, std::min<int32_t>(100, ingo));

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        ingo -= std::min<int32_t>(100, ingo);
    }
    uint32_t crys = money / 10000;
    money -= crys * 10000;
    while (crys > 0) {
        Item* remaindItem = Item::CreateItem(ITEM_CRYSTAL_COIN, std::min<int32_t>(100, crys));

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        crys -= std::min<int32_t>(100, crys);
    }

    uint16_t plat = money / 100;
    if (plat != 0) {
        Item* remaindItem = Item::CreateItem(ITEM_PLATINUM_COIN, plat);

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        money -= plat * 100;
    }

    if (money != 0) {
        Item* remaindItem = Item::CreateItem(ITEM_GOLD_COIN, money);

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }
    }
}
 
Ok Friend Im trying to compile but I have this errors:

http://paste.ofcode.org/pJ8RXEy2CMSnGE8SKQePhw

My game.cpp - now trying to make new value of cash (gold ingot and bar of gold) the code:
Code:
void Game::addMoney(Cylinder* cylinder, uint64_t money, uint32_t flags /*= 0*/)
{
    {
    uint32_t baro = money / 100000000;
    money -= baro * 100000000;
    while (baro > 0) {
        Item* remaindItem = Item::CreateItem(ITEM_BAR_OF_GOLD, std::min<int32_t>(100, baro));

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        baro -= std::min<int32_t>(100, baro);
    }
    {
    uint32_t ingo = money / 1000000;
    money -= ingo * 1000000;
    while (ingo > 0) {
        Item* remaindItem = Item::CreateItem(ITEM_GOLD_INGOT, std::min<int32_t>(100, ingo));

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        ingo -= std::min<int32_t>(100, ingo);
    }
    uint32_t crys = money / 10000;
    money -= crys * 10000;
    while (crys > 0) {
        Item* remaindItem = Item::CreateItem(ITEM_CRYSTAL_COIN, std::min<int32_t>(100, crys));

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        crys -= std::min<int32_t>(100, crys);
    }

    uint16_t plat = money / 100;
    if (plat != 0) {
        Item* remaindItem = Item::CreateItem(ITEM_PLATINUM_COIN, plat);

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }

        money -= plat * 100;
    }

    if (money != 0) {
        Item* remaindItem = Item::CreateItem(ITEM_GOLD_COIN, money);

        ReturnValue ret = internalAddItem(cylinder, remaindItem, INDEX_WHEREEVER, flags);
        if (ret != RETURNVALUE_NOERROR) {
            internalAddItem(cylinder->getTile(), remaindItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
        }
    }
}
If you are going to copy and paste code at least don't over write existing code
This should come before everything else
Code:
    if (money == 0) {
        return;
    }

Did you define ITEM_BAR_OF_GOLD & ITEM_GOLD_INGOT elsewhere?
 
yes:

items.cpp
Code:
int32_t Item::getWorth() const
{
    switch (id) {
        case ITEM_GOLD_COIN:
            return count;

        case ITEM_PLATINUM_COIN:
            return count * 100;

        case ITEM_CRYSTAL_COIN:
            return count * 10000;
           
        case ITEM_GOLD_INGOT:
            return count * 1000000;
           
        case ITEM_BAR_OF_GOLD:
            return count * 100000000;

        default:
            return 0;
    }
}
player.cpp
Code:
bool Player::updateSaleShopList(const Item* item)
{
    uint16_t itemId = item->getID();
    if (itemId != ITEM_GOLD_COIN && itemId != ITEM_PLATINUM_COIN && itemId != ITEM_CRYSTAL_COIN && itemId != ITEM_GOLD_INGOT && itemId != ITEM_BAR_OF_GOLD) {
        auto it = std::find_if(shopItemList.begin(), shopItemList.end(), [itemId](const ShopInfo& shopInfo) { return shopInfo.itemId == itemId && shopInfo.sellPrice != 0; });
        if (it == shopItemList.end()) {
            const Container* container = item->getContainer();
            if (!container) {
                return false;
            }
const.cpp
Code:
    ITEM_GOLD_COIN = 2148,
    ITEM_PLATINUM_COIN = 2152,
    ITEM_CRYSTAL_COIN = 2160,
    ITEM_GOLD_INGOT = 9971,
    ITEM_BAR_OF_GOLD = 15515,
 
and luascript.cpp

Code:
    registerEnum(ITEM_GOLD_COIN)
    registerEnum(ITEM_PLATINUM_COIN)
    registerEnum(ITEM_CRYSTAL_COIN)
    registerEnum(ITEM_GOLD_INGOT)
    registerEnum(ITEM_BAR_OF_GOLD)
 
If you are going to copy and paste code at least don't over write existing code
This should come before everything else
Code:
    if (money == 0) {
        return;
    }

Did you define ITEM_BAR_OF_GOLD & ITEM_GOLD_INGOT elsewhere?



I don't have clue where I must put 'if (money == 0) { return; }" - I already see it in script - probably I'm doing something wrong, can you edit it for me?
 
Well since I don't like to mess with source I would do it all in LUA.
All you need is to define the itemID worth and when trading with NPC's, add few extra lines which will check do you have these items in bag to increase the actual amount of gold you have.
 
Well since I don't like to mess with source I would do it all in LUA.
All you need is to define the itemID worth and when trading with NPC's, add few extra lines which will check do you have these items in bag to increase the actual amount of gold you have.

I already have changegold.lua but the script don't work:

actions.xml
Code:
    <action itemid="2148" script="other/changegold.lua"/>
    <action itemid="2152" script="other/changegold.lua"/>
    <action itemid="2160" script="other/changegold.lua"/>
    <action itemid="15515" script="other/changegold.lua"/> <!-- 1 bar of gold -->
    <action itemid="9971" script="other/changegold.lua"/> <!-- 1 Gold Ingot -->

changegold.lua:

Code:
local config = {
    [ITEM_GOLD_COIN] = {changeTo = ITEM_PLATINUM_COIN},
    [ITEM_PLATINUM_COIN] = {changeBack = ITEM_GOLD_COIN, changeTo = ITEM_CRYSTAL_COIN},
    [ITEM_CRYSTAL_COIN] = {changeBack = ITEM_PLATINUM_COIN, changeTo = ITEM_GOLD_INGOT},
    [ITEM_GOLD_INGOT] = {changeBack = ITEM_CRYSTAL_COIN, changeTo = ITEM_BAR_OF_GOLD},
    [ITEM_BAR_OF_GOLD] = {changeBack = ITEM_GOLD_INGOT}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local coin = config[item:getId()]
    if coin.changeTo and item.type == 100 then
        item:remove()
        player:addItem(coin.changeTo, 1)
    elseif coin.changeBack then
        item:remove(1)
        player:addItem(coin.changeBack, 100)
    else
        return false
    end
    return true
end
 
Then where I'm must put it? (sorry for my noobish but I'm realy bad in scripting.
Start learning!
Code:
-- before you can use a variable it must be defined 1st
ITEM_GOLD_INGOT = 9971
ITEM_BAR_OF_GOLD = 15515

local config = {
    [ITEM_GOLD_COIN] = {changeTo = ITEM_PLATINUM_COIN},
    [ITEM_PLATINUM_COIN] = {changeBack = ITEM_GOLD_COIN, changeTo = ITEM_CRYSTAL_COIN},
    [ITEM_CRYSTAL_COIN] = {changeBack = ITEM_PLATINUM_COIN, changeTo = ITEM_GOLD_INGOT},
    [ITEM_GOLD_INGOT] = {changeBack = ITEM_CRYSTAL_COIN, changeTo = ITEM_BAR_OF_GOLD},
    [ITEM_BAR_OF_GOLD] = {changeBack = ITEM_GOLD_INGOT}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local coin = config[item:getId()]
    if coin.changeTo and item.type == 100 then
        item:remove()
        player:addItem(coin.changeTo, 1)
    elseif coin.changeBack then
        item:remove(1)
        player:addItem(coin.changeBack, 100)
    else
        return false
    end
    return true
end
 
Back
Top