Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-16-2011, 02:36 AM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
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); 
  }
 } 
Reply With Quote
  #2  
Old 12-16-2011, 02:38 AM
iBeatz iBeatz is offline
Kavan
iBeatz's Avatar
Join Date: Dec 2010
Location: Northern Ireland, UK
Posts: 154
iBeatz will become famous soon enough
Send a message via Yahoo to iBeatz
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)
__________________

Intelligence without ambition is like a bird without wings.

Reply With Quote
  #3  
Old 12-16-2011, 03:09 AM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
Quote:
Originally Posted by iBeatz View Post
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
Reply With Quote
  #4  
Old 12-16-2011, 03:18 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
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

__________________
Quote:
Reply With Quote
  #5  
Old 12-16-2011, 08:08 AM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
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.
Reply With Quote
  #6  
Old 12-16-2011, 08:16 AM
Tricxta Tricxta is offline
The Muffin Man
Tricxta's Avatar
Join Date: Oct 2010
Location: Australia
Posts: 563
Tricxta is a jewel in the roughTricxta is a jewel in the rough
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
Reply With Quote
  #7  
Old 12-16-2011, 08:35 AM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
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?
Reply With Quote
  #8  
Old 12-16-2011, 08:51 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #9  
Old 12-16-2011, 11:43 AM
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 khortez View Post
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.
__________________
Reply With Quote
  #10  
Old 12-16-2011, 04:22 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
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
__________________
MEEP!
Reply With Quote
  #11  
Old 12-16-2011, 04:53 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
That one post pretty much summed up everything nicely.
rep+
Edit: You must spread it around before giving it to callimuc x-x
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #12  
Old 12-16-2011, 05:34 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
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.
__________________
Reply With Quote
  #13  
Old 12-16-2011, 07:32 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by callimuc View Post
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!");
  }

__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #14  
Old 12-16-2011, 08:01 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Lmao compacting it up as much as we can I see....
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #15  
Old 12-16-2011, 09:57 PM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
Quote:
Originally Posted by callimuc View Post
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 . 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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 09:51 AM.


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