Link to home
Create AccountLog in
Avatar of Valleriani
VallerianiFlag for Sweden

asked on

VB.NET - Shoul I loop through a playerlist or have a arraylist per room to process packets (like chat)

How should I handle this scenario?

There are lets say thousands of rooms for a game (Mud based lets say). and let's say I need to have a local chat based in the current room someone says something. So simple way of putting is if anyone is someone chats and everyone in the room gets the message. This would be for more then just chatting, could be for attacking/etc. But you get the drift (hopefully.)

I think I have two easy ways of doing it. First is looping through the players connected and if currentroom of player connected = the player who has stated the message, send the message to them. This is probably a standard way of doing it... Aka

localroomtosend = players(senderID)
For i = 0 to playerscount
 if players(playercount).room = localroomtosend then
 ... do this
 end if
Next


----
Second way I was thinking is to have an array for each room that stores people who are currently there. So when a player moves to that room it would add them to that array, and when they leave, it will remove them. So you'd need to loop through this array, you wouldn't have to do a check because you know there already there. So EACH room has its own array so when they would walk in, it would add, or remove. I was thinking of using a 'arraylist' however in this case. (Dynamic Array).



Any other ideas are welcome. Pretty much I want the 'fastest' solution, regardless of how much coding is required.

Thanks!
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Code a Room class that defines a Chat event. Have each player instance hook to the Chat event as they enter a room and drop the hook on leaving the room. Add a SendChat() method to the room class that raises the Chat event.
A Player instance then only needs to call the SendChat() method.
When defining the Chat() event add a Sender parameter so that the Person class can check the originator of the Chat event and ignore the ones it that it triggered itself.
Avatar of Valleriani

ASKER

Hey Mike,

What do you exactly mean by a player instance hook that when the enter the room and drop it when they leave? Overall it seems like #2 but you are speaking about instance hook.
I'm off-line now until Tuesday, I'll post sample code then.
Mike
Ok Mike, thank you!
ASKER CERTIFIED SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
If you do need to store a collection of players for a room then a generic collection will be much easier than an array:
   Private players as new List(of Player)
   
Great! Thanks Mike!