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

[EO] Dynamic Friends List - UPDATED (uses server-side)


RavenStar
 Share

Recommended Posts

This is an update of the previous tutorial I wrote, which I left at the very bottom of this post for reference, I recommend you use this new version :)

This tutorial will show you how to implement a dynamic friends list into your game. The friends list is now stored on the server and each character has a different friends list. The player is able to bring up the friends list, click a name and it will start a PM to that person. You are also able to define the maximum friends a character is allowed.

**CLIENT SIDE**

Open up the IDE of frmMainGame.
Find your picInventory panel, copy and paste it. When it asks if you want to make a control array, click no.
Now name this new panel to picFriends
Add a label inside this new panel and call it lblFriendAdd, this label is used to add new friends so give it a caption aswell.

Your panel should look something like this;

>! ![](http://i46.photobucket.com/albums/f142/onfiya/newfl1.gif)

Now double click lblFriendAdd and add this code;
```
Dim n As Long
        strinput = InputBox("Friend's Name : ", "Add Friend")
        If StrPtr(strinput) = 0 Or strinput = vbNullString Then Exit Sub
            n = CLng(Trim$(strinput))

            Call SendAddFriend(n)
```

Now goto modClientTCP and add this to the very bottom;
```
'RavenStar
'Add new friend
Public Sub SendAddFriend(ByVal Name As String)
    Dim Buffer As clsBuffer
    Set Buffer = New clsBuffer
    Buffer.WriteLong CAddFriend
    Buffer.WriteLong Name
    SendData Buffer.ToArray()
    Set Buffer = Nothing
End Sub

```

Next goto modConstants and find this;
```
' Bank constants
```
Add this code **above** it;
' Friend List constants - RavenStar
Public Const FLTop As Byte = 32
Public Const FLLeft As Byte = 32
Public Const FLOffsetY As Byte = 24

Open up modGlobals, add this at the very bottom;
```
' Used for friend list - RavenStar
Public FRIENDS_INDEX(1 To 8) As String
Public FriendX As Long
Public FriendY As Long

```

Open modEnumerations, find this;
```
    ' Make sure SMSG_COUNT is below everything else
    SMSG_COUNT
End Enum
```
Add this **above** it;
```
SFriendList 'RavenStar
```

Then find this;
```
    ' Make sure CMSG_COUNT is below everything else
    CMSG_COUNT
End Enum
```
Add this **above** it;
```
CAddFriend 'RavenStar
```

Goto modHandleData, find this;
```
HandleDataSub(STradeStatus) = GetAddress(AddressOf HandleTradeStatus)
```
Add this below it;
```
HandleDataSub(SFriendList) = GetAddress(AddressOf HandleFriendsList) 'RavenStar
```

Then goto the very bottom of modHandleData and add this;
```
'Friends List - RavenStar
Private Sub HandleFriendsList(ByVal Index As Long, ByRef Data() As Byte, ByVal StartAddr As Long, ByVal ExtraVar As Long)
    Dim Buffer As clsBuffer
    Dim ALL_FRIENDS As Long, I As Long, Y As Long, X As Long
    Dim FriendName As String

    Set Buffer = New clsBuffer
    Buffer.WriteBytes Data()

  frmMainGame.picFriends.Cls

    For I = 1 To UBound(FRIENDS_INDEX)

        Y = FLTop + (FLOffsetY * (I - 1))
        X = FLLeft

        FriendName = Buffer.ReadString
        FRIENDS_INDEX(I) = FriendName
        DrawText frmMainGame.picFriends.hDC, X, Y, FriendName, QBColor(White)

    Next

    Set Buffer = Nothing
End Sub
```

Open the code for frmMainGame, add this to the very bottom;
**I originally forgot to add this to the tutorial re-write, so if you used this tutorial before I added this, make sure you add it now.**
```
Private Sub picFriends_DblClick()
    Dim friendnum As Long

    friendnum = IsFriendName(FriendX, FriendY)

    If friendnum <> 0 Then
        txtMyChat.Enabled = True
        txtMyChat.text = "!" & FRIENDS_INDEX(friendnum) & " "
        txtMyChat.SetFocus
        txtMyChat.SelStart = Len(txtMyChat.text)
        Exit Sub
    End If
End Sub

Private Function IsFriendName(ByVal X As Single, ByVal Y As Single) As Long
    Dim tempRec As RECT
    Dim I As Long

    IsFriendName = 0

    For I = 1 To UBound(FRIENDS_INDEX)

            With tempRec
                .Top = FLTop + (FLOffsetY * (I - 1))
                .Bottom = .Top + 16
                .Left = FLLeft
                .Right = .Left + (picFriends.width - .Left)
            End With

            If X >= tempRec.Left And X <= tempRec.Right Then
                If Y >= tempRec.Top And Y <= tempRec.Bottom Then
                    IsFriendName = I
                    Exit Function
                End If
            End If
    Next

End Function

```

That's it for the client side, next is the server side, you're almost done!

**SERVER SIDE**

Open modHandleData and find;
```
HandleDataSub(CUntradeItem) = GetAddress(AddressOf HandleUntradeItem)
```
Add this below it;
```
HandleDataSub(CAddFriend) = GetAddress(AddressOf HandleAddFriend)
```

Goto modServerTCP, add this to the very bottom;
```
' Lets send their friend list - RavenStar
Sub SendFriendsList(ByVal Index As Long)
    Dim Buffer As clsBuffer
    Dim I As Long

    Dim fName As String
    Dim ff As Integer
    Dim RAW As String
    Dim Txt() As String

    fName = App.Path & "\data\friends\" & GetPlayerName(Index) & ".txt"
    If FileExist(fName, True) Then
        ff = FreeFile
        Open fName For Binary As #ff
        RAW = String$(LOF(ff), 32)
        Get #ff, 1, RAW
        Close #ff
        Txt$ = Split(RAW, vbCrLf)

        Set Buffer = New clsBuffer
        Buffer.WriteLong SFriendList

        For I = LBound(Txt$) To UBound(Txt$)
            Buffer.WriteString Txt$(I)
        Next

        SendDataTo Index, Buffer.ToArray()

        Set Buffer = Nothing

    Else
        ' Create the friend list if it doesn't exist..
        Open App.Path & "\data\friends\" & GetPlayerName(Index) & ".txt" For Output As #1
        Close #1
    End If
End Sub
```

Open modPlayer and find this;
```
Call SendMapEquipment(Index)
```
Add this below it;
```
Call SendFriendsList(Index)
```

Open modEnumerations and find this;
```
    ' Make sure SMSG_COUNT is below everything else
    SMSG_COUNT
End Enum
```
add this **above** it;
```
    SFriendList 'RavenStar
```
Woohoo you're all done!

Here's a screenshot for all you kids!
![](http://i46.photobucket.com/albums/f142/onfiya/coding/newflfinish.gif)

**Old Friends List Tutorial (Not Recommended)**

>! Hi again, I just finished writing this little code and thought I'd share it with you all.
>! This tutorial will guide you through the process of adding a dynamic friends list to your game. The friends list is stored locally so there is no server-side edits required.
>! What do I mean by dynamic? Basically this code will use a control array to create the extra labels (that store the friends name) for you, so if you want to adjust how many friends a player can store, you simply change 1 variable and done!
>! Lets get started!
>! **CLIENT SIDE**
>! Goto frmMainGame and find your picCharacter, this is where your characters name & stats are displayed (or you can use any of the other panels if you'd like).
>! Now copy and paste this panel, it will ask you if you want to create a control array, click no.
Now delete everything inside that panel so it's blank (see picture for example) and change the name of the panel to picFriendsList.
>! >! ![](http://i46.photobucket.com/albums/f142/onfiya/coding/FL1.gif)
>! Next create a label inside the picFriendsList panel and call it lblFriendName.
Copy this label then paste it inside the picFriendsList panel. It will ask you if you want to create a control array, click yes!
Now delete the newly created label, so you're left with only one label inside the panel. (see picture for example)
>! >! ![](http://i46.photobucket.com/albums/f142/onfiya/coding/FL2.gif)
>! Next, change the visibility of the label to FALSE. (see picture for example)
>! >! ![](http://i46.photobucket.com/albums/f142/onfiya/coding/FL3.gif)
>! Goto frmMainGame code and find;
```
Private Sub Form_Load()
```
Add this within the Form_Load sub; (Note: In the middle of this code where it has "For I = 1 To 4" simply change the 4 to the maximum number of friends you want to appear on the Friends List)
```
Dim FileName As String
Dim FriendsFile As Integer
Dim TextOfLine As String
Dim I As Integer
>! FileName = App.Path & "\Data Files\friendslist.txt"
FriendsFile = FreeFile
Open FileName For Input As FriendsFile
    For I = 1 To 4 ' <----- Change the 4 to the MAX number of friends a player can have!
        Load lblFriendName(I)
        Line Input #FriendsFile, TextOfLine
        With lblFriendName(I)
            .Left = lblFriendName(0).Left
            .Top = lblFriendName(I - 1).Top + lblFriendName(0).height + 6
            .Caption = TextOfLine
            .AutoSize = True
            .Visible = True
        End With
    Next I
Close FriendsFile
```
>! Next, double click the label we made earlier and add this code;
```
    txtMyChat.text = "! " & lblFriendName(Index).Caption & " "
    txtMyChat.Enabled = True
    txtMyChat.SetFocus
>! ```
>! You're all done!
>! Now a few notes..
-You'll need to figure out where you want to put the Friends List panel and add any hide/show code for it if you desire.
-Make sure you create a text file called "friendslist.txt" inside your "data files" folder.
-You will have to manually edit the friendslist.txt file, one name per line.
-When you click a persons name on the Friends List it will populate your chat bar with
-The friend names will appear under where you created the label earlier (even though it's not visible)
>! **I may later on add a tutorial on how to allow the player to edit their friends list from within the game, so they don't have to manually do it… I'll reserve the next post for that tutorial.**
>! Here's a screenshot of how mine ended up :)
![](http://i46.photobucket.com/albums/f142/onfiya/coding/FL4.gif)
Link to comment
Share on other sites

  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

**Rewrote tutorial to use a new method for friends list, it's now stored server side.

Edit: Crap I forgot to add a little code for the client-side, used to detect mouse click. Make sure you re-read the tutorial and add the missing part (if you haven't already got it).**
Link to comment
Share on other sites

Having a variable store the max number of friends doesn't make it dynamic, it makes it static. If it were dynamic, it'd store the names in a dynamic array and would automatically changed the UBound when new strings were added.

Client-side friends lists are fairly unhelpful. You simply need to re-download the client and *POOF*, there goes your friends list. I suggest you look into creating a few packets and store the lists server-side. You should also look at how I've handled things like conversations. It'll show you how to handle dynamic arrays.

Also, using 1 label per friend in the list is a poor way of doing things. You should look at how I've handled things like the inventory. You can run through the array of friends and draw the text to the actual picturebox without having to use a label. This'll sort out the control mess you'll be left with from this tutorial. It'll also show you how to efficiently detect mouse events on a picturebox.
Link to comment
Share on other sites

@Robin:

> Having a variable store the max number of friends doesn't make it dynamic, it makes it static. If it were dynamic, it'd store the names in a dynamic array and would automatically changed the UBound when new strings were added.
>
> Client-side friends lists are fairly unhelpful. You simply need to re-download the client and *POOF*, there goes your friends list. I suggest you look into creating a few packets and store the lists server-side. You should also look at how I've handled things like conversations. It'll show you how to handle dynamic arrays.
>
> Also, using 1 label per friend in the list is a poor way of doing things. You should look at how I've handled things like the inventory. You can run through the array of friends and draw the text to the actual picturebox without having to use a label. This'll sort out the control mess you'll be left with from this tutorial. It'll also show you how to efficiently detect mouse events on a picturebox.

I agree it isn't truly dynamic, what I meant by this was it uses object controls to create labels to store the friend name.

As for storing them server-side, this is something I've already started to work on however I thought I'd leave this short tutorial here for those that don't care.

Thanks for the tip on drawing the text instead of using labels, this never crossed my mind. I'll check out the code.
Cheers.

Edit:
I was aware of the control mess, but as mentioned it never crossed my mind to directly draw the text inside of my used method. ^_^ ty
Link to comment
Share on other sites

Yeah, I've started on a completely different method where the friendslist is stored server-side.

It's all working, just need to add ability to add/edit friends then I'll write up a tutorial for it. I'll edit the first post with the new tutorial once I'm done.
Link to comment
Share on other sites

I'm having a bit of a problem with this script, the client side went great no problems. Then I got to the server side and followed the guide exactly how it said to. But im getting 2 errors.

I HIGHLIGHTED WHERE IM GETTING THE ERRORS IN YELLOW! ALSO HERE IS 2 SCREENSHOTS:

ModhandleData Error:
![](http://runeworld.site11.com/1.bmp)

ModserverTCP Error:
![](http://runeworld.site11.com/2.bmp)

HERE IS MY SOURCE CODE TO MY SERVER

DOWNLOAD MY SERVER FILE WITH THE 2 ERRORS:
[http://runeworld.site11.com/HELP.rar](http://runeworld.site11.com/HELP.rar)
Link to comment
Share on other sites

@tmoney,
There, I added an extra step to the tutorial in the first post, it's at the end highlighted in red.

I was crazy tired when I wrote this so I forgot to add that :P

Also, from what I gather in your first screenshot, you're using the wrong code.
You've put```
HandleDataSub(SFriendList) = GetAddress(AddressOf HandleFriendsList) 'RavenStar
```when that is only meant for the client-side.

Try re-reading the tutorial and figure out where you went wrong.
Link to comment
Share on other sites

I've repeated this tutorial to it's exact words at least 5 times + and every outcome seems to have an error. It needs to be a bit more user friendly because ive done everything right then some of the code snipplets dont belong in certain areas then you get it setup and it doesnt want to work or it gives you a Mismatch runtime error.

Very buggy IMO it could just be me, but im pretty sure ive ran through this tutorial enough to know im not the one messing up.

Re-Look it raven and maybe make the code a bit more user friendly that already has variables set, defined and code snipplets placed in the right place. Or maybe even add screenies, thanks for the hope though i thought i had myself a friends list :) but ill figure one out some day.
Link to comment
Share on other sites

There is an error ravenstar.
The fact is the enums on client and server must be identical.

you need to add this server side
in modEnumerations find
```
' Make sure CMSG_COUNT is below everything else
    CMSG_COUNT

```and add this before it
```
CAddFriend

```

You also are issing the HandleAddFriend sub server side.

Add this to the bottom of modHandleData (Server)
```
Sub HandleAddFriend(ByVal Index As Long, ByRef Data() As Byte, ByVal StartAddr As Long, ByVal ExtraVar As Long)
Dim name As String
Dim F As Long
Dim fname As String
Dim Buffer As clsBuffer
Set Buffer = New clsBuffer
    Buffer.WriteBytes Data()

    name = Buffer.ReadString

    Set Buffer = Nothing

fname = App.Path & "\data\friends\" & GetPlayerName(Index) & ".txt"
        F = FreeFile
        Open fname For Append As #F
        Print #F, name
        Close #F

        Call SendFriendsList(Index)
End Sub

```

You also need to create the friends directory if it doesn't exist
in modGeneral (server)
find
```
If LCase$(Dir(App.Path & "\data\accounts", vbDirectory)) <> "accounts" Then
        Call MkDir(App.Path & "\data\accounts")
    End If

```add after
```
If LCase$(Dir(App.Path & "\data\friends", vbDirectory)) <> "friends" Then
        Call MkDir(App.Path & "\data\friends")
    End If

```

also…  (client)

```
Dim n As Long
        strinput = InputBox("Friend's Name : ", "Add Friend")
        If StrPtr(strinput) = 0 Or strinput = vbNullString Then Exit Sub
            n = CLng(Trim$(strinput))

            Call SendAddFriend(n)

```should be
```
Dim n As Long
Dim strinput As String
        strinput = InputBox("Friend's Name : ", "Add Friend")
        If StrPtr(strinput) = 0 Or strinput = vbNullString Then Exit Sub

            Call SendAddFriend(Trim$(strinput))

```

and

this
```
'RavenStar
'Add new friend
Public Sub SendAddFriend(ByVal Name As String)
    Dim Buffer As clsBuffer
    Set Buffer = New clsBuffer
    Buffer.WriteLong CAddFriend
    Buffer.WriteLong Name
    SendData Buffer.ToArray()
    Set Buffer = Nothing
End Sub
```should be
```
Public Sub SendAddFriend(ByVal Name As String)
    Dim Buffer As clsBuffer
    Set Buffer = New clsBuffer
    Buffer.WriteLong CAddFriend
    Buffer.WriteString Name
    SendData Buffer.ToArray()
    Set Buffer = Nothing
End Sub

```  I think that solves most of the problems.

EDIT:
If you've already installed this make sure remove the Exit Sub from HandleAddFriend
Link to comment
Share on other sites

Actually there are a couple things more to do.

in ModServerTCP change the current sub SendFriendsList to this one:
```
' Lets send their friend list - RavenStar
Sub SendFriendsList(ByVal Index As Long)
    Dim Buffer As clsBuffer
    Dim I As Long

    Dim fname As String
    Dim ff As Integer
    Dim RAW As String
    Dim Txt() As String

    fname = App.Path & "\data\friends\" & GetPlayerName(Index) & ".txt"
    If Not (FileExist(fname, True)) Then
    ' Create the friend list if it doesn't exist..
        Open App.Path & "\data\friends\" & GetPlayerName(Index) & ".txt" For Output As #1
        Close #1
        End If
        ff = FreeFile
        Open fname For Binary As #ff
        RAW = String$(LOF(ff), 32)
        Get #ff, 1, RAW
        Close #ff
        Txt$ = Split(RAW, vbCrLf)

        Set Buffer = New clsBuffer
        Buffer.WriteLong SFriendList

        For I = LBound(Txt$) To UBound(Txt$)
            Buffer.WriteString Txt$(I)
        Next

        SendDataTo Index, Buffer.ToArray()

        Set Buffer = Nothing
End Sub
```

ALSO:  MADE AN EDIT TO MY ABOVE POST, YOU NEED TO CHANGE IT FOR IT TO WORK.
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...