From string to Enum variable
#1

Is it possible to convert a string into a variable name?

PHP код:
enum info
{
    
Float:black1[3],
Float:black2[3],
    
Float:white1[3],
Float:white2[3],
}
new 
Maps[MAX_MAPS][info]; 
PHP код:
new GetRandomNumber(1,2);
Maps[id][black+r// How do I do this 
how can I make string like "black1" into the enum
Reply
#2

You can't.
During runtime, you cannot modify a variablename as they are converted into memory addresses during compilation.
The variablename is lost when you compile your code.
Manipulating variablenames through code won't work because of that.

If you need to add numbers to it, just make an array so you can use that number as the index in the array.
Reply
#3

PHP код:
new GetRandomNumber(1,2);
switch(
r)
{
case 
0: {Maps[id][black1]}
case 
1: {Maps[id][black2]}

Could be a solution tbh
Reply
#4

When you declare an enum, the name of this enum is the constant of the totality of your constants in your enum.
Example :
PHP код:
enum e_MY_ENUM
{
    
e_A,
    
e_B,
    
e_C

Here, e_MY_ENUM is 3, because there are 3 constants within it.

e_A, e_B and e_C, here are constants. They are enumarated starting with 0 by default by the compiler.

So :
e_A is 0
e_B is 1
e_C is 2

But when you name your enum, you tag the constants in the enum by the name of it.

So really :
e_A is e_MY_ENUM:0
e_B is e_MY_ENUM:1
e_C is e_MY_ENUM:2

For your problem, you can't do this, but with the previous explanation, you can do this :
PHP код:
enum info 

    
Float:black1[3], 
    
Float:black2[3], 
    
Float:white1[3], 
    
Float:white2[3]

new 
    
Maps[MAX_MAPS][info];  
main()
{
    new 
        
id 0,
        
random(2);
        
    
Maps[id][info:r][/* a value between 0 and 2 */]; // there is correct

Reply
#5

Quote:
Originally Posted by Dutheil
Посмотреть сообщение
When you declare an enum, the name of this enum is the constant of the totality of your constants in your enum.
That is incorrect. While the name of the enum is indeed a constant, its value is the value of the last constant + 1. Therefore if you have something like this:
PHP код:
enum eMyEnum (<<= 1) {
    
VAR_A 1,
    
VAR_B// this will be 2
    
VAR_C // this will be 4

Then eMyEnum will be 5.
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
That is incorrect. While the name of the enum is indeed a constant, its value is the value of the last constant + 1. Therefore if you have something like this:
PHP код:
enum eMyEnum (<<= 1) {
    
VAR_A 1,
    
VAR_B// this will be 2
    
VAR_C // this will be 4

Then eMyEnum will be 5.
Yes I was not quite awake when I wrote that
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)