We really appreciate that you're posting in the code gallery and that you're intersted in helping other scripters. At this point, though, the script has some errors in it that make it unsuitable to be posted here. There's lots of scripters here who are happy to give you feedback on scripts; a good idea would be to post your threads in one of the other subforums (e.g. the GS2 subforum) before posting it here to get feedback. Generally we try to keep the Code Gallery free of erroneous scripts so that it can be used as a learning resource.
The most important thing to do when posting your scripts is to make sure they're properly styled/indented. There's
a tool that will do it for you, but eventually you'll want to get good at indenting your code as you write it. This makes it a lot easier to read and to spot problems in the code.
PHP Code:
//Scripted by Fatel
//#CLIENTSIDE
function getCoordinates(xOffset, yOffset) {
if (player.dir == 0) {
this.x = player.x + 0.5;
this.y = player.y - yOffset;
}
elseif(player.dir == 1) {
this.x = playerx - xOffset;
this.y = player.y + 0.5;
}
elseif(player.dir == 2) {
this.x = player.x + 0.5;
this.y = player.y + yOffset;
}
elseif(player.dir == 3) {
this.x = player.x + xOffset;
this.y = player.y + 0.5;
}
}
function onWeaponFired() {
setani("shoot", NULL);
getCoordinates(7, 7)
if (player.dir == 3) {
putexplosion 0.8, player.x + 3, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 4, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 5, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 6, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 7, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 8, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 9, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 10, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 11, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 12, player.y + 1;
}
if (player.dir == 1) {
putexplosion 0.5, player.x - 2, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 4, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 5, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 6, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 7, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 8, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 9, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 10, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 11, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x - 12, player.y + 1;
}
if (player.dir == 2) {
putexplosion 0.5, player.x + 0.8, player.y + 3;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 4;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 5;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 6;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 7;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 8;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 9;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 10;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 11;
sleep 0.01;
putexplosion 0.5, player.x + 0.8, player.y + 12;
}
if (player.dir == 0) {
putexplosion 0.5, player.x + 0.6, player.y - 2;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 4;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 5;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 6;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 7;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 8;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 9;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 10;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 11;
sleep 0.01;
putexplosion 0.5, player.x + 0.6, player.y - 12;
}
}
I originally rewrote the getCoordinates function, but after glancing at the script, it turns out it's not actually being used for anything; the positions it calculates are never used in the script, so we can just cut it out.
The first main problem that stands out is that you're using GS1 syntax for the
putexplosion and
sleep commands. The proper way to write these commands is to surround the parameters with parentheses, like this:
PHP Code:
putexplosion(0.8, player.x + 3, player.y + 1);
sleep(0.01);
putexplosion(0.5, player.x + 4, player.y + 1);
sleep(0.01);
The next problem is that, as Tricxta pointed out, the Graal client runs at 20 FPS; as such, the most you can sleep for is one frame (1/20th of a second = 0.05 seconds).
PHP Code:
putexplosion(0.8, player.x + 3, player.y + 1);
sleep(0.05);
putexplosion(0.5, player.x + 4, player.y + 1);
sleep(0.05);
The script right now calls the same commands over and over in a predictable way; we can use loops to avoid having to write every single explosion out. The pattern is that the explosions move 1 tile farther in the direction of the player with every iteration (there's 10 of them). There are a few inconsistencies (likely mistakes), but we can ignore them and do something like this:
PHP Code:
if (player.dir == 3) {
for (temp.i = 0; temp.i < 10; temp.i ++) {
putexplosion(0.5, player.x + 3 + temp.i, player.y + 1);
sleep(0.05);
}
}
in place of:
PHP Code:
if (player.dir == 3) {
putexplosion 0.8, player.x + 3, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 4, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 5, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 6, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 7, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 8, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 9, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 10, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 11, player.y + 1;
sleep 0.01;
putexplosion 0.5, player.x + 12, player.y + 1;
}
If we do this for every direction, the script gets a lot cleaner:
PHP Code:
//Scripted by Fatel
//#CLIENTSIDE
function onWeaponFired() {
setani("shoot", NULL);
if (player.dir == 3) {
for (temp.i = 0; temp.i < 10; temp.i ++) {
putexplosion(0.5, player.x + 3 + temp.i, player.y + 1);
sleep(0.05);
}
} else if (player.dir == 1) {
for (temp.i = 0; temp.i < 10; temp.i ++) {
putexplosion(0.5, player.x - 2 - temp.i, player.y + 1);
sleep(0.05);
}
} else if (player.dir == 2) {
for (temp.i = 0; temp.i < 10; temp.i ++) {
putexplosion(0.5, player.x + 0.8, player.y + 3 + temp.i);
sleep(0.05);
}
} else if (player.dir == 0) {
for (temp.i = 0; temp.i < 10; temp.i ++) {
putexplosion(0.5, player.x + 0.6, player.y - 2 - temp.i);
sleep(0.05);
}
}
}
Note that I'm also using else-if, as it makes it easier for the scripter (and the interpreter) to follow the execution of the code. There are some other ways to make this script cleaner, such as using
the vector position functions, that you may want to look at if you're interested in learning.
Quote:
Originally Posted by Fatel
by "gs2" i mean its usable online, there IS gs1 commands, however the entire point of me posting this is to give players an online flamethrower, whether its 100% perfect or not. i understand theres some
non used commands, but if it works, no matter right?
|
The main purpose of the scripting forums, including the Code Gallery, is to help others learn, not to provide ready-made scripts to be used on servers (this is the secondary purpose). When you post incomplete or erroneous code, it introduces the possibility that a new scripter will look at it and learn the wrong way to do things.
Generally we'd prefer it if you would avoid posting code like this in the Code Gallery, and instead post it in the GS2 subforum first for feedback.