Post: Quick and easy ways to clean up your script.
10-19-2014, 07:51 PM #1
Taylor
Former Black Knight.
(adsbygoogle = window.adsbygoogle || []).push({});
Hello Gsc Fanatics

I wrote this thread because I was sick of seeing poorly written functions being posted each day. They are sloppy and bad to look at.




  1. Toggles


    • Cluttered way.
          
      godmode_toggle()
      {
      if(self.godmode == false)
      {
      self EnableInvulnerability();
      self iPrintln("Godmode Enabled");
      self.godmode = true;
      }
      else
      {
      self DisableInvulnerability();
      self iPrintln("Godmode Disabled");
      self.godmode = false;
      }
      }



    • Clean way. You must login or register to view this content.
          
      godmode_toggle()
      {
      if(!isDefined(self.isGod) || !self.isGod)
      self EnableInvulnerability();
      else
      self DisableInvulnerability();

      self.isGod = !self.isGod;
      self iPrintLn("Godmode " + self.isGod ? "Enabled" : "Disabled");
      }

      Explanation: I use this method myself and it is overall better. It may look like more code however the toggle function can be re-used with multiple functions. So instead of writing the same two lines over and over again you have one function that you call that does it for you. As well as the toggle function itself being smaller.


  2. If statements and for loops.


    • Cluttered way.
          
      if(self.menuOpen == true)
      {
      for(i = 0; i < level.players.size; i++)
      {
      level.players[i] iPrintln("Menu is open");
      }
      }

    • Clean way. You must login or register to view this content.
          
      if(self.menuOpen)
      {
      for(i = 0; i < level.players.size; i++)
      level.players[i] iPrintln("Menu is open");
      }


      OR

          
      if(self.menuOpen)
      self iPrintln("Menu is open");

      Explanation: When using if statements and for loops, you do not need to add braces to your code IF AND ONLY IF the code that you would put between the braces is one line of code. Also you don't need self.menuOpen == true, self.menuOpen by itself works just as fine. If will read the variable as true because if(self.menuOpen) is like saying if(true). Likewise with variables that are false. You don't need self.menuOpen == false, if(!self.menuOpen) will read the variable as false.


  3. Function calling


    • Cluttered way.
          
      self thread function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

    • Clean way. You must login or register to view this content.
          
      self function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }


      OR

          
      function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

      Explanation: When a function does NOT contain a CONTINUOUS for or while loop, then you do NOT need to call it as a thread. You can just call the function normally on an entity you wish to call it on. However, in the last example that is clean I bet you noticed I did not define an entity. In this case, it is because if I call a function without an entity definition, it defaults to the entity that the scope is being called on.




If you would like for me to touch base on any other ways to optimize your code then please leave me a reply stating what you want an explanation for.

Thanks for reading!
- Taylor

[/font]
Last edited by Taylor ; 10-21-2014 at 12:12 PM.

The following 22 users say thank you to Taylor for this useful post:

-Numb, Chris, Cien, DJ_KILLA1337, DoraTheKiller97, Exelo, Father Luckeyy, Full-Evil, Geo, HiddenHour, ikilzu, iRnZ, KranK, Magnate13Snake, MCabCon, seb5594, ShutTheCrunchUp, SyGnUs, ViRuzModzHD, Fatality, xK ELITE GaminG, Zambie
08-06-2015, 08:13 PM #47
the codes work the same, remember that there is always more than one way to code something.
08-27-2015, 03:45 AM #48
itsSorrow
In my man cave
Originally posted by xMrMods View Post
the codes work the same, remember that there is always more than one way to code something.


Wait, really?!?!? Omg I never knew!
08-27-2015, 04:25 AM #49
Originally posted by GentleSlugger View Post
Wait, really?!?!? Omg I never knew!


Do you just post months later to something that does not even need a reply to because ur bored or what?

The following user thanked TheNiceUb3r for this useful post:

DF_AUS
08-27-2015, 04:30 AM #50
itsSorrow
In my man cave
Originally posted by TheNiceUb3r View Post
Do you just post months later to something that does not even need a reply to because ur bored or what?


both lol
10-28-2015, 09:19 PM #51
Tristen
Who’s Jim Erased?
nice tut thanks
01-01-2016, 08:40 PM #52
UkHazeOG
Bounty hunter
Helped Me This Thanks Smile
08-10-2016, 08:56 PM #53
xSynthmodz
Do a barrel roll!
noice
10-21-2017, 04:36 PM #54
Originally posted by Taylor View Post
Hello Gsc Fanatics

I wrote this thread because I was sick of seeing poorly written functions being posted each day. They are sloppy and bad to look at.




  1. Toggles


    • Cluttered way.
          
      godmode_toggle()
      {
      if(self.godmode == false)
      {
      self EnableInvulnerability();
      self iPrintln("Godmode Enabled");
      self.godmode = true;
      }
      else
      {
      self DisableInvulnerability();
      self iPrintln("Godmode Disabled");
      self.godmode = false;
      }
      }



    • Clean way. You must login or register to view this content.
          
      godmode_toggle()
      {
      if(!isDefined(self.isGod) || !self.isGod)
      self EnableInvulnerability();
      else
      self DisableInvulnerability();

      self.isGod = !self.isGod;
      self iPrintLn("Godmode " + self.isGod ? "Enabled" : "Disabled");
      }

      Explanation: I use this method myself and it is overall better. It may look like more code however the toggle function can be re-used with multiple functions. So instead of writing the same two lines over and over again you have one function that you call that does it for you. As well as the toggle function itself being smaller.


  2. If statements and for loops.


    • Cluttered way.
          
      if(self.menuOpen == true)
      {
      for(i = 0; i < level.players.size; i++)
      {
      level.players[i] iPrintln("Menu is open");
      }
      }

    • Clean way. You must login or register to view this content.
          
      if(self.menuOpen)
      {
      for(i = 0; i < level.players.size; i++)
      level.players[i] iPrintln("Menu is open");
      }


      OR

          
      if(self.menuOpen)
      self iPrintln("Menu is open");

      Explanation: When using if statements and for loops, you do not need to add braces to your code IF AND ONLY IF the code that you would put between the braces is one line of code. Also you don't need self.menuOpen == true, self.menuOpen by itself works just as fine. If will read the variable as true because if(self.menuOpen) is like saying if(true). Likewise with variables that are false. You don't need self.menuOpen == false, if(!self.menuOpen) will read the variable as false.


  3. Function calling


    • Cluttered way.
          
      self thread function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

    • Clean way. You must login or register to view this content.
          
      self function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }


      OR

          
      function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

      Explanation: When a function does NOT contain a CONTINUOUS for or while loop, then you do NOT need to call it as a thread. You can just call the function normally on an entity you wish to call it on. However, in the last example that is clean I bet you noticed I did not define an entity. In this case, it is because if I call a function without an entity definition, it defaults to the entity that the scope is being called on.




If you would like for me to touch base on any other ways to optimize your code then please leave me a reply stating what you want an explanation for.

Thanks for reading!
- Taylor

[/font]


thank you!

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo