otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.59k stars 1.06k forks source link

Player.getSlotItem always returns nil #111

Closed Flatlander57 closed 11 years ago

Flatlander57 commented 11 years ago

I actually have no idea why this happens, but the function Player.getSlotItem will always return nil.

I added errors that output to the console, and it looks like it cannot find "player".

Example: Player.getSlotItem(cid, 1), whether there is an item in that slot or not, it will return nil and will output my error "Cannot find player" to console That I added in the source.

Zbizu commented 11 years ago

try(depending on what you need): getPlayerSlotItem(cid, slot).uid getPlayerSlotItem(cid, slot).itemid getPlayerSlotItem(cid, slot).actionid

and it's player:getSlotItem(slot) btw

using . instead of : will return nil, because it will try to search it as table entry instead of method registered in luascript.cpp

EvanMC commented 11 years ago

Faith2531, the function returns an Item userdata, so you cannot use those. You need to use the proper Item methods: https://github.com/EvanMC/forgottenserver/wiki/Metatable:-Item

:getId() = itemid :getActionId() = actionid :getUniqueId() = uniqueid

I've done testing with getSlotItem, it seems to be bugged. It's been crashing the server for me, no matter how I use it. So, I'd wait for Mark or Dalkon.

marksamman commented 11 years ago

Can you post example scripts that cause crash/returns nil when it shouldn't?

It's working fine for me with these examples:

        local player = Player(cid)
        local item = player:getSlotItem(CONST_SLOT_ARMOR)
        if item ~= nil then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Result: " .. item:getId())
        else
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Result: no armor")
        end

and

        local item = getPlayerSlotItem(cid, CONST_SLOT_ARMOR)
        if item.uid ~= 0 then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Result: " .. item.itemid)
        else
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Result: no armor")
        end