AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to transfer a lot of options in "for" operator (https://forums.alliedmods.net/showthread.php?t=263612)

groofshark 05-29-2015 06:31

How to transfer a lot of options in "for" operator
 
Hello, I have a lot of options and I want to save space and turn them into "for" operator, but I dont know how... I tried but there isn't effect...

Simple Example Code


I want to do something like that, but now there isn't effect:
PHP Code:

for( new itemitem<sizeof(options-1); item++ )
{    
    for( new 
inuminum<sizeof(iItem-1); inum++ )
    {
        if( 
g_options[id][item] && iItem == inum 
        {
            return 
ITEM_DISABLED;
        }
    }


I would like to help me. Thanks :)

aron9forever 05-29-2015 09:10

Re: How to transfer a lot of options in "for" operator
 
just a heads up, just because the second code "looks better" it's actually worse in terms of optimization since you're uselessly adding two loops in there

this is micro optimization, aka it doesn't directly affect the server, but still, it's good not to get used to such practices as when they add up they start impacting the server

groofshark 05-29-2015 14:56

Re: How to transfer a lot of options in "for" operator
 
Didn't understand...

Neeeeeeeeeel.- 05-29-2015 15:39

Re: How to transfer a lot of options in "for" operator
 
If you tell us about what exactly you are doing we could help you optimizing your code. For example, what is iItem, g_option, etc.

As aron9forever said, if there are just 3 conditionals would be better to leave it as the first example.

groofshark 05-29-2015 17:22

Re: How to transfer a lot of options in "for" operator
 
I want exactly to change the content of menu_cb function to for operators. Something like 2nd code, but correctly written. No there are more options, just I want to see example.

fysiks 05-29-2015 17:57

Re: How to transfer a lot of options in "for" operator
 
Quote:

Originally Posted by groofshark (Post 2302526)
I want exactly to change the content of menu_cb function to for operators. Something like 2nd code, but correctly written. No there are more options, just I want to see example.

Just because you want to do it doesn't mean you should do it. As the others have already said, you need to provide better context of what you are trying to do. I wouldn't be surprised if this is an XY Problem.

Nextra 05-30-2015 04:37

Re: How to transfer a lot of options in "for" operator
 
You already have a lookup table that is indexed by your item numbers. If your code is more complex than the example you provided, you should always look towards crafting this type of setup, where you simply retrieve from an array index - it doesn't matter if it is of constant size or dynamic. If you have a case where you feel that a for loop is actually necessary, you need to update your example and show us.

Code:
if (g_options[id][iItem]) {    return MENU_DISABLED; }

groofshark 05-30-2015 04:47

Re: How to transfer a lot of options in "for" operator
 
Quote:

Originally Posted by Nextra (Post 2302645)
You already have a lookup table that is indexed by your item numbers. If your code is more complex than the example you provided, you should always look towards crafting this type of setup, where you simply retrieve from an array index - it doesn't matter if it is of constant size or dynamic. If you have a case where you feel that a for loop is actually necessary, you need to update your example and show us.

Code:
if (g_options[id][iItem]) {    return MENU_DISABLED; }

That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)

EDIT(sorry, double posting):
That's the example


I can't explain it better.

Bugsy 05-30-2015 10:31

Re: How to transfer a lot of options in "for" operator
 
What Nextra provided should do the trick as long as your sample code is accurate to what you're trying to do.

Nextra 05-30-2015 11:23

Re: How to transfer a lot of options in "for" operator
 
Quote:

Originally Posted by groofshark (Post 2302647)
That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)

You don't need to loop unless you have a more complex situation.

Code:
/**  * Given this enum the following things are true:  *    opt1 == 0 && opt2 == 1 && opt3 == 2  */ enum _:options {     opt1,     opt2,     opt3, } /**  * That means that this if can easily be reduced.  * Observe that when |iItem == 0| then |opt1 == iItem| is also true.  */ if(g_options[id][opt1] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * So instead we can write it as following, note that |opt1| has  * been replaced, and instead |iItem| is retrieved from the array.  * This does not change code behavior.  */ if(g_options[id][iItem] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * Since the code inside of your if statements does not  * differ (it always returns the same value), the |iItem == 0|  * check is actually redundant. This means that we can remove it  * and get rid of all the other if statements at the same time:  */ if(g_options[id][iItem]) {     return ITEM_DISABLED; }

A loop would unnecessarily complicate things. You need to do something based on a single value, and you have the means to directly derive the necessary information from that single value. You don't need to search for anything, you don't need to iterate. A lookup-table is perfect for this, and you already have it in place. You just need to use it properly.


All times are GMT -4. The time now is 08:42.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.