» dynamic plugin tutorials
This site relies heavily on Javascript. You should enable it if you want the full experience. Learn more.

dynamic plugin tutorials

under construction

Introduction

You already are familiar with what a dynamic plugin is. We now examine how to code some simple dynamic plugins in c#.

Beginner

#1 - Adding an If - Statement

A very common example is a node that handles some logic or that can be configured or just can be disabled.

Let's say we want to write a node that randomly increases/decreases its output value, which could be used to simulate the movement of an insect. At any time we want to be able to put it to sleep. And we want to be able to change the vividness.

So please clone with me the Template (Value) and give the name "RandomWalker" or something like that.

The Random Behaviour

Behind

FOutput.SliceCount = SpreadMax;

write the folowing:

var r = new Random();

This creates a random generator and assignes it to a local variable which we just called "r".
If you later on write "r." you see which methods can be called on the random generator. Use the keyboard arrow keys and move over "r.NextDouble".
In the tool tip you can see that r.NextDouble will generate values between 0.0 and 1.0.
Now change the line within the for-loop to:

FOutput[i] = (r.NextDouble()-0.5)*FInput[i];

In the tool tip we saw that r.NextDouble will generate values between 0.0 and 1.0. Subtracting by 0.5 will result in a value around 0. So 50% it will be negative (walk left), 50% it will be positive (walk right). Multiplying with the input gives the ability to scale the random walking at any time. But still this is only the walking velocity. The position doesn't change yet.
Now change the "=" in that line to to "+=", which means that the value of the last frame is changed by the random amount. Now it walks off...

Don't forget to use CTRL-S to save and compile.

Delete the "logger" stuff by hitting CTRL-D. You deleted the wrong line? Undo (CTRL-Z) is your friend.
Your code should now look like this:

public void Evaluate(int SpreadMax)
{
    FOutput.SliceCount = SpreadMax;
 
    var r = new Random();
 
    for (int i = 0; i < SpreadMax; i++)
        FOutput[i] += (r.NextDouble()-0.5)*FInput[i];    
}

Sleep

Create a new Input by opening the region "fields & pins" and adding the following line behind the first input declaration:

[Input("Enabled", DefaultValue = 1.0, IsSingle = true)]
ISpread<bool> FEnabled;

After hitting CTRL-S you will see the new pin.
Note that it is

  • called "Enabled"
  • accepts 0 or 1 (because it is a boolean)
  • it true (1.0) by default
  • and is not spreadable (IsSingle = true).

Add

if (FEnabled[0])

before your for-loop and the for loop will only be evaluated if "Enabled" is true.
Please always indent your code by using TABs to make the structure of the code more visible.

if (FEnabled[0])
    for (int i = 0; i < SpreadMax; i++)
        FOutput[i] += (r.NextDouble()-0.5)*FInput[i];

Note that the above is a shorthand for writing:

if (FEnabled[0])
{
    for (int i = 0; i < SpreadMax; i++)
    {
        FOutput[i] += (r.NextDouble()-0.5)*FInput[i];
    }
}

As soon as you want to do more in your loop or more is dependant on if Enabled = true, you will need the code blocks anyway.

Ideas for more tutorials

  1. Dealing with several Input Spreads
  2. Dealing with several Outputs
  3. Working with Strings
  4. Working with Colors
  5. Working with Enumerations
  6. How to write readable Code (1)
  7. How to share a Dynamic Plugin
  8. Working with Spreads of Spreads
  9. Working with your own Data Type
  10. Working with Transformations
  11. Working with External Libraries

anonymous user login

Shoutbox

~4h ago

joreg: vvvvTv S02E01 is out: Buttons & Sliders with Dear ImGui: https://www.youtube.com/live/PuuTilbqd9w

~7d ago

joreg: vvvvTv S02E00 is out: Sensors & Servos with Arduino: https://visualprogramming.net/blog/2024/vvvvtv-is-back-with-season-2/

~7d ago

fleg: hey there! What's the best tool for remote work? Teamviewer feels terrible. Thanks!

~21d ago

joreg: Last call: 6-session vvvv beginner course starting November 4: https://thenodeinstitute.org/courses/ws24-5-vvvv-beginners-part-i/

~1mth ago

joreg: Missed the last meetup? You can rewatch it here: https://www.youtube.com/live/MdvTa58uxB0?si=Fwi-9hHoCmo794Ag

~1mth ago

theurbankind: When is the next big event, like node festival ?

~1mth ago

~1mth ago

joreg: Join us for the next vvvv meetup on Oktober 17th: https://visualprogramming.net/blog/2024/25.-vvvv-worldwide-meetup/

~1mth ago

joreg: 6 session beginner course part 2 "Deep Dive" starts January 13th: https://thenodeinstitute.org/courses/ws24-5-vvvv-beginners-part-ii/