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

[EVB, EO2.X] Hunger System


Justn
 Share

Recommended Posts

 
Hello this is a tutorial requested by [Strik3R](http://www.eclipseorigins.com/community/index.php?/user/42684-strik3r/) .  We will be adding a new player vital (hunger) that will decrease over time and a player can die if they don't eat. We are also adding to the consumable items so you can eat and get full. Basically this only for hunger. To add thirst just do the tutorial again changing Hunger to Thirst.

This tutorial was made with EVB. If you are using EO you just need to change all the TimeGetTime's to "gettickcount" (without the quotes). If you cant get this to work let me know.

**Client and Server**

**ModTypes**

In PlayerRec before End Type add this :

```
'Player Vitals New
    HungerTimer As Long
```In ItemRec before End Type add this:

```
addHunger As Long
    effectHunger As Long
```**ModEnumerations**

Add this to the bottom of the server packet list

```
SPlayerHunger
```Next find this:
```
' Vitals used by Players, Npcs and Classes
Public Enum Vitals
    HP = 1
    MP
```Under it add this:
```
Hunger
```The above steps must be done both in the client and the server!

**Client**

**ModGameEditors**

Find this :

```
If frmEditor_Item.cmbType.ListIndex = ITEM_TYPE_CONSUME Then
            frmEditor_Item.fraVitals.visible = True
            frmEditor_Item.scrlAddHp.value = .AddHP
            frmEditor_Item.scrlAddMP.value = .AddMP
            frmEditor_Item.scrlAddExp.value = .AddEXP
            frmEditor_Item.scrlCastSpell.value = .CastSpell
            frmEditor_Item.chkInstant.value = .instaCast
```Under it add this:
```
 frmEditor_Item.scrlAddHunger.value = .addHunger
 frmEditor_Item.scrleffecthunger.value = .effectHunger
```**ModHandleData**

In InitMessages add this to the bottom of the rest:

```
HandleDataSub(SPlayerHunger) = GetAddress(AddressOf HandlePlayerHunger)
```At the bottom of ModHandleData add this:

```
Private Sub HandlePlayerHunger(ByVal index As Long, ByRef data() As Byte, ByVal StartAddr As Long, ByVal ExtraVar As Long)
Dim buffer As clsBuffer  
   ' If debug mode, handle error then exit out
    If Options.Debug = 1 Then On Error GoTo errorhandler
         Set buffer = New clsBuffer
    buffer.WriteBytes data()
    Player(MyIndex).MaxVital(Vitals.Hunger) = buffer.ReadLong
    Call SetPlayerVital(MyIndex, Vitals.Hunger, buffer.ReadLong)

Select Case Player(MyIndex).Vital(Vitals.Hunger)
             Case 0
            frmMain.lblHunger.Caption = "Dead"
        Case 1 To 20
            frmMain.lblHunger.Caption = "Starving"
        Case 21 To 40
            frmMain.lblHunger.Caption = "Very Hungry"
        Case 41 To 60
            frmMain.lblHunger.Caption = "Hungry"
        Case 61 To 80
            frmMain.lblHunger.Caption = "Slightly Hungry"
        Case 81 To 100
            frmMain.lblHunger.Caption = "Full"
    End Select

    ' Error handler
    Exit Sub
errorhandler:
    HandleError "HandlePlayerHunger", "modHandleData", Err.Number, Err.Description, Err.Source, Err.HelpContext
    Err.Clear
    Exit Sub
End Sub
```**Server**

**modDatabase**

In Sub AddChar find these lines:

```
Player(index).Dir = DIR_DOWN
         Player(index).Map = Class(Player(index).Class).StartMap
        Player(index).x = Class(Player(index).Class).StartMapX
        Player(index).y = Class(Player(index).Class).StartMapY
        Player(index).Dir = DIR_DOWN
        Player(index).Vital(Vitals.HP) = GetPlayerMaxVital(index, Vitals.HP)
        Player(index).Vital(Vitals.MP) = GetPlayerMaxVital(index, Vitals.MP)
```Under it add this:
```
Player(index).Vital(Vitals.Hunger) = GetPlayerMaxVital(index, Vitals.Hunger)
```**modServerTCP**

find this:

```
Select Case Vital
        Case HP
            Buffer.WriteLong SPlayerHp
            Buffer.WriteLong GetPlayerMaxVital(index, Vitals.HP)
            Buffer.WriteLong GetPlayerVital(index, Vitals.HP)
        Case MP
            Buffer.WriteLong SPlayerMp
            Buffer.WriteLong GetPlayerMaxVital(index, Vitals.MP)
            Buffer.WriteLong GetPlayerVital(index, Vitals.MP)
```Under it add this:
```
Case Hunger
            Buffer.WriteLong SPlayerHunger
            Buffer.WriteLong GetPlayerMaxVital(index, Vitals.Hunger)
            Buffer.WriteLong GetPlayerVital(index, Vitals.Hunger)
```**modServerLoop**

Find this:

```
For i = 1 To Player_HighIndex
            If IsPlaying(i) Then
                If Tick > tmr25 Then
```Under it add this:
```
' hunger timer
                    If Tick > Player(i).HungerTimer Then
                    If Player(i).Vital(Vitals.Hunger) > 0 Then
                        ' remove 1 hunger point
                        Player(i).Vital(Vitals.Hunger) = Player(i).Vital(Vitals.Hunger) - 1
                            If Player(i).Vital(Vitals.Hunger) < 0 Then Player(i).Vital(Vitals.Hunger) = 0
                            If Player(i).Vital(Vitals.Hunger) = 0 Then
                            KillPlayer i
                            End If
                        Call SendVital(i, Vitals.Hunger)

' for every 30,000 in tick
                        Player(i).HungerTimer = timeGetTime + 30000
                    End If
                    End If
```**modPlayer**

Find this in sub OnDeath:

```
' Restore vitals
    Call SetPlayerVital(index, Vitals.HP, GetPlayerMaxVital(index, Vitals.HP))
    Call SetPlayerVital(index, Vitals.MP, GetPlayerMaxVital(index, Vitals.MP))
```Under it add this:
```
Call SetPlayerVital(index, Vitals.Hunger, GetPlayerMaxVital(index, Vitals.Hunger))
```Right under that you should see:
```
Call SendVital(index, Vitals.HP)
    Call SendVital(index, Vitals.MP)
```Under those two lines add this:
```
Call SendVital(index, Vitals.Hunger)
```In sub UseItem in the consumeable case find this:

```
' add exp
                If Item(ItemNum).AddEXP > 0 Then
                    SetPlayerExp index, GetPlayerExp(index) + Item(ItemNum).AddEXP
                    CheckPlayerLevelUp index
                    SendActionMsg GetPlayerMap(index), "+" & Item(ItemNum).AddEXP & " EXP", White, ACTIONMSG_SCROLL, GetPlayerX(index) * 32, GetPlayerY(index) * 32                     SendEXP index
                End If
```Under it add this:
```
' add hunger
            If Item(ItemNum).addHunger > 0 Then
             Player(index).Vital(Vitals.Hunger) = Player(index).Vital(Vitals.Hunger) + Item(ItemNum).addHunger
             If Player(index).Vital(Vitals.Hunger) > GetPlayerMaxVital(index, Vitals.Hunger) Then Player(index).Vital(Vitals.Hunger) = GetPlayerMaxVital(index,Vitals.Hunger)
                    SendActionMsg GetPlayerMap(index), "mmmm", BrightGreen, ACTIONMSG_SCROLL, GetPlayerX(index) * 32, GetPlayerY(index) * 32
                    SendVital index, Hunger
                End If

               ' effect hunger
                If Item(ItemNum).effectHunger > 0 Then
                    Player(index).Vital(Vitals.Hunger) = Player(index).Vital(Vitals.Hunger) - Item(ItemNum).effectHunger
                   If Player(index).Vital(Vitals.Hunger) < 0 Then Player(index).Vital(Vitals.Hunger) = 0
                   ' SendActionMsg GetPlayerMap(Index), "mmmm", BrightGreen, ACTIONMSG_SCROLL, GetPlayerX(Index) * 32, GetPlayerY(Index) * 32
                    SendVital index, Hunger
                End If
```**modCombat**

In GetPlayerMaxVital you will see this:

```
Case HP
            Select Case GetPlayerClass(index)
                Case 1 ' Warrior
                    GetPlayerMaxVital = ((GetPlayerLevel(index) / 2) + (GetPlayerStat(index, Endurance) / 2)) * 15 + 150
                Case 2 ' Magician
                    GetPlayerMaxVital = ((GetPlayerLevel(index) / 2) + (GetPlayerStat(index, Endurance) / 2)) * 5 + 65
                Case Else ' Anything else - Warrior by default
                    GetPlayerMaxVital = ((GetPlayerLevel(index) / 2) + (GetPlayerStat(index, Endurance) / 2)) * 15 + 150
            End Select
        Case MP
            Select Case GetPlayerClass(index)
                Case 1 ' Warrior
                    GetPlayerMaxVital = ((GetPlayerLevel(index) / 2) + (GetPlayerStat(index, Intelligence) / 2)) * 5 + 25
                Case 2 ' Magician
                    GetPlayerMaxVital = ((GetPlayerLevel(index) / 2) + (GetPlayerStat(index, Intelligence) / 2)) * 30 + 85
                Case Else ' Anything else - Warrior by default
                    GetPlayerMaxVital = ((GetPlayerLevel(index) / 2) + (GetPlayerStat(index, Intelligence) / 2)) * 5 + 25
            End Select
```Under it add this:(please note you can add whatever formula to get the max hunger like they do for mp and hp I just used a set amount at the current time)
```
Case Hunger
            Select Case GetPlayerClass(index)
            Case 1 ' Warrior
                    GetPlayerMaxVital = 100
                Case Else ' Anything else - Warrior by default
                    GetPlayerMaxVital = 100
            End Select
```**Client Form Work**

Now the way I currently have this base the player's hunger level is displayed as a text on frmMain. I did this for myself as I plan to add moodles for displaying hunger levels when i finish the graphics. But you can use bars such as the hp/mp/xp bars its very simple if you look at how they are handled if you need any help ask me and i will add it.

**frmMain**

Add a new Label anywhere on frmMain, name it: lblHunger

**frmEditor_Item**

Find the frame : **fraVitals**

Add two new scrollbars and two labels above them in the frame.
Name one scrollbar : scrlAddHunger

Double click this scrollbar and add this code:

```
' If debug mode, handle error then exit out
    If Options.Debug = 1 Then On Error GoTo errorhandler
         lblAddHunger.Caption = "+Hunger: " & scrlAddHunger.value
    Item(EditorIndex).addHunger = scrlAddHunger.value
         ' Error handler
    Exit Sub errorhandler:
    HandleError "scrlAddHunger_Change", "frmEditor_Item", Err.Number, Err.Description, Err.Source, Err.HelpContext
    Err.Clear
    Exit Sub
```Name the label above this scroll bar: lblAddHunger
Set the caption to this: +Hunger: 0

Name the next scroll bar this: scrleffecthunger

Double click the scrollbar and add the following code:
```
' If debug mode, handle error then exit out
    If Options.Debug = 1 Then On Error GoTo errorhandler
         lbleffecthunger.Caption = "-Hunger: " & scrleffecthunger.value
    Item(EditorIndex).effectHunger = scrleffecthunger.value
         ' Error handler     Exit Sub errorhandler:
    HandleError "scrleffecthunger_Change", "frmEditor_Item", Err.Number, Err.Description, Err.Source, Err.HelpContext
    Err.Clear
    Exit Sub
```Name the label above this one : lbleffecthunger
Set the caption to this: -Hunger: 0

And that should be it. If I missed anything please let me know and do know you can customize this anyway you want to fit your game =)
Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...
> And how I do that? I'm new to vb6\. Can you explain step by step?

You add a control to the form visually, it is a basic part of working with **VIsual Basic 6**. You have to rename the control to the right name, right click it and go to **Properties**.

![](http://i.imgur.com/H6EIlNz.gif)
Link to comment
Share on other sites

  • 2 months later...
I have a problem. Sometimes, Hunger will stop draining for people. I'm not sure when this happens, but if somebody could look at the code to see what's wrong, that would help a lot.

DEBUGGING

Edit1: Tried shutting of the server abruptly, this does not stop the timers from going down.
Link to comment
Share on other sites

  • 3 months later...
@'GalacticGlum':

> @'lurv':
>
> > My engine doesn't really save the items Addhunger or effecthunger data. Any idea what i've done wrong?
>
> Go over the code again. Maybe see if you did something wrong and accidentally forgot that part of the code.

I did before i posted and couldn't seem to figure out what i did wrong.
Now as a temp solution i create cases for consume items in Sub UseItem and manually add SetPlayerVital(index, hunger) + enter hunger here
Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...
Hey, I'm back and i've done this tutorial three times
Hunger randomly stops counting down for players(Haven't worked out what the problem is)
Usually takes from a couple of hours to two days ish to happen
Done this one two engines (Final frontier and Skywyre), same thing happens
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...