Forum rules - please read before posting.

Remove inventory items through script. 2 player setup

Hi!

Iv got AC set up to run 2 players, turn based style. Now I want the players to be able to trade items with eachother. I set up a Unity UI menu and create a list of inventory items and buttons.

I keep track of the items the player wants to trade in List<InvItem> OtherPlayerItemList. Below is the code for the function I call when the player clicks the "trade" button:

public void ProcessSwitchButtonClick()
{
//depending on which player is currently active...
if(KickStarter.player.ID == 0)
{
//...we look at the list of items that this player wants to get rid of...
foreach(InvItem invI in OtherPlayerItemList)
{
if(invI != null)
{
//...and gives them to the other player.
KickStarter.runtimeInventory.Add(invI.id,1,false,1);
KickStarter.runtimeInventory.Remove(invI);
}
}
}
else
{
foreach(InvItem invI in OtherPlayerItemList)
{
if(invI != null)
{
KickStarter.runtimeInventory.Add(invI.id,1,false,0);
KickStarter.runtimeInventory.Remove(invI);
}
}
}
}

Now what I found out through putting in some Traces, is that the KickStarter.runtimeInventory.Add() part works perfectly, but the KickStarter.runtimeInventory.Remove() doesnt. It leaves the items in. What am I doing wrong?




Comments

  • That does fine to me, but try calling the Remove function with the extra parameters (the same that Add has) just to see if that makes a difference.
  • Thanks as always for the swift reply!
    Your suggestion works! Am I also had an error in my own code. When I build up my own menu with inventory items, I only grab the runtimeinventory items once in Start(). No wonder the items that I remove from the inventory dont get removed from that particular menu! Woops.

    I rewrote the above function slightly. This now works:

    public void ProcessSwitchButtonClick()
    {
    int ActivePlayerID = -1;
    int OtherPlayerID = -1;
    //depending on which player is currently active...
    if(KickStarter.player.ID == 0)
    {
    ActivePlayerID = 0;
    OtherPlayerID = 1;
    }
    else
    {
    ActivePlayerID = 1;
    OtherPlayerID = 0;
    }

    //...we look at the list of items that this player wants to get rid of...
    foreach(InvItem invI in OtherPlayerItemList)
    {
    if(invI != null)
    {
    //...and gives them to the other player.
    KickStarter.runtimeInventory.Add(invI.id,1,false, OtherPlayerID);
    KickStarter.runtimeInventory.Remove(invI.id, 1, true, ActivePlayerID);
    }
    }
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.