ID:173314
 
Well, I'm making a game, and I want the enemies to get harder with more people logged on (kindof like Diablo).

I just need to know a code to count the number of users logged onto the world at the moment.

thanks in advance.
Coolchris wrote:
Well, I'm making a game, and I want the enemies to get harder with more people logged on (kindof like Diablo).

I just need to know a code to count the number of users logged onto the world at the moment.

thanks in advance.

Well, we'll need to use a proc for this. A proc is a section of code that can be called by typing something like this:

player.IamAProc(some stuff)

In this example, player would be what is "calling" the proc. Calling is the term used for when the proc gets, well, called, executed, run, or what have you. IamAProc is the name of the proc, and the parenthesis, and the stuff inside is used to send some information to the proc. Like say...

player.Damage(50)

Then you could write the damage proc to take that 50, or whatever other number it might be, and do that much damage to the player.

Anyways, on with the question. The proc we would use in this instance is very simple.

proc
CountPlayers() // There is nothing between the parenthesis, which means that this proc won't accept any additionally info... it doesn't need to though, since it simply counts the players and returns the number
var/players_counted = 0 // This is what we will increase each time we find a player
var/mob/M // This is the type of atom we want to search for, a mob. It's saved as the variable "M"
for(M in world.contents) // This looks for all of M's type inside the world (the contents of the world is everything in it). M's type has already been set as a /mob
if(M.client) // Make sure that M has a "client," or a connected player on another computer. Otherwise you'll end up counting the monsters too
players_counted += 1 // Raise the amount of players by one
// The for() loop above will repeat for each mob in the world, and check if they are the player, then add to the players_counted variable. Then the loop will end and the code will continue
return players_counted // After all the players are counted, we need to "return" the value of players_counted back to whatever was looking for it (say your who's online verb). This just sends the players back, and you could store it in a new variable


Now that the proc is all done, copy and paste it into your code. Now we have to insert it someplace in your code for it to work. I'll display a simple verb that will show how many players are online, I will also show another variation of the verb to show you a different method of doing things.

mob
verb
who1()
set name = "How many players are online right now?"
var/players_online = CountPlayers() // We don't need a caller like player.CountPlayers(), since this is known as a global proc
src << "There are [players_online] players on right now."

who2()
set name = "See if there are more than 5 players online right now."
if(CountPlayers() > 5) // If the player count is greater than 5 players...
src << "There are more than 5 players on right now."
else
src << "There aren't more than 5 players on right now."


Hopefully you can see how to use this, and good luck with your game.


~Polatrite~
First, make a variable accessable from anywhere.
Eg var/usersloggedin
Whenever someone joins your game(ie in client/New()) increase that variable by one.
Whenever someone leaves your game(client/Del()) decrease the variable by one.