Unrealscript: how to add a weapon to the inventory only if your weapon's group is unique

2k views Asked by At

I have been trying to get a custom inventory script working in unrealscript. I need to check on weapon pickup if the weapon you are picking up has a weapon group that matches one you already have or not. If so it swaps the weapons of the given group. If not you gain the new weapon. Currently we have 4 weapon groups. What i have tried to no avail to find is how to find the weapon group of a weapon being picked up. and how to check that against the other weapons. I have been working with the add inventory function.

NewItem is the item you just interacted with. And i need to find the other weapons in your current inventory and their weapon groups.

simulated function bool AddInventory(Inventory NewItem, optional bool bDoNotActivate)
{
    local Inventory Item, LastItem;

    // The item should not have been destroyed if we get here.
    if( (NewItem != None) && !NewItem.bDeleteMe )
    {
        // if we don't have an inventory list, start here
        if( InventoryChain == None )
        {
            InventoryChain = newItem;
        }
        else
        {
            // Skip if already in the inventory.
            for (Item = InventoryChain; Item != None; Item = Item.Inventory)
            {
                if( Item == NewItem )
                {
                    return FALSE;
                }
                LastItem = Item;
            }
            LastItem.Inventory = NewItem;
        }

        //Edited by Matt Kerns

        `LogInv("adding" @ NewItem @ "bDoNotActivate:" @ bDoNotActivate);

        `Log ("Added to inventory!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        NewItem.SetOwner( Instigator );
        NewItem.Instigator = Instigator;
        NewItem.InvManager = Self;
        NewItem.GivenTo( Instigator, bDoNotActivate);

        // Trigger inventory event
        Instigator.TriggerEventClass(class'SeqEvent_GetInventory', NewItem);
        return TRUE;
    }

    return FALSE;
}
1

There are 1 answers

0
Wo1fer On BEST ANSWER

If you want write all code in InventoryManager it will be look like

simulated function bool AddInventory(Inventory NewItem, optional bool bDoNotActivate){
local Inventory Item, LastItem;

// The item should not have been destroyed if we get here.
if( (NewItem != None) && !NewItem.bDeleteMe )
{
//if weapon - work in personal weapon function
if(Weapon(NewItem) != none)
        return AddItemWeapon(NewItem, bDoNotActivate);
    // if we don't have an inventory list, start here
    if( InventoryChain == None )
    {
        InventoryChain = newItem;
    }
    else
    {
        // Skip if already in the inventory.
        for (Item = InventoryChain; Item != None; Item = Item.Inventory)
        {
            if( Item == NewItem )
            {
                return FALSE;
            }
            LastItem = Item;
        }
        LastItem.Inventory = NewItem;
    }

    //Edited by Matt Kerns

    `LogInv("adding" @ NewItem @ "bDoNotActivate:" @ bDoNotActivate);

    `Log ("Added to inventory!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    NewItem.SetOwner( Instigator );
    NewItem.Instigator = Instigator;
    NewItem.InvManager = Self;
    NewItem.GivenTo( Instigator, bDoNotActivate);

    // Trigger inventory event
    Instigator.TriggerEventClass(class'SeqEvent_GetInventory', NewItem);
    return TRUE;
}

return FALSE;

}

simulated function AddItemWeapon(Inventory NewItem, optional bool bDoNotActivate) {

local Inventory Item, LastItem;
local Weapon WeaponNewItem;

WeaponNewItem= Weapon(NewItem);

// The item should not have been destroyed if we get here.
if( (NewItem != None) && !NewItem.bDeleteMe )
{
    // if we don't have an inventory list, start here
    if( InventoryChain == None )
    {
        InventoryChain = newItem;
    }
    else
    {
        //look for our group
        for (Item = InventoryChain; Item != None; Item = Item.Inventory){
            if(Weapon(Item) != none && Weapon(Item).Group == WeaponNewItem.Group){
                RemoveFromInventory(Item);
                break;
            }

        }
            // Skip if already in the inventory.
            for (Item = InventoryChain; Item != None; Item = Item.Inventory)
            {
                if( Item == NewItem )
                {
                    return FALSE;
                }
                LastItem = Item;
            }
            LastItem.Inventory = NewItem;

    }

    //Edited by Matt Kerns

    `LogInv("adding" @ NewItem @ "bDoNotActivate:" @ bDoNotActivate);

    `Log ("Added to inventory!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    NewItem.SetOwner( Instigator );
    NewItem.Instigator = Instigator;
    NewItem.InvManager = Self;
    NewItem.GivenTo( Instigator, bDoNotActivate);

    // Trigger inventory event
    Instigator.TriggerEventClass(class'SeqEvent_GetInventory', NewItem);
    return TRUE;
}

return FALSE;

}

This is best way if your game use network and replicate inventory. If not, you can simple write something like

simulated function AddItemWeapon(Inventory NewItem, optional bool bDoNotActivate){
local Inventory Item, LastItem;
local Weapon WeaponNewItem;
local bool bAddNewWeapon;

bAddNewWeapon = true;
WeaponNewItem = Weapon(NewItem);

// The item should not have been destroyed if we get here.
if( (NewItem != None) && !NewItem.bDeleteMe )
{
    // if we don't have an inventory list, start here
    if( InventoryChain == None )
    {
        InventoryChain = newItem;
    }
    else
    {
        //look for our group
        for (Item = InventoryChain; Item != None; Item = Item.Inventory){
            if(Weapon(Item) != none && Weapon(Item).Group == WeaponNewItem.Group){
                NewItem.Inventory = Item.Inventory; 
                bAddNewWeapon = false;
                RemoveFromInventory(Item);
                break;
            }
            LastItem = Item;

        }
        if(bAddNewWeapon){
            for (Item = InventoryChain; Item != None; Item = Item.Inventory){
                LastItem = Item;
            }
        }
        LastItem.Inventory = NewItem;
    }

But im sure that this is bad way. You should write class which will save all used weapon groups and use InventoryManager only as storage