Home AMX User Forum NetLinx Studio
Options

variable variable names

Hello,

was wondering if there is option to make variable variable names in netlinx?

What i have in mind is like you have option in php to change the variable name...

Example:
if i have 3 variables named room1,room2,room3
and i want to put same value in them in lets say a for loop.

this is what it would look like in php:
for ($i=1;$i<=10;$i++)
{
${"room".$i)='some value';
}

is this possible in netlinx or not :)

tnx

Comments

  • Options
    Use an Array of variable

    You can use an array of variable
    char Room[3][20]

    local_var integer i
    for(i=1;i<=3;i++)
    {
    room = 'some value'
    }
  • Options
    idea is to change device names

    but tnx for your answer
  • Options
    the8thstthe8thst Posts: 470
    No you cannot use dynamic variable names in Netlinx.

    Array will work as stated above, but structures are cleaner and allow you to store a lot more info in 1 place.

    For example:
    Here we track 10 rooms with names, volume, state, source, etc.
    Upon startup we will initialize the structure with all rooms off and volume at zero (not something you would do in the real world I know).
    We also set the room names for each room because it is volatile in this example (again not practical in the real world).

    I think you will find this a lot easier than keeping a whole mess of individual typed variable for every room.

    ** sorry for the huge tab spaces. I don't know why chrome does that when I paste code from NS3 **
    define_type
    	struct _roomInfo {
    		char name[15]
    		integer state
    		integer volume
    		integer sourceIndex
    		char sourceName[15]
    	}
    
    define_variable
    	volatile _roomInfo sMyRooms[10]
    
    define_start
    	{
    		stack_var integer x
    		
    		for(x = 10; x; x--) {
    			sMyRooms[x].name = "'Room #',itoa(x)"
    			sMyRooms[x].state = 0
    			sMyRooms[x].volume = 0
    			sMyRooms[x].sourceIndex = 0
    			sMyRooms[x].sourceName = 'Room is Off'
    		}
    	}
    
  • Options
    <OFF TOPIC>
    the8thst wrote: »
    ** sorry for the huge tab spaces. I don't know why chrome does that when I paste code from NS3 **

    That spacing problem is actually the message board's handling of TAB characters. You probably have Studio set up to use a tab stop of 4 characters and the Board's tab stop is 8. So the spacing you see in Studio doubles:
    123456	123456
    
    1234567	1234567
    
    12345678	12345678
    
    
    </OFF TOPIC>
  • Options
    mpullinmpullin Posts: 949
    Yes, you want to look into structures. They are a great way of organizing data into entities that you want to know multiple things about, e.g. name and value. I'm not entirely sure what you were asking if you could do as your pseudocode contains syntax errors. :)
Sign In or Register to comment.