Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
setVariable adds variables out of order

I have a simple script that ought to create variables in sequence. I am using it in a new parts studio without anything else.
My expectation is that the variables should be added (unless something else precludes this) in the order requested.
FeatureScript 2716; import(path : "onshape/std/geometry.fs", version : "2716.0"); export function fretOffsetFromBridge( scale_length is ValueWithUnits, fret_number is number ) returns ValueWithUnits { return scale_length / ( 2 ^ ( fret_number / 12 ) ); } export function fretOffsetFromHead( scale_length is ValueWithUnits, fret_number is number ) returns ValueWithUnits { return scale_length - fretOffsetFromBridge( scale_length, fret_number ); } annotation { "Feature Type Name" : "Calculate Fret Positions for Scale Length", "Feature Type Description" : "" } export const calculateLayoutForScaleLength = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Scale Length" } isLength( definition.scaleLength, { (inch) : [1, 24.75, 100] } as LengthBoundSpec ); annotation { "Name" : "Number of Frets" } isInteger( definition.numberOfFrets, { (unitless) : [ 0, 24, 48 ] } as IntegerBoundSpec ); annotation { "Name" : "Scale Length Variable Name", "Default": "scaleLength" } definition.scaleLengthVariableName is string; annotation { "Name" : "Fret Offset from Head Variable Name Prefix", "Default": "fretOffsetFromHead" } definition.fretOffsetFromHeadVariableNamePrefix is string; annotation { "Name" : "Fret Offset from Bridge Variable Name Prefix", "Default": "fretOffsetFromBridge" } definition.fretOffsetFromBridgeVariableNamePrefix is string; } { setVariable(context, definition.scaleLengthVariableName, definition.scaleLength); for ( var fret_number = 1 ; fret_number < definition.numberOfFrets ; fret_number += 1 ) { var this_var_name = definition.fretOffsetFromBridgeVariableNamePrefix ~ "_" ~ fret_number; var this_offset = fretOffsetFromBridge( definition.scaleLength, fret_number ); setVariable( context, this_var_name, this_offset ); } for ( var fret_number = 1 ; fret_number < definition.numberOfFrets ; fret_number += 1 ) { var this_var_name = definition.fretOffsetFromHeadVariableNamePrefix ~ "_" ~ fret_number; var this_offset = fretOffsetFromHead( definition.scaleLength, fret_number ); setVariable( context, this_var_name, this_offset ); } });
Instead, I am getting this:
Why am I not getting the result expected, meaning the variables in the order they were set?
Tagged:
0
Comments
These things are stored in hash structures, so contiguous rows coming from the same custom feature will be added in (probably deterministic but) random order. We could record the order within a custom feature; your ask sounds reasonable. Feel free to file an improvement request.
Meanwhile, you could use a simple custom table (https://cad.onshape.com/FsDoc/tables.html) that sorts the variables, say, lexicographically. Thusly: https://cad.onshape.com/documents/49636ae30e6a72d0b149428a/w/2379b34a4e07faeb937f25a6/e/d0491baba54e0a2b6d0eb62f
I appreciate your answer, but I seem to be missing something.
When I use your code in place of mine, the table code never seems to be called. Debug / println output never occurs, and the list doesn't appear to be sorted.
Any suggestion?
Ah! I see. They are added in a custom table in addition to the variable table.
Thanks for the solution and the explanation!