SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 04/01/2018, 04:20 PM   #1
TahaMhr
Little Clucker
 
Join Date: Aug 2017
Posts: 24
Reputation: 1
Question Can different checks be converted into single check?

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;
Code:
	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
Code:
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.
TahaMhr is offline   Reply With Quote
Old 04/01/2018, 04:35 PM   #2
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,944
Reputation: 504
Default Re: Can different checks be converted into single check?

you can use another array something like errstate[MAX_PLAYERS] and use this array to store the message id
Code:
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 Code:
if (errstate[playerid] != -1)
{
     return 
ErrorMessages(playeriderrstate[playerid]);

SyS is offline   Reply With Quote
Old 04/01/2018, 05:53 PM   #3
admantis
High-roller
 
Join Date: Sep 2009
Posts: 2,524
Reputation: 449
Default Re: Can different checks be converted into single check?

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:

Code:
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:
Code:
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:
Code:
CMD:teleportcar(playerid, params[]) {
	if(TeleportDisabled(playerid, .vehiclesOnly = 1)) return ErrorMessage(playerid, TeleportDisabled(playerid));
	
	// Teleport the car
	return 1;
}
admantis is offline   Reply With Quote
Old 04/01/2018, 07:01 PM   #4
Beckett
High-roller
 
Beckett's Avatar
 
Join Date: Feb 2012
Location: www.finalnights-rp.com
Posts: 1,794
Reputation: 104
Default Re: Can different checks be converted into single check?

Quote:
Originally Posted by admantis View Post
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:

Code:
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.
Beckett is offline   Reply With Quote
Old 04/01/2018, 10:02 PM   #5
admantis
High-roller
 
Join Date: Sep 2009
Posts: 2,524
Reputation: 449
Default Re: Can different checks be converted into single check?

Quote:
Originally Posted by DaniceMcHarley View Post
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 View Post
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
Code:
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.
admantis is offline   Reply With Quote
Old 04/01/2018, 10:21 PM   #6
Sew_Sumi
Banned
 
Join Date: Jun 2008
Location: Azerbaijan
Posts: 6,244
Reputation: 525
Default Re: Can different checks be converted into single check?

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.
Sew_Sumi is offline   Reply With Quote
Old 05/01/2018, 05:05 PM   #7
TahaMhr
Little Clucker
 
Join Date: Aug 2017
Posts: 24
Reputation: 1
Default Re: Can different checks be converted into single check?

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.
TahaMhr is offline   Reply With Quote
Old 06/01/2018, 01:23 PM   #8
GhostHacker9
Big Clucker
 
GhostHacker9's Avatar
 
Join Date: Jan 2017
Location: New Zealand
Posts: 105
Reputation: 109
Default Re: Can different checks be converted into single check?

Quote:
Originally Posted by TahaMhr View Post
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.
GhostHacker9 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can this be converted? Tomix Scripting Help 2 02/06/2013 11:11 AM
can anyone checks? iBot Scripting Help 3 17/11/2012 02:09 AM
Checks Machida Help Archive 4 20/08/2011 12:16 AM
How to do these Key Checks? Bcklup Help Archive 4 09/07/2010 06:59 AM
Cannot see objects when converted from MTA? gemadon Help Archive 15 04/11/2009 10:44 PM


All times are GMT. The time now is 10:45 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.