[Tutorial] How to PROPERLY make a self toggle command
So I see that alot of people like to uh.. make a command with a 0|1 option to toggle it on/off, and personally that's just inefficient for the user/admin.
Before I start: I want to say that this is generally what I like to do, as it might be a little extra work on my end, but it defiantly looks much nicer and refined. Instead of using an argument to declare if we want to have, for instance, God Mode enabled, we can use a Global Variable. This will be our FINISHED PRODUCT:
Spoiler
Here is an example of the OLD METHOD.
Spoiler
I'm no master of sourcepawn, but I can tell you that there's really a ton wrong with this. For one, perhaps someone puts an argument value of 2? To fix this problem, we'll add a global client boolean, Implement our OnClientConnect()/OnClientPostAdminCheck() and OnClientDisconnect() and modify our godmode command slightly:
Spoiler
As you can see, we've used our boolean to check and determine if the player has godmode. We also set it to 0 when they connect and disconnect because if we didn't, a player could leave and another would take their place, and this would assign the old client's value to the new client. (Pretty much, g_bIsEnabled[22].) EXTRA NOTES: Quote:
|
Re: [Tutorial] How to PROPERLY make a toggle command
enabled = !enabled
if(enabled) .... Or invert it in the if? |
Re: [Tutorial] How to PROPERLY make a toggle command
Quote:
|
Re: [Tutorial] How to PROPERLY make a toggle command
There are a few things I notice people doing:
Over/under validating clients. Storing/passing entities or clients rather than using entrefs/serials/userids in delayed callbacks. Using new instead of decl when the variable is going to be overwritten. Disregarding return values of natives that have significant return values (usually bools, but also things like using isvalidentity instead of just checking for -1/INVALID_ENT_REFERENCE). Making new variables before passing them to other functions, or using them in various statements (if, switch, etc), rather than just passing the return value. Using servercommand, to change cvars or execute commands, rather than implementing the natives behind what needs to be done, or creating new natives to interface with related plugins. Other stuff: Using includes/stocks that don't really do anything other than add extra overhead for the mod they are working with. Adding translation and config files to simple things that do not need them. Annoying stuff: Making code unreadable by using bad indentation, multiple file includes that don't make sense or are not necessary, or using inconsistent nesting (if / break / continue / return). Reading in and constantly traversing a large file of keyvakues, rather than storing parts of them in a more optimized format for what you need, be it static vars/arrays, or tries/adt arrays. Not using pragma semicolon. |
Re: [Tutorial] How to PROPERLY make a toggle command
Spoiler
Half of that probably consists of my coding lmao xD What i'm seeing more now days is people storing player data in enums. i would like to know the down fall of that, does that use up more memory per say? _________________ Re-wrote the short plugin, i replaced alot. you dont really need #defines if you are only replacing one thing with them. Version should be the only #define, especially if you plan on releasing something like this it makes it easier to update the cvar and the plugin version at the same time. Also you were doing a RegConsoleCmd on MapStart, im not entirely sure about this one, but im sure it's wrong, since you only need to do it on plugin start and unforseen things might happen with the code (like executing twice after a few map changes.)
Spoiler
|
Re: [Tutorial] How to PROPERLY make a toggle command
Quote:
Quote:
|
Re: [Tutorial] How to PROPERLY make a toggle command
Quote:
These do the same thing. I like to use them in the cases of which i have to show a ON/OFF text in a print. I like having shorter code cause its easier to spot the bugs :3 either which way is right. it just comes down to personal preferences. although it would be better if you have a lot of functions you needed to check against a bool you would use the bracket statements. |
Re: [Tutorial] How to PROPERLY make a toggle command
This thread should be titled "How to PROPERLY make a self toggle command," as most commands would probably have some sort of admin targeting capability. Especially one that toggles godmode. And why return an error if arguments are provided? Why not just ignore the arguments?
|
Re: [Tutorial] How to PROPERLY make a toggle command
Why are you using "Action:3" instead of "Plugin_Handled" ?
|
Re: [Tutorial] How to PROPERLY make a toggle command
Quote:
Quote:
|
| All times are GMT -4. The time now is 18:38. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.