Hello, Dynamo Forum! I’ve been using Dynamo for a while now for Revit (mainly for exploring its capabilities in modelling and organizing data). Recently, I’ve made the step to try coding within Dynamo to study automating workflows within Revit. However, I’m new to DesignScript, so I don’t understand some things, even when reading excerpts from the DesignScript guide over and over again.
In the image above, I’ve tried making a program that uses a function to list ordinal numbers, given a range of numbers. I made it work, but I’m not sure I understand it completely. For example:
Why do I need to assign a variable “d = floor” for the program to work? Does it matter whether I make this definition in the associative block of the function or inside the imperative block?
What’s the significance of the line “return [Imperative]”? I’ve seen other topics coding “var = [Imperative]”. Is that also correct, and what function does that have? I only know that imperative blocks are needed to add conditional statements into code blocks in DesignScript.
Lastly, is there a way to write this program better? Like, I’ve tried using “if(num==[11,12,13])” for line 8, but it doesn’t work. I suppose you can’t use lists inside conditionals? What else can you use and not use inside conditional statements? Can this program be written better using a “for” statement?
I know I have a lot of questions, so I apologize if I may take too much of your time. I really want to learn more about DesignScript, but the DS confuses me more often. Please answer only what you can, and I’ll close the topic when I have sufficient answers. Thank you!
I am just getting into DesignScript, so I really can’t answer any of your questions. But if you are looking to consolidate your formula down into a simplified manor, you could use below. Consolidating the first three lines into one, forces the code block to return only the first three numbers, so that’s why they are separated out. I do like the approach of defining a function as you did.
May I know what syntax this is called (I hope I’m using the right term hahaha)? I’m sure it can be useful, but I do agree with your sentiment of using a function.
Also, I find DesignScript to be less straightforward and less forgiving (compared to languages I’ve encountered before), especially in Dynamo’s interface. I’m sure we’ll be good at it soon if we keep on practicing.
What I showed was just a inline way of using the “if” “elseif” and “else” statements. So like the if statement it’s (test ? value-if-true : value-if-false).
If that’s not the answer you are asking for, please clarify.
You can’t utilize the variable ‘floor’ in the imperative statement until it’s been defined. You can define them in the associative portion of the code though.
The reserved keyword return is to indicate to Dynamo what the result of the definition or imperative statement should be. In this case you’re telling Dynamo to return the result of the statement. Oftentimes it’s best to define that as a variable rather than just returning the result, such as 'suffix = [Imperative]`. This allows reuse of the variable elsewhere in the code.
Perhaps; the real answer depends on what your definition of better is.
If I had to go imperative here, I would define the suffix in the imperative statement, then merge it with the number outside of the statement.
But personally i would try avoiding the use of imperative code entirely as it’s harder for beginners users to pick up/edit/maintain/revise/troubleshoot.
@staylor, I see! Thank you for the follow-up. And yes, that’s the answer that I was looking for.
@jacob.small thanks, you legend! I now understand the topic a lot better. I’ll definitely try using the “Associative only” approach. Also, thank you for showing me how to incorporate Dynamo nodes in conditional statements instead of Boolean operators only. Lastly, was it that obvious that I wanted to make floor names? Hahahaha, thank you!
You guys have been amazing. I’ll close the topic soon. Thank you again!