Author Topic: Loop Through Clients and Get BL_ID's  (Read 3464 times)

Title basically says it all. I'm new to this language and I'm trying to write some basic scripts.

I basically want to build a server command that when called loops through all the players on the server and gets their in game names and blockland id's and posts it neatly in the console. I plan on doing more with this later but I'd like to start small. Any pointers would be helpful.

Research done so far:
http://youtu.be/B7AZ2FC2lyw
http://youtu.be/-Ta7SmI5EjA
http://youtu.be/92IjMl2XFBI
http://youtu.be/zkoGCn-dWFc
http://forum.blockland.us/index.php?topic=238007.0

Code: [Select]
//Were opening a for loop and running through the client group, which holds all client connections
for(%i = 0; %i < clientGroup.getCount(); %i++)
{
//Were specifying the client here
    %c = clientGroup.getObject(%i);
//now we echo the clients name to the console
    echo(%c.name SPC %c.bl_id);
}

I'm supposing you understand the basics of a for loop, and object groups.
If not, those are fairly similar in all languages, so a broad search of the logic would be ideal.

Thanks for your quick reply!

I'm very familiar with basic programming concepts I've had experience with some C++, VB, AutoHotkey, and Lua.

I'll mess around with this and come back with what I've come up with.

Thanks!

Thanks for your quick reply!

I'm very familiar with basic programming concepts I've had experience with some C++, VB, AutoHotkey, and Lua.

I'll mess around with this and come back with what I've come up with.

Thanks!
No problem, in coding help you'll get as much help as you need, don't hesitate asking questions you cannot solve.

And a word of advice, don't lock this topic when you have found the complete answer, it's a rule of thumb here in this section

And a word of advice, don't lock this topic when you have found the complete answer, it's a rule of thumb here in this section
I'll keep that in mind.

Ok I'm not sure what I'm doing wrong here but I have my file located here: C:\Users\Mickers\Documents\Blockland\Add-Ons\Admin_Aboose\server.cs
With code:
Code: [Select]
function serverCmdPlayerList(%client)
{
    //Were opening a for loop and running through the client group, which holds all client connections
    for(%i = 0; %i < clientGroup.getCount(); %i++)
    {
        //Were specifying the client here
        %c = clientGroup.getObject(%i);
        //now we echo the clients name to the console
        echo(%c.name SPC %c.bl_id);
    }
}
Of course I have no idea what I'm doing but I'm getting the error in the console when I type:
Code: [Select]
exec("Add-Ons\Admin_Aboose\server.cs");
Missing file: Add-OnsAdmin_Abooseserver.cs!
What am I missing? Also are the additions to the script correct?

separate folders with / in torque

Ok it's executing properly now but it doesn't do anything. :(

I'm looking it over and clientGroup isn't highlighted. Do I need to send this to the function or link it somehow with %client?

Ok it's executing properly now but it doesn't do anything. :(

I'm looking it over and clientGroup isn't highlighted. Do I need to send this to the function or link it somehow with %client?
I see an issue with the function name,

try just function playercheck
the %client parameter is not needed.

I see an issue with the function name,

try just function playercheck
the %client parameter is not needed.

There is no issue with the function name - he is making a /command (server command). OP, if you wish the code to list the clients in chat rather than console, change echo to talk. Then you could just type /playerList in chat to run it.

I'm looking it over and clientGroup isn't highlighted. Do I need to send this to the function or link it somehow with %client?
What do you mean by highlighted? I assume that you're talking about whatever IDE you're using? That's fine, just ignore it.

There is no issue with the function name - he is making a /command (server command). OP, if you wish the code to list the clients in chat rather than console, change echo to talk. Then you could just type /playerList in chat to run it.
What do you mean by highlighted? I assume that you're talking about whatever IDE you're using? That's fine, just ignore it.
I wasn't sure what default function called the f2 menu, I thought it was playerlist, my apologies.


I wasn't sure what default function called the f2 menu, I thought it was playerlist, my apologies.
Oh, that could be. You didn't say anything about that so I didn't know what you were talking about.

Don't spoon feed a newbie without explaining how things work

Step by step how to make what you were asking for

for ( initialization ; condition ; update ) { body }

the initialization usually starts with %i = 0, for several reasons, in this case, because the first object in the ClientGroup can be referenced by ClientGroup.getObject(0);

the condition is a boolean statement, or a true/false statement, here we use %i < ClientGroup.getCount() so we can loop from 0 to the number of clients, minus one. We do the "minus one" because the first object is referenced by 0, so everything else is shifted down.

the update here is %i++, which just increments the %i value by 1
There are several ways of adding one (%i=%i+1 OR %i+=1 OR %i++) but we use %i++ because it's fast (I'm not so sure if it's any faster in TorqueScript)
The reason %i++ SHOULD be fast is because it's Just one single line of assembly code, so it's efficient. %i+=1 is actually about 7 lines of assembly code (unless the compiler fixes those things), which is 7 times slower than you actually need.

Right now, you have this code
for(%i=0;%i<ClientGroup.getCount();%i++){ body }

The ClientGroup contains a list of Objects that hold information about the clients connected to the server
To reference these objects, you would use the ClientGroup.getObject() method, and if we wanted to get the "%i"th object in the ClientGroup, we would use ClientGroup.getObject(%i)

Let's say we want to store this Object in a variable.
We would use %client = ClientGroup.getObject(%i); to set the variable %client to hold that certain object (an ID that points to the object)

Now, we want to get their BL_ID and print it out
the "print out" method is called echo();
and the BL_ID variable can be accessed through the new "%client" we have, so to print out the BL_ID of %client, we would use
echo(%client.BL_ID);

Now, our full code is:
for(%i=0;%i<ClientGroup.getCount();%i++)
{
    %client = ClientGroup.getObject(%i);
    echo(%client.BL_ID);
}


And thus, you can now read each client's bl_id

Creating a function:
function functionName(arguments) { body }

the function name is what you are calling, make sure that if you want a /command, you will start your function with:
function servercmdFunctionName ( for /FunctionName )

each servercmd function has 1 default optional argument, the client (Remember from above?)

function servercmdListPlayers(%client){body}

This is the header for a function to list the information, all we need is a body

The body can be the code from before:
function servercmdListPlayers(%client)
{
    for(%i=0;%i<ClientGroup.getCount();%i++)
    {
        %newClient= ClientGroup.getObject(%i);
        echo(%newClient.BL_ID);
    }
}

However, this is lacking protection, anyone on the server can call this /command, so let's make it Admin only

To see if a client is admin, you simply need to check their isAdmin variable, also see isSuperAdmin

We want to put this in an if statement

if ( condition ) { body }

the condition we will put here is %client.isAdmin

We also want the client's name, which happens to be %client.name

When joining parts of a "String" or line of characters, you can use these symbols:
SPC - Join and add a space
@ - Join without space

SPC acts the same as @" "@

function servercmdListPlayers(%client)
{
    if(%client.isAdmin == true)
    {
        for(%i=0;%i<ClientGroup.getCount();%i++)
        {
            %newClient= ClientGroup.getObject(%i);
            echo(%newClient.name SPC %newClient.BL_ID);
        }
    }
}


That there is what you were asking for

Try to use the %client.chatMessage( words ); function to print out into the client's chat
« Last Edit: September 05, 2013, 11:42:59 PM by MARBLE MAN »


listclients();

???
Lol'd.

Um, great little tutorial Marble, but he says he's had experience with programming and knows the basics. Sure, don't spoon-feed, but don't give him too much either. XD

@MARBLE MAN
Thanks for the going into so much detail I appreciate it. Although BluetoothBoy is correct I am familiar with programming basics however I was confused with how some of the code worked which you have helped cleared up for me.

I do however have a question:
Do I need to pass %client to the function at all to access ClientGroup? Or is it only needed so I can test if the client who called the function is an admin?

Thanks!

Edit:
Ok I've solved an issue but run into a problem. I'm attempting to host a server from my computer with a batch file which works fine. However although I can load my custom script  with exec("script path"); I can't access the function in game or from the console... However if I host a game from within Blockland, aka by hitting the "Start a Game" button from the title screen, the function runs just fine. What gives?

Thanks again.
« Last Edit: September 06, 2013, 11:06:29 AM by Mickers »