Node Copied to Clipboard!
div

How to use Custom .NET DLL Assemblies in the Storyboarder


In this tutorial you will learn how to load a custom DLL into Modest Tree Suite and directly execute code in this assembly from the Storyboarder. We will focus on loading .NET DLL's first (for C / C++ DLL's see the article "How to use custom C/C++ DLL assemblies in the Storyboarder")

** Note: The full source code for this lesson, including the Modest Tree Suite project can be found in the Samples project on modest3d.com. The project is named "Simulator DLL Loading" and the source for the C# is included as a zip file in the project**

Step 1: Create your custom .NET DLL.

Create a new C# project and add a single file named CarSimulator.cs, with the following contents: Note that the .NET Framework version for your project should be set to a maximum of 3.5.

using System;

namespace SimExample1
{
    public static class CarControls
    {
        public static bool IsAcceleratorDown;
        public static bool IsBrakeDown;
    }

    public static class CarSimulator
    {
        const float AcceleratorSpeed = 10;
        const float BrakeSpeed = 0.8f;

        static float _speed = 0;
        static float _position = 0;

        public static float GetSpeed()
        {
            return _speed;
        }

        public static float GetPosition()
        {
            return _position;
        }

        public static void Update(float deltaTime)
        {
            _position += _speed * deltaTime;

            if (CarControls.IsAcceleratorDown)
            {
                _speed += AcceleratorSpeed * deltaTime;
            }

            if (CarControls.IsBrakeDown)
            {
                _speed *= BrakeSpeed;
            }
        }
    }
}

Step 2: Create a new project

If using Modest Tree Suite cloud, log in to modest3d.com then click the "Projects" page on the left. Click the New button, and enter details for your project. Then open the Modest Tree Suite Cloud app, login with your username and password, then select the project that you just created.

If using the standalone offline version of Modest Tree Suite, create a new directory somewhere on your hard drive that will contain the assets for our project. Then open Modest Tree Suite, select the Local tab, then select your new directory.

Step 3: Add your DLL to your project

Build your DLL.

In Modest Tree Suite, after selecting your project, click the Add button in the Assets tab and select your DLL to add it to your project.

If using the standalone offline version of Modest Tree Suite, you can also just copy the DLL to your project folder directly.

Step 4: Restart Modest Tree Suite

In order for Modest Tree Suite to pick up the change to the DLL, you need to fully restart Modest Tree Suite.

Step 5: Start a new lesson

Open your project in Modest Tree Suite again, then click the New button on the left to create a new lesson.

Step 6: Import the classes used in your DLL to your project.

Create a new file in a text editor such as Notepad with the following contents:

import CarSimulator from Sim1.CarSimulator;
import CarControls from Sim1.CarControls;

Save the file to a temporary location on your hard drive.

In Modest3d, click on the Assets tab then click the Add button. Select the "Sim1Imports.teak" file you just created.

If using Modest Tree Suite Offline Standalone, you can also just save this file directly to your project folder.

These lines will tell Modest Tree Suite what name to use to refer to your C# classes. Note that the name of this file does not matter, however it must have the extension ".TEAK".

Step 7: Hit F10.

This will cause Modest Tree Suite to read your newly added .TEAK file without needing to restart. Any time you make a change to a .TEAK file, you need to either restart Modest Tree Suite or simply hit F10 to reload the new changes.

Step 8: Test nodes

Your classes should now be imported and ready for use in the Storyboarder. If you right-click in the Storyboarder, then type "Car" you should see our two classes "CarSimulator" and "CarControls" listed as suggestions.

Note that you have to be in Programmer mode otherwise these classes will be hidden. You can change the difficulty setting at any time by clicking "Settings" in the left pane, then typing the letters "1337". The option to select Programmer mode should then appear.

Step 9: Import test model.

Import a model into your lesson using the initial setup node. You can do this by adding or importing a model to the assets pane then dragging the model to the Setup node tab.

Step 10: Create the following lesson

Note that here we assume the model you loaded is named "Car"

Step 11: Run the lesson.

Press the Play button in the left pane. Then click on the Player tab so that it is is Yellow. This means that it is capturing input. Finally, you can now press up and down arrows to cause your model to go forward and stop.

Step 12 (optional): Clean up

Clean up your lesson a bit by using templates

Step 13 (optional): Reload your DLL after making changes

Add a new method to the Car Simulator class inside your C# project. Rebuild it and copy the new DLL to your project. Note that at this point, if we try to select our new method in the Storyboarder, it will not appear. This is because we need to tell Modest Tree Suite to reload the assemblies. You do this by pressing F11. After pressing F11, you should see your new method appear. By making changes on the C# side, rebuilding the DLL, going back to Modest Tree Suite, and then hitting F11, you can very quickly iterate on your C# code, without needing to restart Modest Tree Suite.

Step 14 (optional): Debug your DLL in Visual Studio or MonoDevelop

See this article for more info on doing this.

Notes about this feature

  • This currently only works when used with the desktop version of Modest Tree Suite. We are in the process of investigating supporting this in the embedded web version as well.

Next How to use custom DLL assemblies in the StoryBoarder