PDA

View Full Version : Reducing amount of checks per command


CXdur
14/10/2016, 10:58 PM
Hey,

I recently thought about reducing the amount of if statement checks per command. I am working on a gamemode with a lot of commands, some of these for different teams and such. As you can imagine there is a lot of different if statements in the different commands, a lot of these are repeated per command. Checking if the player is on foot, in a vehicle, has different pvars etc.

I was thinking of different ways to do it but I don't feel like I've come up with any good ideas yet. I was thinking of making functions to shorten the code, for example creating a function named IsPlayerOnFootAndCanInteract(playerid) which would check if on foot, different pvars for frozen, cuffed, stunned etc. These are just examples.

This doesn't seem like a good approach to me. I also thought about creating a function where I could input various checks in a string and it would run if checks for specified ones, but this would also seem kind of messy to me and probably inefficient. For instance: SetCommandRule(playerid, "allowvehicle=yes&requirespawn=no");. I didn't see this as a good approach to it, just thought I'd put it here so you guys can see my current ideas.

Thanks!

EDIT:

Wanted to add that I am using ZCMD, I have considered switching to YCMD, but would this help for this in any way and how? If so, do you mind providing any sample code as I have just recently looked into it and the functions seem pretty useful. I see that I can enable / disable commands for specific players, but I'm not sure whether or not there's anything in y_commands that will help me reduce the amount of if checks. There's about 500 commands so I think it would take a bit of effort as well, would like to know whether it's worth it or not.

Logic_
15/10/2016, 06:02 AM
Changing to YSI library will increase the size of your executable gamemode (.amx), make a function which has some IF checks and use that function in the CMD.

CMD:cmd(playerid, Paramus[])
{
Something();
return 1;
}
Something()
{
// IF checks
if(GetPlayerScore(playerid) > 100) return .....
}

DwayneMichael
15/10/2016, 09:01 AM
I am also struggling in this part, got too many checks and I can't just scramble them at the beginning of every command/function..

Spmn
15/10/2016, 09:12 AM
Command flags + PAWN.Cmd