Make a Switch to change code flow

Hi Dynamo Lovers all over the EARTH !

I’m trying to make a switch based on user input (0 or 1) to execute two different ‘Data.ExportToExcel’ nodes but can’t get the job done, any help ?!?

Attached here is a photo of the first export node with the switch input

@Ahmed_Ramy
Have a look here True/False Gate or passthrough? - #12 by rnarracci

@Kai anks Kai for help ! But I don’t understand how it actually works. So if you can interpret it for me, I will be grateful.

1 Like

Hi Ahmed,

Visual programming tools like Dynamo or Grasshopper are designed with premade basic nodes/components that allow you to do stuff easily…like, for example, an “Addition” node lets you plug two numbers into it and adds them for you. Most visual programming tools also allow you to do more complex things using various programming languages (Python, C#, Ruby, etc…) typically typed up within a blank node that they provide for coding. In the case of Dynamo, the “CodeBlock” node is simply a vessel within which you can write out something more complex (or, in this case, simple) with DesignScript programming language developed by Autodesk. DesignScript IS the programming language of Dynamo…so its baked in.

If you find my switch expression confusing, you’re not alone; I do also because I’m not a programmer and why it works is a mystery to me. In this case, the question mark just accepts the boolean toggle and switches the output from a to b.

If you want to learn more, you can read about real life examples of using the code block at the Dynamo Primer (link below).
https://primer.dynamobim.org/07_Code-Block/7_Code-Blocks-and-Design-Script.html

There lots of detailed (programmer-speak) info about DesignScript language in this document (link below).

2 Likes

@rnarracci Thanks for replying !

@rnarracci I’ve coded a complete applciaiton with Unity 3D bolt visual scripting tool. In bolt you can control the code flow direction where here in dynamo it 's still very confusing to me.

1 Like

@rnarracci I am wondering if I can accomplish what I need with functions ? may be you can advise me from your experience ?

Functions | The Dynamo Primer (dynamobim.org)

I think it would be easier to think of this as switching the inputs that go into a single Data.ExportToExcel node instead of branching off to a duplicate of the same node. I’m assuming you’re only trying to have the user control one of the inputs, but not all 7 of them?

2 Likes

This is DesignScript shorthand for writing a basic if/else statement. In this specific case, the Code Block has three variables defined (x, a, and b). It says:

x ? a : b

The equivalent if you were to write a full if/else statement would be:

if (x)
    a
else
    b

In the example, it’s a pretty basic check because the variable ‘x’ is a boolean (true/false node). But you can put whatever you need in that first part (called the conditional). Maybe you’d want to check if ‘x’ is a certain number, like this:

x == 5 ? “x is equal to 5” : “x is not equal to 5”

3 Likes

I think you’re probably already more knowledgeable that me on the subject! Not sure what you mean by flow direction but I have noticed that GH and DYN are averse to recursiveness and one has to take care about avoiding feedback loops that blow your stuff up. Good luck.

1 Like

@zachri.jensen Thanks for help ! Now I got it .