Fetching Global Variable Value Using String Variable?

Fetching Global Variable Value Using String Variable?

rajankur6494
Advocate Advocate
90 Views
25 Replies
Message 1 of 26

Fetching Global Variable Value Using String Variable?

rajankur6494
Advocate
Advocate

[ FlexSim 21.0.10 ]


Hi Team,

I am using string variable to get global variable value. But it is writing variable name instead of value. What is the right way to do it?


1654008091820.png

GlobalVariableUsingStringVariable.fsm

Thank you!

0 Likes
Accepted solutions (1)
91 Views
25 Replies
Replies (25)
Message 2 of 26

kavika_faleumu
Autodesk
Autodesk

Hey @Ankur A3, from what I understand you want to retrieve the value of a cell from your global table using code? If so, then you're close! Right now, you're assigning the cell the value "TableValue". That's because whatever is on the left of the "=" will be assigned the value of whatever is on the right of the "=". If you want to retrieve the value rather than assign it, put it on the left like so: 1654021918505.png

I set a variable "var value" equal to the result of the right side (whatever is in the global table at that cell). Now you can use that value later on in the code if you want. Did this answer your question?

0 Likes
Message 3 of 26

Matthew_Gillespie
Autodesk
Autodesk
Accepted solution

You can use the executestring() command to execute a text string as FlexScript code. For example:

int val = executestring("Table" + "Value");


Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 4 of 26

rajankur6494
Advocate
Advocate
Thank you so much! @Matthew Gillespie
0 Likes
Message 5 of 26

rajankur6494
Advocate
Advocate

Hi @Kavika F,

I was asking same what @Matthew Gillespie answered.

Thanks for your support!

0 Likes
Message 6 of 26

rajankur6494
Advocate
Advocate

Hi @Matthew Gillespie,

This solution is working fine if I want to write global variable value.

I also want to change global variable value in similar fashion. It is not working in the same way. What is the right way to do it?

1654061744882.png

Thank you!

0 Likes
Message 7 of 26

jason_lightfootVL7B4
Autodesk
Autodesk
executestr(str1+"Value")

returns number. So you're then setting a number (eg. 5) to 80 - which doesn't make sense. You seem to want

executestring(str1+"Value=80");

But this looks all very poorly structured to me - it might serve you better to thing about the wealth of supported data structures and data types in FlexSim and choose something different (Maps, Arrays, Tables, Lists, Pointers).

0 Likes
Message 8 of 26

rajankur6494
Advocate
Advocate

Hi @Jason Lightfoot,

Thanks for your answer!

Actually, I have series of global variables having some common text (Eg: Obj1_Mon_Utilization, Obj2_Tue_Utilization etc). I have to access all variables for n number of objects. So, I want to create a object loop for better scalability.

Would you like to suggest any other approach?

Thank you!

0 Likes
Message 9 of 26

rajankur6494
Advocate
Advocate

Hi @Jason Lightfoot,

I have one more question if I can. I need to increment global variable by 1 every time. How can I do it using same approach?

I tried using below code. But it is throwing error:

executestring(str1+"Value=+1");
0 Likes
Message 10 of 26

rajankur6494
Advocate
Advocate
Sorry @Jason Lightfoot,

I got it. I typed it wrong. it should be "Value+=1".

Thanks for your support!

0 Likes
Message 11 of 26

jason_lightfootVL7B4
Autodesk
Autodesk

Your syntax is just a little off:

executestring(str1+"Value+=1");

or

executestring(str1+"Value++");
0 Likes
Message 12 of 26

jason_lightfootVL7B4
Autodesk
Autodesk
It's hard to suggest the best structure without know exactly what you're trying to do. The fact that you have to create numerous variables with different names that match other object names suggests that you're making something hard to build and maintain rather than something that does the heavy work for you.
0 Likes
Message 13 of 26

rajankur6494
Advocate
Advocate

Thank you! @Jason Lightfoot

One more question if I can. How to assign its value to table cell?

I am trying it as below: But it is not the right syntax.

executestring(str1+"Value=Table("GlobalTable")[1][1]");

It is throwing error.

Thank you!


0 Likes
Message 14 of 26

rajankur6494
Advocate
Advocate
Hi @Jason Lightfoot,

Let me give you fair idea what I want.

There are 10 objects with string name: A, B, C, D, E, F, G, H, I, J

I have multiple global variable having common text other than object name. Here is just one of them as below:

A_Count, B_Count, C_Count..............................J_Count

I have around 100 lines of code related to 1 object having multiple same kind of global variables. So, I want to create loop with object names as below to access code for every object as below:

for(int i=1; i<=10; i++)

{ String obj_name=Table("GlobalTable")[1];

executestring(obj_name+"_Count+=1");

}


0 Likes
Message 15 of 26

jason_lightfootVL7B4
Autodesk
Autodesk

Why do you have all those global variables? Why aren't they attributes of your objects - such as labels?

Why can't you refer to all your objects as members of a group?

Array myobjects=Group("MyGroup").toFlatArray();

have any loop iterate through that array and update any labels on the objects themselves.

If all those objects are of the same type then you can even use a template to manage them all through the template master (v22+)

0 Likes
Message 16 of 26

jason_lightfootVL7B4
Autodesk
Autodesk

Try

  1. executestring(str1+"Value=Table(\"GlobalTable\")[1][1]");
0 Likes
Message 17 of 26

rajankur6494
Advocate
Advocate

Hi @Jason Lightfoot,

It's difficult to follow like this.

Can you make some changes in model just for demo if possible?

GlobalVariableApproach.fsm

Thank you!


0 Likes
Message 18 of 26

rajankur6494
Advocate
Advocate
Thank you! @Jason Lightfoot
0 Likes
Message 19 of 26

jason_lightfootVL7B4
Autodesk
Autodesk
0 Likes
Message 20 of 26

rajankur6494
Advocate
Advocate
Thank you so much! @Jason Lightfoot


0 Likes