Hello all, I would like to begin by saying I'm learning to script very slowly. I'm currently going through PA school and scripting gives me a break from my current studies. (sounds odd but learning something different lets my mind rest a little) I have a couple of projects that I like to work on whenever I can. I would like input on my scripts so that they are made better and secure, so one day I can put these to use or someone else could learn from or use them.
Thanks,
Zie
With that said this first script is fairly simple and straight foward, but I think there might be other ways to attempt this script. It is a hat viewer/collector. I made this awhile ago, but now I would like some feedback.
PHP Code:
function onCreated() {
setimg("haticon.png");
}
function onPlayertouchsme() {
addweapon("Personal/kingcj/haticon");
clientr.hat_array = {0,1,2,3,4,5};
}
else if (clientr.hat_array = {0,1,2,3,4,5}) { //My goal here was if the player had the weapon it wouldn't reset the hat array... Doesn't work very well.
addweapon("Personal/kingcj/haticon");
}
}
function onActionServerSide(tokens) { //Lay the hat down portion, I tried vecx/vecy but it would place it somewhere awkward or too close and I would pick it back up
if (playerdir == 0){
temp.x = playerx + 0.15;
temp.y = playery - 2.25;
}
if (playerdir == 1){
temp.x = playerx - 2.3;
temp.y = playery + 1;
}
if (playerdir == 2){
temp.x = playerx + 0.15;
temp.y = playery + 3;
}
if (playerdir == 3){
temp.x = playerx + 3.0;
temp.y = playery + 1;
}
temp.npc = putnpc2(temp.x, temp.y, null); //laid hat info
npc.hatimage = tokens;
npc.join("personal_kingcj_hatlaid");
clientr.hat_array.remove(tokens);
}
//#CLIENTSIDE
function onHatShow() { //Viewing current hats portion
showimg(199, "hat" @ clientr.hat_array[this.current_hat] @ ".png", player.x + 07,player.y - 3);
changeimgpart(199,98,0,45,47);
showimg(201, "hat" @ clientr.hat_array[this.current_hat + 1] @ ".png", player.x + 3,player.y - 3);
changeimgpart(201,98,0,45,47);
showimg(202, "hat" @ clientr.hat_array[this.current_hat - 1] @ ".png", player.x - 3,player.y - 3);
changeimgpart(202,98,0,45,47);
}
function onHidenGo() { //call this when a hat is choosen or ended viewing
hideimg(199);
hideimg(201);
hideimg(202);
enabledefmovement();
this.hatsel = false;
}
function onCreated(){ //Show basic set variables
this.hatsel = false;
this.current_hat = 0;
this.change = false;
}
function onWeaponFired() { //Fired weapon and viewing
this.hatsel = true;
disabledefmovement();
onHatShow();
for (temp.i = 0; temp.i < clientr.hat_array.size(); temp.i++) {
clientr.hat_array[temp.i].sorting = clientr.hat_array[temp.i];
}
clientr.hat_array.sortbyvalue("sorting", "float", true);
setani("idle",null);
}
function onKeypressed(code) { // Cycling through hats
if (this.hatsel) {
if (keydown(3)) {
this.current_hat = (this.current_hat == clientr.hat_array.size() - 1 ? 0:this.current_hat + 1);
onHatShow();
}
else if (keydown(1)) { // cycling through hats the other way
this.current_hat = (this.current_hat == 0 ? clientr.hat_array.size() - 1:this.current_hat - 1);
onHatShow();
}
else if (keydown(4) && this.haton != this.current_hat && this.change == false) { //putting the hat on
player.attr[1] = "hat"@ clientr.hat_array[this.current_hat] @".png";
freezeplayer(.7);
setani("haton",null);
sleep(.7);
setani("idle",null);
this.haton = this.current_hat;
this.change = true;
onHidenGo();
}
else if (keydown(4) && this.current_hat == this.haton) { //check to see if hat choosen is already on or not
player.chat = "Hat Already On";
}
else if (keydown(4) && this.change == true) { //switch hats
freezeplayer(1.35);
setani("hatoff",null);
sleep(.55);
setani("haton",null);
player.attr[1] = "hat" @ clientr.hat_array[this.current_hat] @".png";
sleep(.7);
setani("idle",null);
this.haton = this.current_hat;
onHidenGo();
}
else if (keydown(5) && this.change == true) { //Take off hats
freezeplayer(.55);
setani("hatoff",null);
sleep(.55);
setani("idle",null);
player.attr[1] = "";
this.change = false;
this.haton = -1;
onHidenGo();
}
else if (keydown(5) && this.change == false) { //Get out of hat viewing
player.attr[1] = "";
this.change = false;
this.haton = -1;
onHidenGo();
}
else if (keydown(6) && this.current_hat > 5 && this.current_hat == this.haton) { //Lay a hat down that player is wearing
freezeplayer(0.3);
setani("lay",null);
player.attr[1] = "";
temp.tokens = clientr.hat_array[this.current_hat];
triggerServer("weapon",this.name,tokens);
this.current_hat = 0;
this.change = false;
this.haton = -1;
onHidenGo();
}
else if (keydown(6) && this.current_hat > 5) { //Lay down hat in inventory
freezeplayer(0.3);
setani("lay",null);
temp.tokens = clientr.hat_array[this.current_hat];
triggerServer("weapon",this.name,tokens);
this.current_hat = 0;
this.change = false;
onHidenGo();
}
else if (keydown(6) && this.current_hat <= 5) { //Can't get rid of base set of hats
player.chat = "You can't get rid of free hats!";}
}
}
And the hat laid class is
PHP Code:
function onCreated() {
setimgpart("hat" @ this.hatimage @ ".png",98,0,45,47);
}
function onPlayerTouchsme() {
clientr.hat_array.add(this.hatimage);
this.destroy();
}
Thanks for any feedback!