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
10-21-2014, 11:22 AM #29
Taylor
Former Black Knight.
Originally posted by TheFallen View Post
It's not always about less code though, it's about efficiency and readability.


Yes. However, less code inevitably leads to better readability as well as efficiency. Regardless you end down the same path.
10-21-2014, 11:25 AM #30
Taylor
Former Black Knight.
Originally posted by dtx12 View Post
In any case GSC has support only for shitty coding style(procedural), it doesn't support OOP by design.
Also as for first post, sometimes I do things using another way:
    
godmode_toggle()
{
if(!self.isGod)
self EnableInvulnerability();
else
self DisableInvulnerability();
self.isGod = !self.isGod;
self toggle("Godmode", self.isGod);
}
toggle(text, isEnabled)
{
self iPrintLn(text + isEnabled ? " Enabled" : " Disabled");
}


Oh thank you, I couldn't figure out the ternary operator correctly Happy

Edit: Maybe it isn't supported in call of duty 4 (where I test my gsc script).
You must login or register to view this content.
Last edited by Taylor ; 10-21-2014 at 11:31 AM.
10-21-2014, 11:33 AM #31
Taylor
Former Black Knight.
Updated the clean toggled. You must login or register to view this content.
10-21-2014, 11:52 AM #32
dtx12
Do a barrel roll!
Originally posted by Taylor View Post
Updated the clean toggled. You must login or register to view this content.


You are using non-initialized variables(isEnabled and text), also check if variable is defined or not isn't necessary, I guess, since people initialize vars like this in onPlayerConnect function. Also as far as I know cod4 doesn't even have support for foreach loop(not only for ternary operator), this is a really old game.
Last edited by dtx12 ; 10-21-2014 at 11:54 AM.

The following user thanked dtx12 for this useful post:

TheFallen
10-21-2014, 12:24 PM #33
TheFallen
Former Dark Night
Originally posted by ScaRzModZ View Post
I would agree with you, except one thing. no one can really decide what pure clean code is.. people all have their own way of coding. I wouldn't recommend this, as I still prefer the normal coding way I use aka "Cluttered way", but that's my style of coding.


I didn't say anything about clean code :p but technically there still are standards one could follow to "clean" up their code. This however is just Taylor's style of coding.
10-21-2014, 12:31 PM #34
Taylor
Former Black Knight.
Originally posted by dtx12 View Post
You are using non-initialized variables(isEnabled and text), also check if variable is defined or not isn't necessary, I guess, since people initialize vars like this in onPlayerConnect function. Also as far as I know cod4 doesn't even have support for foreach loop(not only for ternary operator), this is a really old game.


Fixed, I didn't notice my error till now. :p I appreciate your input! Also, you don't have to define vars onPlayerConnect, onPlayerSpawn, etc. In fact you don't have to define them at all until you call the function itself.

    
if(!isDefined(self.menuOpen) || !self.menuOpen)
self.menuOpen = true;


Then change it with
    
self.menuOpen = !self.menuOpen;


Hence the two cases in the if statement, because the !isDefined will only catch once, then the bool just goes back and forth between true and false. Which explains why the if statement has the second case.
10-21-2014, 02:23 PM #35
dtx12
Do a barrel roll!
Originally posted by Taylor View Post
Fixed, I didn't notice my error till now. :p I appreciate your input! Also, you don't have to define vars onPlayerConnect, onPlayerSpawn, etc. In fact you don't have to define them at all until you call the function itself.

    
if(!isDefined(self.menuOpen) || !self.menuOpen)
self.menuOpen = true;


Then change it with
    
self.menuOpen = !self.menuOpen;


Hence the two cases in the if statement, because the !isDefined will only catch once, then the bool just goes back and forth between true and false. Which explains why the if statement has the second case.


I knew it, I just said, what people usually do.
Last edited by dtx12 ; 10-21-2014 at 02:26 PM.
10-21-2014, 02:32 PM #36
Taylor
Former Black Knight.
This is a good discussion guys. :p
10-21-2014, 08:02 PM #37
TheFallen
Former Dark Night
Originally posted by Taylor View Post
Yes. However, less code inevitably leads to better readability as well as efficiency. Regardless you end down the same path.


Not necessarily :p efficiency comes from using less operations and readability comes from organization and naming. There is a correlation between cleaner, more efficient code and less code but just because you have less code doesn't mean the former.

The following user thanked TheFallen for this useful post:

SyGnUs

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo