PDA

View Full Version : Boot Help


Twilikari
12-31-2010, 07:17 AM
I was looking at some scripts on Testbed, because i'm attempting to learn, and the boot script(s) didn't make a bit of sense to me. If somebody could please just take it apart and explain it to me, I'd be really greatful :D

cbk1994
12-31-2010, 08:30 AM
The boot script on Testbed is atrocious, not sure why it's so awful as it was much better when I first added it.

A better boot script would look like this:


//#CLIENTSIDE
function onCreated() { // called when the client logs in or the script is updated
this.speed = 1; // set the default speed to 1
}

function onKeyPressed(code, key) {
if (key == "b") {
// toggle boots
this.on = ! this.on; // sets this.on to the opposite of the current value, i.e. toggles it
player.chat = "Boots: " @ (this.on ? "On" : "Off"); // ternary operator makes it easier than an if-statement, but either works

if (this.on) { // is it on now?
this.trigger("timeout", null); // start the timeout; second parameter not req'd on v6; can also use setTimer(0.05)
}
}
}

function onTimeOut() {
if (! this.on) { // boots were turned off
return; // this statement ends the function; code below is not executed
}

// there are four directions and keydown takes 0/1/2/3 for up/left/down/right
// so looping through these and checking if the key is down is the easiest
// way to move the player if the key is pressed
for (temp.key = 0; key < 4; key ++) {
if (keydown(key)) { // is the key down?
// vecx and vecy, in a simple explanation, give the amount that the player
// needs to move horizontally and vertically for the given direction;
// multiplying this by this.speed increases the speed at which the player
// moves in that direction
player.x += (vecx(key) * this.speed);
player.y += (vecy(key) * this.speed);
}
}

this.setTimer(0.05);
}

function onPlayerChats() {
if (player.chat.starts("/speed ")) {
// substring returns the part of the string after the x character, where x
// is the first parameter -- in this case, it returns everything after
// "/speed " in the player's chat
this.speed = player.chat.substring(7).trim(); // trim removes spaces at the beginning and end
player.chat = "Speed: " @ this.speed; // tell the player what the new speed is
}
}


These are pretty heavily commented but if you have any questions I can break it down line-by-line.

Twilikari
12-31-2010, 07:26 PM
Thank you so much!! :D :D :D

Those testbed boots were terrible to understand, but I actually fully understand yours. Thankssss!

PowerProNL
01-01-2011, 11:43 AM
Kentucky Friend ChrisVimes :)