• 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!

Best away to move item from corpse. (Crash)

login12

void newbie scripter()
Joined
Feb 26, 2011
Messages
164
Reaction score
24
Location
Brazil
I am trying to develop a system in c ++ as a study mode but I am having some problems.
I created the code below but when it is executed the item is not "moved" to the player and the server is crashed after I leaving from the body.

C++:
            if (player->canOpenCorpse(corpseOwner)) {
                //std::cout << "You can make loot system here. Items: "; //Print for list loot on monster.
                for (Item* item : container->getItemList()) { //Int the items for show in table the loot.
                    std::cout << item->getName() << '\n';

                    if (item->getName() == "cheese") { //If item has the same name of cheese then...
                         //So lets added the item for player!
                        std::cout << "Name: " << item->getName() << ", Id: " << item->getID() << ", Quanty: " << item->getItemCount() << '\n';
                        g_game.internalPlayerAddItem(player, item, true, CONST_SLOT_WHEREEVER);
                        g_game.internalRemoveItem(item);
                    }
                }
            }

So I printed step to see where is the error and why, but the print returns with valid informations.
So, someone can tell me why it dont works?

Print information:
Code:
Name: cheese, Id: 2696, Quanty: 1

The problem is here;
C++:
g_game.internalPlayerAddItem(player, item, true, CONST_SLOT_WHEREEVER);

When I remove this line, everything work perfectly. (Prints and internalRemoveItem).
So idk why dont work.

If know some away to do it more easily, tell me how and if possible with exemples.
Thanks for u support
 
Last edited:
Solution
You should be using g_game.internalMoveItem for this, not adding + removing it.
g_game.internalMoveItem(container, player, INDEX_WHEREEVER, item, item->getItemCount(), nullptr);
You should be using g_game.internalMoveItem for this, not adding + removing it.
g_game.internalMoveItem(container, player, INDEX_WHEREEVER, item, item->getItemCount(), nullptr);
 
Solution
You should be using g_game.internalMoveItem for this, not adding + removing it.
g_game.internalMoveItem(container, player, INDEX_WHEREEVER, item, item->getItemCount(), nullptr);

Thanks for you support!
It works.
You can tell me why adding and remove result in crash?
 
Because when you addItem, if it's a stackable the item pointer can be released because the item it's merged to is kept but the count is changed, which renders the initial item useless afterwards. Then after the object is destroyed/released, you have a dangling pointer which causes a crash when you try to delete it again through internalRemoveItem, because it tries to release an already deleted object, causing a segfault.
 
Because when you addItem, if it's a stackable the item pointer can be released because the item it's merged to is kept but the count is changed, which renders the initial item useless afterwards. Then after the object is destroyed/released, you have a dangling pointer which causes a crash when you try to delete it again through internalRemoveItem, because it tries to release an already deleted object, causing a segfault.

Thanks for you time bro!
Is great know that.
 
Back
Top