Using a String Variable to identify a table name

Morning all,

Been experiencing a small challenge and wanting to establish if it can be done this way, or if I am doing something wrong.

What I am trying to achieve is to pass the name of a table in some script that I have written as a string variable:

  1. Returns a argument#0 error when compiling

Table_Name = RECIPE;
Table_Name_Len = GetLengthOfTable(Table_Name);

  1. No error when compiling, however does not write value to table element.

Recipe_Value = 150;
Table_Name[0] = Recipe_Value ;

Can this be achieved ?
If I declare the actual name of the table. RECIPE[0] = Recipe_Value, the data is stored

All data types are matched.

Kind Regards
Robin

Welcome to the wonderful world of “Needing to Use Pointers”. :smile:

Basically what you’ll do is to find the table by name using ‘GetPointerFromName’, with the name being the name of your recipe (Chili or somesuch), that’ll give you a pointer to the table. To use that pointer, you’ll ‘dereference’ it by putting a * in front of it, which means ‘do what I’m asking with the thing pointed at by this pointer, not to the pointer itself’. So if you say ‘*mypointer = 12’, you’re not setting the pointer variable to 12, you’re setting the variable pointed to by the pointer to 12. Hope that’s clearer than mud.

So here’s the sample. Have a float table named ‘Chili’ defined, a string named ‘s_RecipeName’, a float named ‘f_RecipeValue’, an int32 named ‘i_RecipeValueIndex’, and that really fun and scary pointer named ‘tp_TablePointer’ with a pointer type of ‘Float Table’, because that’s what we’re going to ask it to point to.

// set up some variables for this test
s_RecipeName = “Chili”;
i_RecipeValueIndex = 3;
f_RecipeValue = 25.4;

// get a pointer to the recipe table for your recipe
GetPointerFromName(s_RecipeName, tp_TablePointer);

// using the pointer, dereference it and pop a value in the right index
*tp_TablePointer[i_RecipeValueIndex] = f_RecipeValue;

If all goes well (and it did when I test ran it), your ‘Chili’ table should end up containing ‘25.4’ in index 3. As a side note, you really really should check the pointer for NULL before you write something using it. I didn’t do that here, because, well… I’m lazy. :slight_smile:

3 Likes

Hi Todd,

Thankyou for the advice and “pointing” me in the right direction. Ha Ha

The way you have explained make sense and I will give it a go in the next day or two.

Appreciate the input !

Best Regards
Robin