In Dynamo code block, can i return a variable input by Functions

Hey everyone…

I am trying to define a function in this function i want to return an input x.
plz see the attached picture

def selectedX(x){
return = x }

Michae, thank for replying, this will need the input every time i have to use the function. what i’m looking for is storing this variable in the function…

X is a variable in your function - you need to set it to be a real thing. See these examples:

In your case you’d need to change the code to be:

def selectedX()
{
return = 15432;
};
1 Like

I have not tested, it might be possible. But I don’t think you’ve described exactly what you want yet. Do you want to form a closure around some other variable? Do you want to set the variable the first time and then if it’s already set just use the existing value inside the function?

Seems like what you’re after is something along the lines of a class instance’s fields and properties that you can read from and write to. Design script functions don’t have any such “memory” however. Implementing such functionality and making it update dynamically is easier said than done. There’s a great package that can already do that, tho it hasn’t been updated in a while:

With a bit of creative thinking, you can make a “lowcost” version that uses files as buffers and the dynamic update functionality of the “File.FromPath” node, but you’ll also have to handle the serialization and deserialization of the data yourself:

2 Likes

@Dimitar_Venkov - what would you recommend to ensure that the write text always happens before the read text? I just built a test version which happens in the opposite order, resulting in a failure to read the right variable on run. It’s unlikely that it’d happen in a linear progression of graph development (you’d have written the variable before reading it), but something to keep in mind as I can see backtracking to re-work the nodes creating an issue.

i want to store the input in the function so whenever i call the function it will contain my variable input automatically without the need to connect it again.
in the last few examples you have shared i need to connect that variable everytime i want to use my function.
also i need to use a level inside my function, but getting the level in the code block is not possible as far as i know.

Your last node is failing as you defined v as an input for the var1 function, but didn’t give any inputs when you called var1.

Refreshing the graph in it’s entirety should allow you to call and recall the variable without issue, either though @Dimitar_Venkov’s method or mine (just be sure you write the code to write the file before you write the node which calls it). Springs.UI package has a node for this type of refresh.

If you want to narrow down the number of nodes which refresh, You could also code a refresh function and nest it in as an unused function. This would allow you to refresh the input for every one of your custom functions by changing one value in one code block.

I was wondering if there is a new alternative to what @Dimitar_Venkov shared (Prorubim package or File.FromPath). At the moment I’m trying to clean up my graph so I’m just looking for a way to have some “hidden wires” as in Grasshopper. I’m using functions to have most of my global parameters in one place. This works “fine-ish” for most variable types. But this solution comes short when I need to input materials for instance, which requires the drop-down list.
Thanks! :slight_smile: