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

HasItem edit


sirsk8aton
 Share

Recommended Posts

Hello, I'm trying to make it so the HasItem function has a value check also. Here's what I got:

```
Function HasItem(ByVal index As Long, ByVal itemnum As Long, itemval 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 GetPlayerInvItemValue(index, itemnum) = itemval Then
                If Item(itemnum).Type = ITEM_TYPE_CURRENCY Then
                    HasItem = GetPlayerInvItemValue(index, i)
                Else
                    HasItem = 1
                End If
            End If
            Exit Function
        End If

    Next

End Function
```
I'm not exactly sure if I did it right or not, but it is not working. Would it be as simple as how I'm thinking of it?
Link to comment
Share on other sites

A few questions:

1\. Do you only want exact values? That's what I see in the code right now - it can only match the exact value. You could add another parameter to try to check less-than/greater-than/equal-to, or you could just assume greater-than-or-equal-to.

2\. Are you updating all calls to HasItem to include the third parameter? If you aren't, then it should be producing errors for every call to HasItem(index, num) without the third 'itemval' parameter. Easy fix for that is make the third parameter optional and default to zero.

3\. Also, this will check value on every single item type, and some item types don't necessarily have a value, or the value doesn't mean anything. You'll probably want to restrict the value checking to certain item types, unless you're certain that all items where value doesn't matter will have a value of zero, in which case the default for itemval described in question 2 would be okay since it would match anyway.

Or you could expand the innermost conditional to include item types other than currency, because the function actually returns the value and not just a 1 if the item type is currency. Using that, you wouldn't need to check for itemval inside of the function, and you could simply check the returned value yourself in the calling code, which would eliminate the need for any value checks in the function.

In short:
1\. You're going to need to modify the function a bit
OR
2\. Use the return value to do your own checks from the calling code.

Honestly, the simpler answer is probably going to be the second one because it gives you more flexibility in the calling code and keeps the function simpler than it would be if you added stuff in to fit situation 1.
Link to comment
Share on other sites

Thank you for your reply!

1\. I was actually looking to check if it is greater than or equal to, that was just a small mistake.

2\. I was setting the third parameter in the code that I trying to check the item value. I haven't gotten any errors otherwise though.

3\. I was also thinking about this. I was thinking about adding in a new item type for quest items. That's the main reason I'm doing this, to check if the player has a certain amount of a quest item.

The currency value check was already in the code before I started fiddling with it. Honestly, I don't have much experience with the source, and I'm just trying to learn on my own. Would I be able to eliminate situation 1 and somehow check if a player has a certain amount of a certain item without messing with the function?
Link to comment
Share on other sites

Actually, I think I'm just going to create a new item type for quest items and do this, if it would work..

```
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)
            ElseIf Item(itemnum).Type = ITEM_TYPE_QUESTITEM Then
                HasItem = GetPlayerInvItemValue(index, i)
            Else
                HasItem = 1
            End If

            Exit Function
        End If

    Next

End Function
```
So I would call it by:
```
If HasItem(index, 15) >= 5 Then
```
Say item number 15 is a quest item.. I would make them stackable so they would have a value. This would work, no?
Link to comment
Share on other sites

That would work, but there's no reason to make separate conditionals that have the same result.
This:
```
If Item(itemnum).Type = ITEM_TYPE_CURRENCY Then
    HasItem = GetPlayerInvItemValue(index, i)
ElseIf Item(itemnum).Type = ITEM_TYPE_QUESTITEM Then
    HasItem = GetPlayerInvItemValue(index, i)
Else
    HasItem = 1
End If
```could be rewritten better as this:
```
If Item(itemnum).Type = ITEM_TYPE_CURRENCY Or Item(itemnum).Type = ITEM_TYPE_QUESTITEM Then
    HasItem = GetPlayerInvItemValue(index, i)
Else
    HasItem = 1
End If
```See what I did there? I linked the two conditions with an Or, so if either of those conditions matches, it will return the value.

Also, the example code you posted:
```
If HasItem(index, 15) >= 5 Then
```was exactly what I was talking about because then you have minimal modification of the function, and you simply check the value that HasItem returns to see if the value is within a certain range - hence, you understand situation 2.
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...