Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Looking for opinons. (https://forums.graalonline.com/forums/showthread.php?t=134265266)

khortez 12-16-2011 02:36 AM

Looking for opinons.
 
As title says, I'm just looking for opinons on this code. If it's unreadable (mainly), takes too much memory whatever. (So long as it's helpful of course..)

Take note, i might ask questions.

PHP Code:

//#CLIENTSIDE
function onCreated(){
this.boots false;
this.speed 2;
}

function 
onKeyPressed(codekey){
if(
key == "b"){

if(
this.boots == false){

this.boots true;
}

else if(
this.boots == true){

this.boots false;
}

if(
this.boots == false){
player.chat "Boots off!";
}

else if(
this.boots == true){

player.chat "Boots on!";
setTimer(0.05);
}
 }
  }


  function 
onTimeout() {
if (
this.boots == true) { 
  for ( 
temp.key 0key 4key++) { 
                                   
   if(
keydown(key)){ 
    
player.+= vecx (key) * this.speed
    
player.+= vecy (key) * this.speed
                                
  } 
}  
  
setTimer(0.05); 
  }
 } 


iBeatz 12-16-2011 02:38 AM

Your styling is quite odd. No offense, but I've never seen horizontally inverted styling like that if you intended it to be that way.
(When I say this, I'm referring to the brackets at the end of the onKeyPressed event)

khortez 12-16-2011 03:09 AM

Quote:

Originally Posted by iBeatz (Post 1678039)
Your styling is quite odd. No offense, but I've never seen horizontally inverted styling like that if you intended it to be that way.
(When I say this, I'm referring to the brackets at the end of the onKeyPressed event)

Yeah i did intend it for it to be like that, I'm new to scripting so. I really haven't learn'd any other way. It helps me identify where stuff begins and how it all goes. If there is a better way, I don't know it

fowlplay4 12-16-2011 03:18 AM

If you don't know how to style your code and want to make it easier for the forum folk to read please use my GS2 Beautifier to clean it up before posting.

The two checks in your key pressed are redundant which also make it confusing in the process.

Not a fan of using temp once then not using it through out.

You don't really need to evaluate for true/false like that either you could just use but that's ultimately personal preference:

PHP Code:

if (this.boots == true) {  // if (this.boots) { // is also fine.
  // boots are on
} else {
  
// boots are off



khortez 12-16-2011 08:08 AM

I evaluated for true/false because i didn't think there was any other way really. i know another way of making boots but, i wanted to do it in a way i knew i understood completly. which is why its created the way it is.


Edit: thanks for the input though. appreciated. If anyone else got anything they'd like to add, feel free to. I'd appreciate it in advance.

Tricxta 12-16-2011 08:16 AM

Quote:

PHP Code:

 function onTimeout() { 
if (
this.boots == true) {  
  for ( 
temp.key 0key 4key++) {  
                                    
   if(
keydown(key)){  
    
player.+= vecx (key) * this.speed;  
    
player.+= vecy (key) * this.speed;  
                                 
  }  
}   
  
setTimer(0.05);  
  } 
 } 


You should fix this up... temp.key and key are 2 different variables.

Apart from your coding and the obvious errors your script is kinda alright...

You can get rid of both flag toggle lines and just have this.boots=!this.boots though. Just a little shortcut :)

khortez 12-16-2011 08:35 AM

yeah, that is a shortcut i have known for awhile. but i didn't feel like i understood it well enough, so i used a code i felt like i could call my own. I wanna understand something instead of just using it.

as for temp.key, i used it the way it is because i figured i defined it already. before it was temp.i but i wanted something that would be easier to recognize and key in my thoughts, fit pretty well.

lastly, what errors? or were they already pointed out?

Gunderak 12-16-2011 08:51 AM

It basically works like this.
By default this.whatever equals null or 0 because you haven't assigned anything to it, It also is a Boolean, which means it can be true or false, eg 0 or 1.
So when you put:
PHP Code:

this.whatever = !this.whatever

It basically just toggles it.

cbk1994 12-16-2011 11:43 AM

Quote:

Originally Posted by khortez (Post 1678108)
as for temp.key, i used it the way it is because i figured i defined it already. before it was temp.i but i wanted something that would be easier to recognize and key in my thoughts, fit pretty well.

It's only necessary to use temp the first time, but I'd highly recommend using it throughout.

callimuc 12-16-2011 04:22 PM

You could also short the script by doing something like this

PHP Code:

function onKeyPressed(codekey) {
  if (
key == "b") {
    if (
this.boots == false) {
      
this.boots true;
      
player.chat "Boots on!";
    }
    else if (
this.boots == true) {
      
this.boots false;
      
player.chat "Boots off!";
    }
  }


OR

PHP Code:

function onKeyPressed(codekey) {
  if (
key == "b") {
    
this.boots = !this.boots//change boolean
    
player.chat "Boots "@ (this.boots"on!" "off!"); //boots are on or off
  
}


Well with the
PHP Code:

this.boots = !this.boots

is something like gunderak said. It changes the boolean (true to false or false to true). Gunderaks post might also help.

And
PHP Code:

(this.boots "on!" "off!"); 

is also very nice (in my opinion). Itīs like also checking the boolean. This example might help
PHP Code:

player.chat this.booleancheck "The boolean is true!" "The boolean is false!";

//or

this.booleancheck BooleanTrue() : BooleanFalse();
  
//if the statement is true, start the function "BooleanTrue()" else if the
  //statement is false, start the function "BooleanFalse()"

function BooleanTrue() {
  
player.chat "The boolean is true!";
}

function 
BooleanFalse() {
  
player.chat "The boolean is false!";



If there are any questions or suggestions about this just ask ^^

Gunderak 12-16-2011 04:53 PM

That one post pretty much summed up everything nicely.
rep+
Edit: You must spread it around before giving it to callimuc x-x

Emera 12-16-2011 05:34 PM

It's a code that does what it was made to do, but it can be coded and styled in a much more readable and simple way that what you've got there. Well done on the code but snoop around the other posts on this thread and you could pick up on a lot of things.

Tolnaftate2004 12-16-2011 07:32 PM

Quote:

Originally Posted by callimuc (Post 1678137)
PHP Code:

function onKeyPressed(codekey) {
  if (
key == "b") {
    
this.boots = !this.boots//change boolean
    
player.chat "Boots "@ (this.boots"on!" "off!"); //boots are on or off
  
}



Not terse enough!

PHP Code:

function onKeyPressed(codekey) {
  if (
key == "b") {
    
player.chat "Boots "@ ((this.boots = !this.boots)? "on!" "off!");
  }


:pluffy:

Gunderak 12-16-2011 08:01 PM

Lmao compacting it up as much as we can I see....

khortez 12-16-2011 09:57 PM

Quote:

Originally Posted by callimuc (Post 1678137)
You could also short the script by doing something like this

PHP Code:

function onKeyPressed(codekey) {
  if (
key == "b") {
    if (
this.boots == false) {
      
this.boots true;
      
player.chat "Boots on!";
    }
    else if (
this.boots == true) {
      
this.boots false;
      
player.chat "Boots off!";
    }
  }


OR

PHP Code:

function onKeyPressed(codekey) {
  if (
key == "b") {
    
this.boots = !this.boots//change boolean
    
player.chat "Boots "@ (this.boots"on!" "off!"); //boots are on or off
  
}


Well with the
PHP Code:

this.boots = !this.boots

is something like gunderak said. It changes the boolean (true to false or false to true). Gunderaks post might also help.

And
PHP Code:

(this.boots "on!" "off!"); 

is also very nice (in my opinion). Itīs like also checking the boolean. This example might help
PHP Code:

player.chat this.booleancheck "The boolean is true!" "The boolean is false!";

//or

this.booleancheck BooleanTrue() : BooleanFalse();
  
//if the statement is true, start the function "BooleanTrue()" else if the
  //statement is false, start the function "BooleanFalse()"

function BooleanTrue() {
  
player.chat "The boolean is true!";
}

function 
BooleanFalse() {
  
player.chat "The boolean is false!";



If there are any questions or suggestions about this just ask ^^

Thanks. that helps a lot :D. I may have over thought the ! operator. But i think i understand it now.


and wow, i never knew what the ?, : meant. but now i believe i do. so to be sure i do. the ? is like saying if(whatever) and the : is like saying 'else'?



thanks everyone for the help. greatly appreciated.


All times are GMT +2. The time now is 05:34 AM.

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