Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Weight To An Item
#1
The feature of adding weight to your items was brought on by James and John's Fabulatra. I figured why not add it. But, as it turns out, since I'm still new, it took me awhile to get it done! Currently all it does is check to see when to add or subtract from the player's weight. Whenever you get/give an item from the ground/shop, it should update your current weight and tell you when you are carrying too much. I leave it up to anyone who wants to use to decide what happens when you are carrying too much weight. Without further ado, let's begin.

Difficulty 3/5 – Comprehension (I tried to not make it a C + P, but I fail at making things hard ^_^)

Client Side

Start off in ModTypes.

Add this into ItemRec, at the bottom
Code:
Weight As Double

Inside PlayerRec, just under the inventory category, add this:
Code:
CurWeight As Long
MaxWeight As Long

Now go into ModGameLogic.
Add the following functions/subs somewhere in there (preferably at the bottom):
Code:
Function GetPlayerCurWeight(ByVal Index As Long) As Double
    GetPlayerCurWeight = Player(Index).CurWeight
End Function

Sub SetPlayerCurWeight(ByVal Index As Long, ByVal Modifier As Double)
    Player(Index).CurWeight = Modifier
End Sub

Function GetPlayerMaxWeight(ByVal Index As Long) As Long
    GetPlayerMaxWeight = Player(Index).MaxWeight
End Function


Sub SetPlayerMaxWeight(ByVal Index As Long, ByVal Modifier As Long)
    Player(Index).MaxWeight = Modifier
End Sub

Now, find the sub “ItemEditorInit” without the quotes of course.
Add this in before the first if statement:
Code:
frmItemEditor.txtWeight.Text = Item(EditorIndex).Weight

Now find the sub “ItemEditorOK”.
Add this before the first if statement:
Code:
Item(EditorIndex).Weight = frmItemEditor.txtWeight.Text

Now find the sub “ClearPlayer”
Add this line right before the armor statements (after the inventory clear):
Code:
Player(Index).CurWeight = 0
    Player(Index).MaxWeight = 0

Now find the sub “ClearItem”.
Add this line at the end:
Code:
Item(Index).Weight = 0

Moving on to ModClientTCP
Find the sub “SendSaveItem”.
Inside the packet part, remove the END_CHAR and add this:
Code:
& .Weight & END_CHAR

Go to ModHandleData
Find the “Edit item packet”
Add this at the end of the “ 'Update the Item” block of code
Code:
Item(n).Weight = Val(Parse(8))

Find the “Update item packet”
Add this right before it says exit sub
Code:
Item(n).Weight = Val(Parse(5))

Now, go into frmItemEditor
Add a text box and a label.
Text Box.Name = txtWeight
Label.Name = lblWeight
Lablel.Caption = Weight

That is all you have to do client side, now onto...

Server Side

Start off in ModTypes again
Add the same variables as you did in the Client

Go to ModGameLogic and add these in there
Code:
Function GetPlayerCurWeight(ByVal Index As Long) As Double
    GetPlayerCurWeight = Player(Index).Char(Player(Index).CharNum).CurWeight
End Function

Sub SetPlayerCurWeight(ByVal Index As Long, ByVal Modifier As Double)
    Player(Index).Char(Player(Index).CharNum).CurWeight = Modifier
End Sub

Function GetPlayerMaxWeight(ByVal Index As Long) As Long
    GetPlayerMaxWeight = Player(Index).Char(Player(Index).CharNum).MaxWeight
End Function

Sub SetPlayerMaxWeight(ByVal Index As Long, ByVal Modifier As Long)
    Player(Index).Char(Player(Index).CharNum).MaxWeight = Modifier
End Sub

Now find the sub “TakeItem”.
Add the top of the sub add a boolean called curtake.
Find “ ' Is what we are trying to take away more then what they have? If so just set it to zero” and replace that and the lines below it (until “ ' Check to see if its any sort of ArmorSlot/WeaponSlot”) with this:
Code:
' Is what we are trying to take away more then what they have?  If so just set it to zero
                If ItemVal >= GetPlayerInvItemValue(Index, i) Then
                    TakeItem = True
                    takecur = True
                Else
                    Call SetPlayerInvItemValue(Index, i, GetPlayerInvItemValue(Index, i) - ItemVal)
                    Call SendInventoryUpdate(Index, i)
                End If
            Else
                takecur = False

Find the sub “GiveItem”.
Add this before the SendInventoryUpdate
Code:
Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) + (Item(GetPlayerInvItemNum(Index, i)).Weight * GetPlayerInvItemValue(Index, i))))
        If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are overencumbered!", White)

Find the Sub “PlayerMapGetItem”.
Replace the if statement
Code:
If n  0
with:
Code:
If n  0 Then
                    ' Set item in players inventor
                    Call SetPlayerInvItemNum(Index, n, MapItem(MapNum, i).Num)
                    If Item(GetPlayerInvItemNum(Index, n)).Type = ITEM_TYPE_CURRENCY Then
                        Call SetPlayerInvItemValue(Index, n, GetPlayerInvItemValue(Index, n) + MapItem(MapNum, i).Value)
                        Msg = "You picked up " & MapItem(MapNum, i).Value & " " & Trim(Item(GetPlayerInvItemNum(Index, n)).Name) & "."
                        Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) + (Item(GetPlayerInvItemNum(Index, n)).Weight * GetPlayerInvItemValue(Index, n))))
                    Else
                        Call SetPlayerInvItemValue(Index, n, 0)
                        Msg = "You picked up a " & Trim(Item(GetPlayerInvItemNum(Index, n)).Name) & "."
                        Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) + (Item(GetPlayerInvItemNum(Index, n)).Weight)))
                    End If
                    Call SetPlayerInvItemDur(Index, n, MapItem(MapNum, i).Dur)
                        
                    ' Erase item from the map
                    MapItem(MapNum, i).Num = 0
                    MapItem(MapNum, i).Value = 0
                    MapItem(MapNum, i).Dur = 0
                    MapItem(MapNum, i).x = 0
                    MapItem(MapNum, i).y = 0
                                            
                    Call SendInventoryUpdate(Index, n)
                    Call SpawnItemSlot(i, 0, 0, 0, GetPlayerMap(Index), GetPlayerX(Index), GetPlayerY(Index))
                    Call PlayerMsg(Index, Msg, Yellow)
                    If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are overencumbered!", White)
                    Exit Sub
                Else
                    Call PlayerMsg(Index, "Your inventory is full.", BrightRed)
                    Exit Sub
                End If

Find the Sub “PlayerDropMapItem”.
Replace the if statement
Code:
If Item(GetPlayerInvItemNum(Index, InvNum)).Type = ITEM_TYPE_CURRENCY Then
with:
[code]If Item(GetPlayerInvItemNum(Index, InvNum)).Type = ITEM_TYPE_CURRENCY Then
' Check if its more then they have and if so drop it all
If Ammount >= GetPlayerInvItemValue(Index, InvNum) Then
MapItem(GetPlayerMap(Index), i).Value = GetPlayerInvItemValue(Index, InvNum)
Call MapMsg(GetPlayerMap(Index), GetPlayerName(Index) & " drops " & GetPlayerInvItemValue(Index, InvNum) & " " & Trim(Item(GetPlayerInvItemNum(Index, InvNum)).Name) & ".", Yellow)
Call SetPlayerInvItemNum(Index, InvNum, 0)
Call SetPlayerInvItemValue(Index, InvNum, 0)
Call SetPlayerInvItemDur(Index, InvNum, 0)
Else
MapItem(GetPlayerMap(Index), i).Value = Ammount
Call MapMsg(GetPlayerMap(Index), GetPlayerName(Index) & " drops " & Ammount & " " & Trim(Item(GetPlayerInvItemNum(Index, InvNum)).Name) & ".", Yellow)
Call SetPlayerInvItemValue(Index, InvNum, GetPlayerInvItemValue(Index, InvNum) - Ammount)
End If
Call SetPlayerCurWeight(Index, (GetPlayerCurWeight(Index) - (Item(GetPlayerInvItemNum(Index, InvNum)).Weight * GetPlayerInvItemValue(Index, InvNum))))
Else
' Its not a currency object so this is easy
MapItem(GetPlayerMap(Index), i).Value = 0
If Item(GetPlayerInvItemNum(Index, InvNum)).Type >= ITEM_TYPE_WEAPON And Item(GetPlayerInvItemNum(Index, InvNum)).Type
Reply
#2
I prefer slots as opposed to weight.

That's what I used in GS, and when you equipped a backpack, it added more slots.
Reply
#3
In my game you have 15 inventory slots and you can get up to 10 more through packs/pouches. I'm not sure if I'm having this weight system. Just made it for the hell of it.
Reply
#4
I prefer movable slots like most good games have. This is a pretty cool idea though.
Reply
#5
I want to point something out.

You defined Weight as a Double. Too big. Single should be fine. Unless you're planning to have each player be a super character. I'm pretty sure the average human can't lift more than 120 pounds... Single can cover that. Tongue

I'm from the US, I regret not knowing the metric system as well as the rest of the world. >.<

Also, CurWeight and MaxWeight, why are they Longs? Longs can't handle floating point numbers. So, if you have a limit of 3kg, and your item is 2.6 kg, if you use Fix(), your cur becomes 2[I'm assuming that's how you did it] instead of .4.

I'm editing this as i read the tut, so, I'll post when I've read this all the way through.

Other than what I mentioned, it's fine. I love weight, as opposed to slots. Makes games more realistic. Though, Pokemon doesn't call for it. Everything is a capsule, lol. So, yeah, good tut.

And I'm done. xD
Reply
#6
CurWeight As Long was a mistake...I changed that to double later, after I finished the first part of the tutorial.
MaxWeight As Long is not a mistake though. Since I like your max weight to be a solid number, I made it a long.

And yes, it is somewhat of a waste to use a double, but I figured if someone wanted to have uber1337 extreme values, they could...
Reply
#7
Just giving you my input.

Btw, I dunno when you've come but I've been seeing you alot.

Welcome. And nice tut. And yeah, super uber weapons. Didn't consider that. Thinking as a player, not a developer. Tongue
Reply
#8
Matt Wrote:Btw, I dunno when you've come but I've been seeing you alot.

If you're referring to me, I have been around for a little over a year and got more active when I decided to start up a project, Portal To Bakaria. I came from playerworlds...
Reply
#9
Stomach Pulser Wrote:
Matt Wrote:Btw, I dunno when you've come but I've been seeing you alot.

If you're referring to me, I have been around for a little over a year and got more active when I decided to start up a project, Portal To Bakaria. I came from playerworlds...

Sweet. Welcome. Have fun! Tongue
Reply
#10
Welcoming me when I joined over a year ago. Me no understand explode logic brain gah!
Reply
#11
Matt.. wtf?
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply
#12
Robin Wrote:Matt.. wtf?

I was indeed thinking the same thing...

I hadn't realized what I was doing. xD

It's this stupid programming class. Lights are dimmed, and I go to sleep alot. xD
Reply
#13
I found a bug.

If my max Weight 80, and my cur weight 79, but i want to pick an item when have 10 weight, i can... It says curr weight 89/ max weight 80... This is only in my source or the tutorial buggy? :roll:
Reply
#14
Does it output that you have to much weight? So far, the code let's you pick up as much weight as you want without penalties, but it should say that you are overencumbered.
Reply
#15
Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are over encumbered!", White)

Thats a key line of code for this system. If you want being over encumbered to carry penalties, that's pretty much how you would do it. I would probably take the above line of code and use it to determine whether a player can walk or not. Putting this in the appropriate sub would work:

Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then CanMove = False
Reply
#16
Sonire Wrote:
Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then Call PlayerMsg(Index, "You are over encumbered!", White)

Thats a key line of code for this system. If you want being over encumbered to carry penalties, that's pretty much how you would do it. I would probably take the above line of code and use it to determine whether a player can walk or not. Putting this in the appropriate sub would work:

Code:
If GetPlayerCurWeight(Index) > GetPlayerMaxWeight(Index) Then CanMove = False

Ty this is nice, works good...Smile my players can't walk if over encumbered... funny :twisted:

I now need another litle help. :roll:
I make a label(lblWeight) and a shape(shpWeight) to the inventory. I use this code:

Private Sub Label24_Click()
'Weight
lblWeight.Caption = GetPlayerMaxWeight & " / " & GetPlayerCurWeight
shpWeight.Width = (((GetPlayerMaxWeight / lblWeight.Width) / GetPlayerCurWeight / lblWeight.Width) * lblWeight.Width)
End Sub

But if i want to make my project, is says:

Argument not optional!

lblWeight.Caption = GetPlayerMaxWeight & " / " & GetPlayerCurWeight

Any tip :?: :roll:
Reply
#17
Dave Wrote:GetPlayerMaxWeight(MyIndex) & " / " & GetPlayerCurWeight(MyIndex)

the index arguement is, like it says, not optional.

lblWeight.Caption = GetPlayerMaxWeight(MyIndex) & " / " &

Variable not defined. :roll:
Reply
#18
laxika Wrote:
Dave Wrote:GetPlayerMaxWeight(MyIndex) & " / " & GetPlayerCurWeight(MyIndex)

the index arguement is, like it says, not optional.

lblWeight.Caption = GetPlayerMaxWeight(MyIndex) & " / " &

Variable not defined. :roll:

Define it, or use one that's defined. Noob. It's common sense... -.-
Reply
#19
Use Index instead of MyIndex.
Reply
#20
Vegeta Wrote:Use Index instead of MyIndex.

K this works thanks to you and Matt! Big Grin
Reply
#21
laxika Wrote:
Vegeta Wrote:Use Index instead of MyIndex.

K this works thanks to you and Matt! Big Grin

All he done was call you a noob..
Reply
#22
Someone has a fucked up source.
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply
#23
Lol.
Reply
#24
Now, I have a fucked up source! Wheeeee.... Sad
Reply
#25
Stop changing your font colour. Makes you look noobish.
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)