Can different checks be converted into single check?
#1

Hello, a question just popped in my mind to make my script a bit more efficient(I guess so xd), but have no idea how to do this. I know the topic name isn't self explanatory so here's what Im thinking:
for example, in my script I have many teleports, and for each of them I have to check the player condition one by one like this;
Код:
	if (InDM[playerid] == 1)
        {
		return ErrorMessages(playerid, 12);
		}
        else

        if(AccInfo[playerid][Frozen] == 1)
        {
        	return ErrorMessages(playerid, 14);
        }
        else

        if(AccInfo[playerid][Jailed] == 1)
        {
        	return ErrorMessages(playerid, 15);
        }
        else

	if(AccInfo[playerid][pCaged] == 1)
	{
		return ErrorMessages(playerid, 16);
	}
	else
	
        if(InDuel[playerid] == 1)
	{
		return ErrorMessages(playerid, 17);
	}
	else
              
        {
                 //main function here
        }
that means if I add something new, I would have to go add new piece of code in each of the teleport code lines. What I want is I make something like
Код:
TeleportRestrictions
containing all of the above checks and use it in the code(where ever these checks are required) so I'll just have to update this when a new thing is added, and all the teleports will be updated. Ofcourse, these kinds of checks ar not only used in teleports, they're almost everywhere. Im sure this can be done in one way or another, but I don't exactly know how?
Any help would be appreciated and thanks.
Reply
#2

you can use another array something like errstate[MAX_PLAYERS] and use this array to store the message id
Код:
when InDM[playerid] becomes 1
set  errstate[playerid] -> 12
and use some other value to represent "free of restriction" something like -1 or -2
and do this
PHP код:
if (errstate[playerid] != -1)
{
     return 
ErrorMessages(playeriderrstate[playerid]);

Reply
#3

Make a function that returns a value (a number: 0 for success, anything else for an error code) and use it as a validator across your commands. Example:

Код:
TeleportDisabled(playerid) {
	if(InDM[playerid] == 1) {
		return 12;
	}
	// Etc.
	return 0;
}

CMD:teleport(playerid, params[]) {
	if(TeleportDisabled(playerid)) return ErrorMessage(playerid, TeleportDisabled(playerid));
	
	// Teleport the player
	return 1;
}
You can also create optional parameters to check for extra circumstances. For example:
Код:
TeleportDisabled(playerid, vehiclesOnly = 0) {
	if(InDM[playerid] == 1) {
		return 12;
	}
	if(!IsPlayerInAnyVehicle(playerid)) && vehiclesOnly == 1) {
		return 24;
	}
	// Etc.
	return 0;
}
When calling that function, you won't be required always to specify a value for vehiclesOnly. You can then use it as this:
Код:
CMD:teleportcar(playerid, params[]) {
	if(TeleportDisabled(playerid, .vehiclesOnly = 1)) return ErrorMessage(playerid, TeleportDisabled(playerid));
	
	// Teleport the car
	return 1;
}
Reply
#4

Quote:
Originally Posted by admantis
Посмотреть сообщение
Make a function that returns a value (a number: 0 for success, anything else for an error code) and use it as a validator across your commands. Example:

Код:
poof
That doesn't really change anything though, he's looking for code optimization.

@TahaMhr, I think that the issue here is the way you've designed this code, can you explain what you're doing exactly? So we can get the better picture.
Reply
#5

Quote:
Originally Posted by DaniceMcHarley
Посмотреть сообщение
That doesn't really change anything though, he's looking for code optimization.

@TahaMhr, I think that the issue here is the way you've designed this code, can you explain what you're doing exactly? So we can get the better picture.
The code is already as optimized as it can possibly be. There's no way to make it "faster" or more "optimized". If you crammed everything into a single line you'd be unable to differentiate the different error codes for each error. The solution was creating a function that returns a value to determine whether you are able to teleport or not, and that way you can edit that single function instead of editing all the commands if you want to alter the criteria needed to perform the command. Read the original post and the part I've highlighted in bold letters and you'll see that I literally provided the right solution for the problem they are addressing:

Quote:
Originally Posted by TahaMhr
Посмотреть сообщение
Hello, a question just popped in my mind to make my script a bit more efficient(I guess so xd), but have no idea how to do this. I know the topic name isn't self explanatory so here's what Im thinking:
for example, in my script I have many teleports, and for each of them I have to check the player condition one by one like this;

that means if I add something new, I would have to go add new piece of code in each of the teleport code lines. What I want is I make something like
Код:
TeleportRestrictions
containing all of the above checks and use it in the code(where ever these checks are required) so I'll just have to update this when a new thing is added, and all the teleports will be updated. Ofcourse, these kinds of checks ar not only used in teleports, they're almost everywhere. Im sure this can be done in one way or another, but I don't exactly know how?

Any help would be appreciated and thanks.
Reply
#6

This, is why most 'edits' collapse... Because people simply keep tacking shit onto the end of a shit edit, and end up with the inherently shitty edit.


It's good that you've asked about this, as this is part of logic.

Sys has the right point.

Say for instance you have the teleport system... Have a variable for each player, showing whether or not they can tele in the first instance.

With admin commands you can check if the players are admins at all in the first instance, rather than checking if they are connected, and whether they are in a certain skin or such.

With the jailed/frozen/muted checks, you could make a variable for the player 'Disciplined', then anything that denies access coming from them being disciplined can be checked before everything else that is checked.


The less checks, the better... The more efficient you can be in this, the better you will handle this.
Reply
#7

Well I guess only @admantis (Thanks for that) understood me. I'll try to play with all the functions above, and will tell you guys the method that worked for me. Thanks to all of you.
Reply
#8

Quote:
Originally Posted by TahaMhr
Посмотреть сообщение
Well I guess only @admantis (Thanks for that) understood me. I'll try to play with all the functions above, and will tell you guys the method that worked for me. Thanks to all of you.
admantis code doesn't avoid those checks its similar to what you doing now only difference is there is an extra overhead for the function. Solving this logically would be better solution as SyS said which reduces the "checks" to only one.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)