AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved pass 'enum' as parameter (https://forums.alliedmods.net/showthread.php?t=338738)

wilian159 07-24-2022 16:42

pass 'enum' as parameter
 
I have that:

PHP Code:

enum _:xEnumProbItems PROB_ITEM_NAME[32], Float:PROB_ITEM_CHANCE }

new const 
xProbItems[][xEnumProbItems] =
{
    { 
"Chinelo"58.5 },
    { 
"Oculos"61.7 },
    { 
"Bazooka"0.01 },
    { 
"Rifle"0.1 },
    { 
"Pistola"2.5 },


and that

PHP Code:

stock probItems(const array2D[][], array2DSize)
{
    new 
find = -1
    
new Float:totalProbability 0.0

    
for(new 0array2DSizei++)
        
totalProbability += array2D[i][PROB_ITEM_CHANCE]

    new 
Float:stopAt random_float(0.00001totalProbability)
    new 
Float:currentProbability 0.0

    
for(new 0array2DSizei++)
    {
        
currentProbability += array2D[i][PROB_ITEM_CHANCE]

        if(
currentProbability >= stopAt)
            
find i

        
if(find != -1)
            break
    }

    return (
find != -1) ? find : -1


notice in the 'stock' that I use the 'enum: PROB_ITEM_CHANCE', but I don't want to use it this way, I want to pass the column 'chance' as a parameter in the stock, more or less like this

PHP Code:

probItems(const array2D[][], array2DSizeenumToCheck

I tested several ways, but it didn't work, bugged the sum of 'float' and other things

fysiks 07-24-2022 16:51

Re: pass 'enum' as parameter
 
My guess is that it will no longer know that it's a floating point value by not using PROB_ITEM_CHANCE strictly. Try applying the Float tag to array2D[][] where you're summing the values.

Also, when you say you tried something that doesn't work, you should provide that version of the code also so we can see what you tried and maybe fix it. It's hard to be sure what you actually tried when you describe it in words.

wilian159 07-24-2022 17:00

Re: pass 'enum' as parameter
 
Quote:

Originally Posted by fysiks (Post 2784619)
My guess is that it will no longer know that it's a floating point value by not using PROB_ITEM_CHANCE strictly. Try applying the Float tag to array2D[][] where you're summing the values.

Also, when you say you tried something that doesn't work, you should provide that version of the code also so we can see what you tried and maybe fix it. It's hard to be sure what you actually tried when you describe it in words.

well, I want to know if there is a way to pass the 'enum' parameter in the stock,


PHP Code:

probItems(xProbItemssizeof(xProbItems), PROB_ITEM_CHANCE

PHP Code:

stock probItems(const array2D[][], array2DSizechance)
{
    new 
find = -1
    
new Float:totalProbability 0.0

    
for(new 0array2DSizei++)
        
totalProbability += array2D[i][chance]

    new 
Float:stopAt random_float(0.00001totalProbability)
    new 
Float:currentProbability 0.0

    
for(new 0array2DSizei++)
    {
        
currentProbability += array2D[i][chance]

        if(
currentProbability >= stopAt)
            
find i

        
if(find != -1)
            break
    }

    return (
find != -1) ? find : -1


because I tried some ways and it didn't work, so I want to know if there's a 'right' way

fysiks 07-24-2022 17:09

Re: pass 'enum' as parameter
 
So, you're not going to read the first paragraph that I wrote? I don't know if that will fix it for sure but it's not hard to try.

wilian159 07-24-2022 17:18

Re: pass 'enum' as parameter
 
Quote:

Originally Posted by fysiks (Post 2784621)
So, you're not going to read the first paragraph that I wrote? I don't know if that will fix it for sure but it's not hard to try.

doesn't work, I recommend you test it yourself, before trying to guess

fysiks 07-24-2022 17:22

Re: pass 'enum' as parameter
 
Quote:

Originally Posted by wilian159 (Post 2784623)
doesn't work, I recommend you test it yourself, before trying to guess

If I was getting paid to waste my time on some random request on the internet I probably would do that more often.

wilian159 07-24-2022 17:27

Re: pass 'enum' as parameter
 
Quote:

Originally Posted by fysiks (Post 2784624)
If I was getting paid to waste my time on some random request on the internet I probably would do that more often.

to disturb the post bro, if you don't have the solution don't comment.

lexzor 07-24-2022 17:53

Re: pass 'enum' as parameter
 
try
PHP Code:

stock probItems(const xEnumProbItems:array2D[][], array2DSize


wilian159 07-24-2022 18:02

Re: pass 'enum' as parameter
 
Quote:

Originally Posted by lexzor (Post 2784635)
try
PHP Code:

stock probItems(const xEnumProbItems:array2D[][], array2DSize


it makes no sense because it would be dependent on 'xEnumProbItems', the idea is to make it dynamic,

what i need is this:

Call
PHP Code:

probItems(xProbItemssizeof(xProbItems), PROB_ITEM_CHANCE


PHP Code:

stock probItems(const array2D[][], array2DSizecolumChanceToCheck)
    for(new 
0array2DSizei++)
        
totalProbability += array2D[i][columChanceToCheck

but when i try the value is not correct

fysiks 07-24-2022 18:47

Re: pass 'enum' as parameter
 
My original suggestion works perfectly for me:

PHP Code:

enum myEnum {NAME[32], Float:CHANCE};
new 
myArray[][myEnum] = 
{
    {
"item1"10.1},
    {
"item2"20.1},
    {
"item3"30.1},
    {
"item4"40.1},
}


public 
cmdTest()
{
    
server_print("sum:  %f"myFunction(myArraysizeof myArrayCHANCE))
}

stock Float:myFunction(array[][], size, const index)
{
    new 
Float:fProb 0.0
    
for( new 0sizei++ )
    {
        
fProb += Float:array[i][index]
    }
    return 
fProb


Output is:

Code:

sum:  100.400001
Which is correct. Without the "Float:" where I said to put it, I get

Code:

sum:  4409498112.000000


All times are GMT -4. The time now is 15:40.

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