Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 09-05-2011, 07:12 PM
kingcj kingcj is offline
Registered User
kingcj's Avatar
Join Date: Apr 2006
Location: TN
Posts: 114
kingcj will become famous soon enough
Send a message via MSN to kingcj
Zie's Scripting Project

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.playerx 0.15
  
temp.playery 2.25;
}
if (
playerdir == 1){
  
temp.playerx 2.3;
  
temp.playery 1;
}
if (
playerdir == 2){
  
temp.playerx 0.15;
  
temp.playery 3;
}
if (
playerdir == 3){
  
temp.playerx 3.0;
  
temp.playery 1;

  
temp.npc putnpc2(temp.xtemp.ynull); //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.07,player.3);
  
changeimgpart(199,98,0,45,47);
  
showimg(201"hat" clientr.hat_array[this.current_hat 1] @ ".png"player.3,player.3);
  
changeimgpart(201,98,0,45,47);
  
showimg(202"hat" clientr.hat_array[this.current_hat 1] @ ".png"player.3,player.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.0temp.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() - 0:this.current_hat 1);
      
onHatShow();
  }
    else if (
keydown(1)) {   // cycling through hats the other way
      
this.current_hat = (this.current_hat == 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 && 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!
__________________
Zie

"It is not necessary to change. Survival is not mandatory." - W. Edwards Deming
Reply With Quote
  #2  
Old 09-05-2011, 07:28 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Work on styling and consistency. As a temporary fix you can use this to style your code. For consistency, things like onPlayertouchsme should be onPlayerTouchsMe if you're going to use onActionServerSide.

It also seems like you're using the temp. prefix part of the time and not others. Pick one way and stick with it throughout your script so it's less confusing. As someone who didn't use it except when necessary for years and now does, I'd strongly recommend you use it always.

In your long line of playerdir if-statements, you should be using player.dir, player.x, and player.y. Variables like playerdir and playerx are remnants from GS1 and shouldn't be used in new scripts. You can also probably simplify that entire bit with vecx/vecy.

PHP Code:
function onPlayertouchsme() {
  
addweapon("Personal/kingcj/haticon");
  
clientr.hat_array = {
    
012345
  
};
} else if (
clientr.hat_array = {012345}) { //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");
}

This doesn't really make sense, it looks like maybe you deleted an if-statement and forgot to delete the else part. Right now that's not even usable code since you've got an extra brace (note the importance of proper indentation in noticing that). Be sure to always use == instead of = for comparison (looks like you are throughout the rest of your script except in that snippet above).

It seems like your script is trying to be part weapon and part NPC. There's no reason to set its image in onCreated since that image is never used. In addition, the onPlayerTouchsMe will never be called in a weapon; that should be moved (along with onCreated) to a level NPC.

Also note that you can't change clientr variables on clientside, so you'll have to trigger serverside to change those.

This is a bit picky, but onCreated should always be at the top of your script (either the very top or right below //#CLIENTSIDE, depending on which side it's run on).
__________________
Reply With Quote
  #3  
Old 09-05-2011, 07:34 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Your code indenting/styling is very inconsistent, either use tabs only or spaces only.

A quick glance reveals this error:

PHP Code:
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"); 
  } 
The problem: You're doing else if on a function.

Edit: Looks like you already touched on that cbk.

You're drawing a hat on 199 but the others on 201, and 202. 199 is a global index so other player's would see that random hat but not the others.

You might benefit from learning how to use functions to your advantage so you don't have everything in onKeyPressed.
__________________
Quote:

Last edited by fowlplay4; 09-06-2011 at 03:34 AM..
Reply With Quote
  #4  
Old 09-05-2011, 08:38 PM
kingcj kingcj is offline
Registered User
kingcj's Avatar
Join Date: Apr 2006
Location: TN
Posts: 114
kingcj will become famous soon enough
Send a message via MSN to kingcj
Thanks guys!
The first couple of lines should be in a level npc. I just recently was able to get into testbed, and I save scripts that go together all in one text file so I posted without realizing I hadn't moved that piece of the script around. There was an if statement but it wasn't working and just reset my hats back to 0-5 (was wanting some advice on that line about how to check, but I think I can use hasweapon).
Next time I post something I'll be sure to make it look better and consistent. I'll play around with vecx and vecy to see if I can come up with some good coordinates that work.
Also the 199 global image is intentional so other players can see the hat so if they wanted to trade/buy they could see it and the other player won't have to change his headgear (or could show off his hats).
And what do you mean by using functions properly? Could you give me an example?
Thanks,
Zie
__________________
Zie

"It is not necessary to change. Survival is not mandatory." - W. Edwards Deming
Reply With Quote
  #5  
Old 09-06-2011, 03:31 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
I picked a bad choice of words before instead of 'use functions properly' I was trying to say 'use functions to your advantage':

PHP Code:
//#CLIENTSIDE
function onKeyPressed() {
  if (
keydown(0)) {
    
doKeyDownZeroStuff();
  }
  else if (
keydown(1)) {
    
doKeyDownOneStuff();
  } 
}

function 
doKeyDownZeroStuff() {
  
// code...
}

function 
doKeyDownOneStuff() {
  
// code...

Obviously give the functions meaningful names i.e:

PHP Code:
//#CLIENTSIDE
function onKeyPressed() {
  if (
keydown(5)) {
    
doAttack();
  }
}

function 
doAttack() {
  
// attack code here...

It increases the maintainability of your code and allows you to separate the different pieces of logic into much more manageable bites.

I.e: In my example above I have the keyboard controls logic in onKeyPressed and my call to doAttack allows me to store all my attacking logic in doAttack.
__________________
Quote:
Reply With Quote
  #6  
Old 09-06-2011, 03:37 AM
kingcj kingcj is offline
Registered User
kingcj's Avatar
Join Date: Apr 2006
Location: TN
Posts: 114
kingcj will become famous soon enough
Send a message via MSN to kingcj
Ah I see what you mean, but if you look closely I used two functions that way. The rest of the vars and such. are all different from each button push which is why I didn't find it appropriate to place 3 or 4 different functions. I just changed the vars individually. Also for my created functions do they need to be at the end of the script?
It just makes more sense to put them before they are used, but I'm unsure of the styling or what's appropriate.
__________________
Zie

"It is not necessary to change. Survival is not mandatory." - W. Edwards Deming

Last edited by kingcj; 09-06-2011 at 03:52 AM..
Reply With Quote
  #7  
Old 09-09-2011, 09:50 PM
kingcj kingcj is offline
Registered User
kingcj's Avatar
Join Date: Apr 2006
Location: TN
Posts: 114
kingcj will become famous soon enough
Send a message via MSN to kingcj
Sorry for the double post. Is there anyway to detect gralats that have been laid, without a custom system? I realize that the player touches the gralat and gets it, but how would another npc detect them?
__________________
Zie

"It is not necessary to change. Survival is not mandatory." - W. Edwards Deming
Reply With Quote
  #8  
Old 09-09-2011, 09:55 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by kingcj View Post
Sorry for the double post. Is there anyway to detect gralats that have been laid, without a custom system? I realize that the player touches the gralat and gets it, but how would another npc detect them?
Find the NPC using the npcs array or findAreaNPCs (or some other method of finding NPCs) and check

PHP Code:
npc.isInClass("gralats"
to see if the NPC is a dropped gralat.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 07:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.