AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Pick random value from enum (https://forums.alliedmods.net/showthread.php?t=288941)

nhnkl159 10-11-2016 07:04

Pick random value from enum
 
Is it possible ?

enum example :

Code:

enum Days(+=1)
{
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
}


Fyren 10-11-2016 08:13

Re: Pick random value from enum
 
By default, enum values start from zero and increment for each. You can pick a random number from 0 to 6 and use it as a value for your enum, though you might have to manually tag/view_as to avoid warnings.

thecount 10-11-2016 15:48

Re: Pick random value from enum
 
For enums of varying or long lengths, you may also add an element at the end of the list to represent the amount of elements before it to help with looping and such. Like:
PHP Code:

enum type{
enum1//0
enum2//1
enum3//2
TOTAL_ENUM 3


For your situation, I think you would just want something like
PHP Code:

view_as<Days>:GetRandomInt(MondaySunday); 

or whatever the syntax is for that. I don't cast variables to different types very often in SourcePawn.

nhnkl159 10-12-2016 12:19

Re: Pick random value from enum
 
Quote:

Originally Posted by thecount (Post 2461203)
For enums of varying or long lengths, you may also add an element at the end of the list to represent the amount of elements before it to help with looping and such. Like:
PHP Code:

enum type{
enum1//0
enum2//1
enum3//2
TOTAL_ENUM 3


For your situation, I think you would just want something like
PHP Code:

view_as<Days>:GetRandomInt(MondaySunday); 

or whatever the syntax is for that. I don't cast variables to different types very often in SourcePawn.

Code:

int day = view_as<Days>(GetRandomInt(Monday, Sunday));
Got 2 same warnings on this line :
warning 213: tag mismatch
warning 213: tag mismatch

shavit 10-12-2016 14:09

Re: Pick random value from enum
 
Code:

enum Day
{
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday,
        MAX_DAYS
}

Day GetRandomDay()
{
        return view_as<Day>(GetRandomInt(0, view_as<int>(MAX_DAYS) - 1));
}


Emp` 10-12-2016 22:56

Re: Pick random value from enum
 
Code:

enum Day
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

// int iDay = GetRandomInt( 0, view_as < int > ( Day ) - 1 );
// Day dayRandom = view_as < Day > ( GetRandomInt( 0, view_as < int > ( Day ) - 1 ) );

Day RandomDay()
{
    return view_as < Day > ( GetRandomInt( 0, view_as < int > ( Day ) - 1 );
}
// Day dayRandom = RandomDay();

Day NextDay( Day dayStart = Sunday )
{
    return ( ++dayStart ) % Day;
}
// Day dayNext = NextDay( Saturday );



All times are GMT -4. The time now is 03:04.

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