AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   other way of while loop? (https://forums.alliedmods.net/showthread.php?t=55525)

Crusher918 05-23-2007 20:41

other way of while loop?
 
I was trying to make something for my plugin that has this

PHP Code:

#include <amxmodx>
 
#define PLUGIN "while loop"
#define VERSION "1.0"
#define AUTHOR "Crusher"
 
new x[10]
 
public 
plugin_init() {
 
register_plugin(PLUGINVERSIONAUTHOR)
 
register_plugin("say /test","cmd_test")
}
public 
cmd_test(id) {
 new 
number 
 
 
while(x[number] != 1) {
 
  
number random_num(1,10)
 
 }
 
x[number] = 1
 
 client_print
(id,print_chat,"Test number: %d",number)
 return 
PLUGIN_HANDLED


the problem is the 9th or 10th time using the command Sometimes the number returns as 0

Does anyone knows of any other way to do this?

Cheap_Suit 05-23-2007 21:08

Re: other way of while loop?
 
PHP Code:

new x[11


Crusher918 05-23-2007 23:35

Re: other way of while loop?
 
Quote:

Originally Posted by Cheap_Suit (Post 479904)
PHP Code:

new x[11


but i want all the numbers to be used

and making it like that would give the 10th time to 0

Cheap_Suit 05-23-2007 23:56

Re: other way of while loop?
 
You can only store 10 numbers with an array[10] (0-9)
So by increasing the array to 11 you'll able to use 0 - 10,
or 1-10 in your case.

Crusher918 05-24-2007 00:44

Re: other way of while loop?
 
Quote:

Originally Posted by Cheap_Suit (Post 479934)
You can only store 9 numbers with an array[10] (0-9)
So by increasing the array to 11 you'll able to use 0 - 10,
or 1-10 in your case.

i know that but wat i meant was i want all the array to be used
because i want the users to use the cmd so each person gets a number and they all have to be different numbers

regalis 05-24-2007 04:49

Re: other way of while loop?
 
Quote:

Originally Posted by Crusher918 (Post 479900)
I was trying to make something for my plugin that has this

PHP Code:

#include <amxmodx>
 
#define PLUGIN "while loop"
#define VERSION "1.0"
#define AUTHOR "Crusher"
 
new x[10]
 
public 
plugin_init() {
 
register_plugin(PLUGINVERSIONAUTHOR)
 
register_plugin("say /test","cmd_test")
}
public 
cmd_test(id) {
 new 
number 
 
 
while(x[number] != 1) {
 
  
number random_num(1,10)
 
 }
 
x[number] = 1
 
 client_print
(id,print_chat,"Test number: %d",number)
 return 
PLUGIN_HANDLED


the problem is the 9th or 10th time using the command Sometimes the number returns as 0

Does anyone knows of any other way to do this?

I think x[number] will be everytime = 0 !
Because you initialize the array and don't assign numbers...
In your case:
x[0] = 0
x[1] = 0
x[2] = 0
x[3] = 0
....and so on

You should do something like that

new x[] = {1,2,3,4,5,6,7,8,9,10}

That should do the trick..for all numbers to be different you have to work out a function which stores the numbers given, and compare every new number with all the given ones...
I'm a little tired maybe i'll post later a function when my coffee wake up my
brain*lol*

greetz regalis

_Master_ 05-24-2007 13:37

Re: other way of while loop?
 
Quote:

Originally Posted by regalis (Post 479997)
I think x[number] will be everytime = 0 !

That's good bacause the test is != 1 (0 means the number hasn't been assigned to a player, 1 means it has - so he must peek another)

@ Crusher918 : use Cheap_Suit's hotfix :D. Also the is another thing you should consider: if random_num() returns 3 in a loop of 100 it will take a while to execute that command. Also random_num() seldom returns the max allowed value, so if that value is the only one left valid it will also take a while to do it.

Crusher918 05-24-2007 17:11

Re: other way of while loop?
 
Quote:

Originally Posted by _Master_ (Post 480113)
That's good bacause the test is != 1 (0 means the number hasn't been assigned to a player, 1 means it has - so he must peek another)

@ Crusher918 : use Cheap_Suit's hotfix :D. Also the is another thing you should consider: if random_num() returns 3 in a loop of 100 it will take a while to execute that command. Also random_num() seldom returns the max allowed value, so if that value is the only one left valid it will also take a while to do it.

yes that wat im trying to do

and for the random_num()
-Sometimes the last number would return to 0 due to reaching the max value
well is there any other way to do it so if its one left it will choose the last number?

_Master_ 05-25-2007 01:36

Re: other way of while loop?
 
There are 2 obvious ways you can do this:
1. extend the max value to 15 for instance. Thus you will have 5 possible values above the max allowed (15 might be a bit much but it will do the job). It will look like this:
PHP Code:

public cmd_test(id){
    
// number must be initialized because, in this case, x[0] will always be != 1
    
new number 1
    
    
while(x[number] != 1){
        
number random_num(115)
        if(
number 10number 10
    
}
    
    
x[number] = 1
    
    
//
    // ADD STUFF HERE
    //
    
    
return PLUGIN_HANDLED


2. This method implies that the number is rather incremental (ie NOT random) !!! You can re-organize the way the index is used: At the moment x[i] stores the VALUE assigned to the player index (refferrenced by "i"). Instead of this you can store the player INDEX in x[i], "i" means the number assigned to. This way you will have a "time-finite" method and you won't have to worry about random_num(). The code will look like this:
PHP Code:

public cmd_test(id){
    new 
i
    
    
// x[i] holds the player index so if it's == 0 then it hasn't been used
    // In this case stop looking any further.
    
for(111i++) if(!x[i]) break
        
    
x[i] = id
    
    
//
    // ADD STUFF HERE
    //
    
    
return PLUGIN_HANDLED



There are other methods you can use. For the purpose of this you could write a custom_random_num() function by yourself and handle those drawbacks in std random_num(); you could use Mel's RandomX module (also a bit too much :D), etc.

kmal2t 05-25-2007 04:23

Re: other way of while loop?
 
I think I know what you mean and I did something like this the other day with 2 for loops to have one give numbers and the other check the numbers for repeats.

Personally I'd like to see a function that could assign numbers to arrays for a random number set. I.e if you have an array[10] those array[0] through array[9] would each be given randomly one of the numbers between 1-10 so that none of the arrays have the same number like:
new array[6]
command(array)
then with a get_command(array) you would know that:
array[0] = 3
array[1] = 5
array[2] = 6
array[3] = 2
array[4] = 4
array[5] = 1
etc. etc.

You could use this to easily shufle players to different groups or give each player unique things like every player has one random individual weapon or characteristic without having to check if a number has already been used.


All times are GMT -4. The time now is 10:31.

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