Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[EO] GetAmountOfItem Function


sirsk8aton
 Share

Recommended Posts

In EO, there's a function that will check if you have a certain item in your inventory, but not how many of that item you have(unless it's currency) This function will return the amount of the specified item.

Please note this is not used for currency, use the HasItem function to get the amount of money you have. This will count each slot that the specified item is in and return that number.

**Server-Side**
In modPlayer, all the way at the bottom add this:
```
Function GetAmountOfItem(ByVal index As Long, ByVal itemNum As Long)
    Dim i As Long, counter As Long
    counter = 0

    ' Check for subscript out of range
    If IsPlaying(index) = False Or itemNum <= 0 Or itemNum > MAX_ITEMS Then
        Exit Function
    End If

    For i = 1 To MAX_INV
        If GetPlayerInvItemNum(index, i) = itemNum Then
            counter = counter + 1
        End If
    Next

    GetAmountOfItem = counter

End Function

```
Now you will be able to check how many of an item you have in the inventory.
Example:
```
if GetAmountOfItem(Index, 10) >= 5
    'Has the item and specific amount
Else
    'Doesn't have all the items
End If

```
If you have the amount 5 or greater of the item 10, it will go to the first comment, if not it will go to the second comment. Hope this turns out helpful to someone. Enjoy.
Link to comment
Share on other sites

Why not
```
    Dim itemamount As Long

    itemamount = HasItem(Index, 1)

    If itemamount = 0 Or itemamount < 5000 Then
        PlayerMsg Index, "Not enough item.", BrightRed
        Exit Sub
    End If

    TakeInvItem Index, 1, 5000

```
Replace the numbers with variables and you got what your code does with out all those loops.
Link to comment
Share on other sites

Sure it can. orginally no. but HasItem is a basic check to see if the person has that item you want to take.

Also You don't have to create an entire new sub. Just edit the current HasItem function to be like the following.

```
Function HasItem(ByVal index As Long, ByVal itemnum As Long, Optional ByVal long itemAmount) As Long
    Dim i As Long

    ' Check for subscript out of range
    If IsPlaying(index) = False Or itemnum <= 0 Or itemnum > MAX_ITEMS Then
        Exit Function
    End If

    For i = 1 To MAX_INV

        ' Check to see if the player has the item
        If GetPlayerInvItemNum(index, i) = itemnum Then
            If Item(itemnum).Type = ITEM_TYPE_CURRENCY Then
                HasItem = GetPlayerInvItemValue(index, i)
            Else
                If itemAmount > 0
                    if Player(index).Inv.Num >= itemAmount
                        HasItem = GetPlayerInvItemValue(index, i)
                Else
                    HasItem = 1
            End If

            Exit Function
        End If

    Next

End Function
```
Code will prolly not work. Been a while since i messed around with EO but you get the drift of all you have to do is have an Optional value passed in. if its not passed in it will be 0.
Link to comment
Share on other sites

It already does that only need a small edit for non currency.

```
Function HasItem(ByVal Index As Long, ByVal itemnum As Long) As Long
    Dim i As Long

    ' Check for subscript out of range
    If IsPlaying(Index) = False Or itemnum <= 0 Or itemnum > MAX_ITEMS Then
        Exit Function
    End If

    For i = 1 To MAX_INV

        ' Check to see if the player has the item
        If GetPlayerInvItemNum(Index, i) = itemnum Then
----------> If Item(itemnum).Type = ITEM_TYPE_CURRENCY Then
------------>  HasItem = GetPlayerInvItemValue(Index, i)
            Else
------------>  HasItem = 1
            End If

            Exit Function
        End If

    Next

End Function

```GetPlayerInvItemValue returns the value(amount) of that item if it is currency(can be edited for others) and returns 1 for non stackable single items.
Link to comment
Share on other sites

I pasted it to point out the sub already checks for the amount, I pointed out the lines where it returns the value(thus re-affirming it needed no edits) which was the whole point of this tutorial than if you look how shops handle transaction you get a great example of working with stacked items(the code I posted above).
Link to comment
Share on other sites

No point in arguing with a brick wall, but point blank he was trying to check to see if a person had multiple of an item and the fact is the current function does what he wants fine.  All you need to do is call HasItem, if it is greater than 1 than it is stackable(currency) if not its a single item.

> How do you know he wants to check for a stacked item everytime

You just call that block of code(below) where he needs to check for the stack why are we even editing the sub?  I could see if he made a sub that checks than removed it(like I think he was trying to do) but he went around it the wrong way.

You just use this(ripped from shops) throw this in a function, replace the numbers with variables bam done.
```
    Dim itemamount As Long

    itemamount = HasItem(Index, 1)

    If itemamount = 0 Or itemamount < 5000 Then
        PlayerMsg Index, "Not enough item.", BrightRed
        Exit Sub
    End If

    TakeInvItem Index, 1, 5000

```
Link to comment
Share on other sites

What I'm trying to actually do is check for x of a non-stackable item. What I use this for in my game is for quests. Say you need to go grab 5 non-stackable items. The HasItem function can check if I have the item, but can not check for  5 of them in my inventory at one time.

The function I made takes each item out of the inventory and counts how many it takes. If it's not enough for the quest I need, then it gives back the number of items it took. If it counts the number that I passed into the function(for ItemAmountReq), then it will return true, ending the quest.
Link to comment
Share on other sites

There we go, just edited the HasItem function to check if the third parameter is not equal to 0\. Then it will count all the specified items throughout the for loop of all the inventory spaces. If the item was counted to match the itemamount parameter, it will return 1.
Link to comment
Share on other sites

Wouldn't it be more useful to return the quantity they have, not a true/false value?

GetNumberOfItem(X) seems more useful than HasItems(X,5000)

ie (very pseudocode-ish)
var ore_count = GetNumberOfItem(Ore);
if ore_count > 5 do stuff, else playermsg("you need " & (5 - ore_count) & " more ore.)

also would let you do stuff like
var items_redeemed = GetNumberOfItem(Redeemable)
grant experience = items_redeemed * 500xp
Link to comment
Share on other sites

Well maybe you seperate the two functions if HasItem has existing code references to it, otherwise I would supersede HasItem with GetNumberOfItem. Or even make the hasitem function just run the getnumberofItem function and if the quantity is >1 return true. Up to you really.

ie HasITem returns a true false as its function name implies,

GetNumberOfItem returns the quantity a player posesses.
Link to comment
Share on other sites

@scott - brick wall? there is no brick wall. its two different views. And your first post made little since. If you give a description of ripping apart one Function/Sub at least give more details. Your second post of what you ment makes more since to me because you went into more detail.

On Topic - Either way you choose to do the system is fine. Adding a new function is a little bit of a waste when you can just add it to the current function. Either way its still a nice add to the HasItem function.
Link to comment
Share on other sites

As Baron stated, the HasItem should do what it sounds like it supposed to do. Check to see if you have the item and return true/false. GetAmountOfItem clearly states what it's supposed to do and does it. Again, thanks for the input. All of you actually helped me do what I needed to do the right way.
Link to comment
Share on other sites

Nice job on finishing it up, your first piece of code had so much useless looping.  My whole point was why where you editing HasItem its used by other areas glad to see you got it working as you intended and made it separate.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...