Author Topic: Applying getRandom to client group and code delay?  (Read 1911 times)

I want everyone in the server to have a random number applied to them.

Let's say I have set %cl as the client group, is there a way to do something like %cl.getRandom(#, #); ?



Next I want to delay a line of code without making a new function for it and scheduling that.  Thinking of something like adding delays to in-game brick events. Simple.


function getRandomClient()
{
    for(%i=0;%i<clientGroup.getCount();%i++)
        clientGroup.getObject(getRandom(0,clientGroup.getCount()-1).schedule(1000,hi);
}

function GameConnection::hi(%client)
{
    %client.chatMessage("hi");
}


eval(getRandomClient()); - Will say "hi" to a client.
?

Next I want to delay a line of code without making a new function for it and scheduling that.  
The only way to delay a line of code without making a function for it is to use eval:
schedule(1000,0,eval,"%client.exampleVar=\"kitty\";");
but you need to be careful of any user inputted data put into an eval'd statement, and filter out anything malicious


1. Define %count=ClientGroup.getCount(); prior to the for loop, and compare %i to that, otherwise you're calling the function twice every single iteration
2. Why are you evaling the function call?
« Last Edit: July 20, 2014, 02:32:04 AM by Headcrab Zombie »

2. Why are you evaling the function call?

Err, it's not just that he's evaling the function call, it's that he's evaling the returned value of the function call. TQS-defined functions auto return the last line which would be the schedule applied to the last client. So you'd be running eval( some number ); which would just syntax error. It makes no sense.

Speaking of making no sense, I have no idea what your getRandomClient() function is trying to do. Its name implies that it, well, returns a random client. Cool. return ClientGroup.getObject(getRandom(0, clientGroup.getCount()-1));. Your version seems to, for some reason, get as many random clients as there are on the server (note: it's totally possible that it'll get the same client every single time since it gets a new random one each iteration) and then schedules it to be sent 'hi' after a second.