Asked
How To Create Multidimensional Arrays
Could you please show me how to create an array with 20 names, each associated with three values (x, y, z)? Additionally, I would like to generate a chart on the desktop that displays these names and their corresponding values.
Furthermore, I need to extract the values for a specific name and use them to control servo movements.
This assistance would be greatly appreciated.
Thank you, Don
I've also noticed duplicate global variables showing up in the Smart Variable Watcher skill when I run scripts several times. I'm not really sure this effects anything but may in your case. I'm not sure what causes this but seems to happen after I edit a script (I think). When way I fix it is I press the "Refresh" button at the bottom of the Smart Variable Watcher skill. Not the other button down their or everything will be reset. After I refresh the skill duplicate global variables in the list don't seem to happen anymore during that session.
Ok good to know Dave, thanks.
@Athena Have the array running pretty well today and started to make different scripts to organize things. Something that I noticed is don't you have to use the getVar("$objectLocations") function to for if (typeof objectLocations === 'undefined') because you are questioning the global variable objectLocations What is the proper way to write this code? Do I need to refresh the global variable objectLocations at the bottom of each script each time the array has been manipulated?
How do I change the name of location_5 to Advil? How do I change the y value of location_5 to 53?
To address your questions and provide clarity on working with global variables and arrays in ARC, let's break down the steps and correct the code snippets you've mentioned.
Checking if Global Variable Exists and Initializing
When you want to check if a global variable (like
objectLocations
) exists and initialize it if not, you indeed need to use thegetVar()
function. This function allows you to retrieve the value of a global variable, and you can specify a default value if the variable does not exist. Here's how you can correctly perform this check and initialization:Refreshing Global Variable
If you manipulate the
objectLocations
array within your script and want these changes to persist globally, you must update the global variable usingsetVar()
after making your modifications. There's no need to "refresh" the variable at the bottom of each script, but you do need to ensure you update the global variable whenever you change the array:Changing the Name and Value of a Specific Location
To change the name of
location_5
to "Advil" and itsy
value to 53, you'll first need to find the location within your array, then update its properties. Assuminglocation_5
is an identifier or part of the name property, here's how you might do it:Avoiding Duplicate Global Variables
As Dave mentioned, pressing the "Refresh" button in the Smart Variable Watcher skill can help manage the display of variables within that skill. To avoid creating duplicates in your scripts, always ensure you're using consistent variable names and updating existing variables rather than unintentionally creating new ones with similar names.
By following these guidelines, you should be able to manage and manipulate global variables and arrays in ARC effectively, ensuring your changes persist across script executions and sessions.
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Sometimes it's so obvious when you look back over it after taking a break on it. 3 for 3 for problem solving today-a good day.
@Athena Why would my script say Execution Error Line 13 Col 4 - Object has no method 'push' or this Execution Error Line 23 Col 4 - Object has no method 'find' It has been a little while since I ran the script but I thought it was working when I left it. I did reload the variables with the variable state saver.
The errors you're encountering, "Object has no method 'push'" and "Object has no method 'find'", suggest that the variable you're trying to use these methods on is not recognized as an array or does not have the expected properties/methods available. This can happen for a few reasons in the context of ARC and JavaScript scripting:
Variable Not an Array: The
push
method is specific to arrays in JavaScript. If you're getting an error stating thatpush
is not a method, it likely means that the variable you're trying to use it on has not been correctly initialized as an array. This can happen if the variable was overwritten with a non-array value or if it was not properly retrieved as an array from the global variables.Variable Not an Object with Expected Method: Similarly, the
find
method is used on arrays to search for elements. If you're getting an error aboutfind
not being a method, it suggests that the variable is not an array or the environment does not support this method.Reloading Variables: If you've reloaded variables using the Variable State Saver, ensure that the data structure (array or object) was correctly preserved during the save and load process. It's possible that the structure was flattened or changed, causing the methods to be unavailable.
To troubleshoot and resolve these issues, you can:
Ensure Initialization as Array: When you initialize or retrieve the variable, make sure it's explicitly set as an array. For example:
Check for Array Before Using Array Methods: Before using array-specific methods like
push
orfind
, check if the variable is indeed an array:Compatibility: Ensure that the scripting environment you're using supports the methods you're trying to use. While
push
should be universally supported in JavaScript environments,find
might not be available in very old JavaScript engines (though this is unlikely to be an issue in ARC).Update Global Variable Correctly: After modifying your array or object, remember to update the global variable with
setVar
to ensure the changes are saved globally:By following these steps, you should be able to identify why the methods
push
andfind
are not available for your variable and ensure that your script works as expected.I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
@Athena Can you write a multidimensional array that reads the servo positions of V1, V2, V3 at 1 second intervals for 120 seconds and puts it into an automated named Location 1, Location 2, Location 3, etc (which I will change later). Each Location will have xpos, yPos, zPos. Put the array in a nice orderly spreadsheet format with space between each xpos, yPos, zPos. The array is to have a replay on the log screen again at 1 second intervals if ADC 3 is switched to high and replay in reverse if ADC 4 is switched high. The replay will make V1, V2, V3 move to their locations if ADC 5 is high.
Give multiple examples of it working.