AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Random num saving for another function (https://forums.alliedmods.net/showthread.php?t=240693)

Flick3rR 05-20-2014 10:07

[HELP] Random num saving for another function
 
Hey guys! I want to ask, because I didn't find the solution, about saving the random num from random_num native. My question: is there a way you get random_num via the native once, and then get the same random num, as the native got before, but using it in another function. Like to save the got random num from this function, then use it in the other function, and the forget it and start from beging with the first function and another random num?

NikKOo31 05-20-2014 10:14

Re: [HELP] Random num saving for another function
 
Save it in a global var when you generate it. After you use it reset the var to a value out of the range of random_num range (?)

Flick3rR 05-20-2014 10:19

Re: [HELP] Random num saving for another function
 
Oh, right, global vars. I'm so dumb for not trying this. Will test and post if something goes wrong. But for now, the case is closed :D
EDIT: Nope, even in a global var the random num is again random in every use of this var... Or I didn't get you right, or this metod doesn't work at all :D Any suggestions?

aron9forever 05-20-2014 11:03

Re: [HELP] Random num saving for another function
 
okay, looks like a bad case of someone who doesn't know basic coding or scripting
here's the deal, you can save your value in a global variable and it will stay the same as long as the function that generated the random number and saved it is not called again

if something keeps calling the random number generation of course it will keep overwriting your global value

Flick3rR 05-20-2014 11:15

Re: [HELP] Random num saving for another function
 
Do you think I didn't get it earlier? Would I make a thread if I didn't try the simplest thing at all. :D Hating all the time, anyways, right?
Here is an example, which doesn't work for me:
PHP Code:

//That's global
new NumP

//Later in a function
    
for( new 0sizeofprimary_weapons ); i++ )
    {
        
NumP random_num(0i)
        
        
        
AmmoNum primary_weapons[NumP][_Ammo]
        
        
give_item(idprimary_weapons[NumP][_WeaponName])
        
        
cs_set_user_bpammo(idprimary_weapons[NumP][_CSW], AmmoNum)

        
client_print(idprint_chat"You got %s"primary_weapons[NumP][_WeaponNameForChat])
    } 

And the weapon it gives me and that, which appears in chat are different...

EDIT: Yea, all of us make mistakes. I agree with the upper posts, just didn't get the idea and meaning of global var. Just an index and it saves, clear. Thank you, mostly nikko.

Black Rose 05-20-2014 17:37

Re: [HELP] Random num saving for another function
 
Quote:

Originally Posted by Flick3rR (Post 2140325)
Do you think I didn't get it earlier? Would I make a thread if I didn't try the simplest thing at all. :D Hating all the time, anyways, right?
Here is an example, which doesn't work for me:
PHP Code:

//That's global
new NumP

//Later in a function
    
for( new 0sizeofprimary_weapons ); i++ )
    {
        
NumP random_num(0i)
        
        
        
AmmoNum primary_weapons[NumP][_Ammo]
        
        
give_item(idprimary_weapons[NumP][_WeaponName])
        
        
cs_set_user_bpammo(idprimary_weapons[NumP][_CSW], AmmoNum)

        
client_print(idprint_chat"You got %s"primary_weapons[NumP][_WeaponNameForChat])
    } 

And the weapon it gives me and that, which appears in chat are different...

EDIT: Yea, all of us make mistakes. I agree with the upper posts, just didn't get the idea and meaning of global var. Just an index and it saves, clear. Thank you, mostly nikko.

That will generate a random number every loop.
Don't need global for that.

NikKOo31 05-20-2014 19:35

Re: [HELP] Random num saving for another function
 
This is what I mean

PHP Code:

//That's global
new NumP


//When you decide to change random num
    
NumP = -//some random value not in 0 - i range (if you change it here, random num will be generated again, else will keep previous value)

//Later in a function
    
for( new 0sizeofprimary_weapons ); i++ )
    {
        if(!(
<= NumP <= i)) //if the value is out of the range
            
NumP random_num(0i//generate another random
        //else keep previous value :)
        
AmmoNum primary_weapons[NumP][_Ammo]
        
        
give_item(idprimary_weapons[NumP][_WeaponName])
        
        
cs_set_user_bpammo(idprimary_weapons[NumP][_CSW], AmmoNum)

        
client_print(idprint_chat"You got %s"primary_weapons[NumP][_WeaponNameForChat])
    } 

If I'm right, you are doing a loop with players, will be easier to put that loop inside this one so every one will get same item, unless you are using the random num in other function, then this code will help you :)

Flick3rR 05-21-2014 01:18

Re: [HELP] Random num saving for another function
 
Problem solved. Excuse that stupid question, wasn't me that night.... My brother is trying to write, making stupid threads all the forums, spamming the people's inboxes... Problem solved anyways, thanks! And now to change that password :D


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

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