Unrealscript:如果你的武器组是唯一的,如何将武器添加到库存中

时间:2012-02-01 20:42:57

标签: unreal-development-kit unrealscript

我一直在努力让自定义库存脚本在unrealscript中运行。如果你拿起的武器有一个与你已经拥有的武器组相匹配的武器组,我需要检查武器拾取。如果是这样,它交换给定组的武器。如果没有,你获得新武器。目前我们有4个武器组。我试图找不到的是如何找到被捡起的武器的武器组。以及如何检查其他武器。我一直在使用添加库存功能。

NewItem是您刚刚与之互动的项目。我需要在你当前的库存及其武器组中找到其他武器。

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 个答案:

答案 0 :(得分:0)

如果您想在InventoryManager中编写所有代码,它将看起来像

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;

}

模拟函数AddItemWeapon(Inventory NewItem,可选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;

}

如果您的游戏使用网络并复制广告资源,这是最佳方式。如果没有,你可以简单地写一些像

这样的东西
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;
    }

但我确定这是不好的方式。您应该编写将保存所有已使用武器组的类,并仅将InventoryManager用作存储