Latest  | Search | Go
Edit this page   |   Attach file 

  Home | Tutorials | Technical Reference | Runtime | API Documentation | ContentCreation > ScriptWriting  

Script Writing

Scripting System features

  • Scripts can be written on C# or VB.Net.
  • Full integration with .NET scripting languages, allowing programmers and artists to write fully debuggable, IDE-integrated code in your favourite language, from C#, C++/CLI, to VB.NET without the need for a compiler
  • Cross-platform support through Mono Compiler
  • You can reference scripts from each other, for example using classes written on one script from another.

Scripting System guidelines

These are some guidelines for writing your scripts:

  • You can't add any references to the script except the pre-added references which are ScriptingSystem.dll and System.dll.
  • All the scripts must be located in the Scripts folder.
  • Any script must have a class with the name of the script file and inherits the GameScript class
  • Keep all your classes public variables as properties so they can be accessed through Reality Builder.
  • All the engine classes that are accessible from scripts have an 'M' letter as a prefix (so actor will be MActor) which stands for managed.
  • Assigning two vectors or matrices to each others is a referencing, but to copy the actual data use the Copy function found on both of the matrix and the vector class.
  • Don't Add namespaces.

Step by Step tutorial

Now I'll take you step by step to write your script, we will write two scripts for moving a simple model, the first script will move the object on the Y axis and the other one will move it on the X.

To use Visual Studio's intellisense

  1. Start Visual Studio .Net and create a new C# class library project on your bin directory with the name "scripts".
  2. Add a reference to the ScriptingSystem.dll found on the system folder in your project's bin directory after building the engine.

Class1.cs

And Now modify your class code to this:

using ScriptingSystem;
using System;


// Main script class, which must inherit from GameScript and have the same name of the script.
public class Class1 : GameScript
{
    //The entry point of the script, You can add any intialization code here
    public void Init(MWorld world, MActor actor, MMatrix transform)
    {
        
    }

    //This function will be called each frame for any updating code
    public void Tick()
    { }
}


//This our simple actor class which will be inherited from the other script
public class MyActor : MActor
{
    //The actor class constructor needs a world
    public MyActor(MWorld world) : base(world)
    {
   //Create a new instance for the actor model
        MyModel = new MModel();
   //Load box.xml to it
        MyModel.Load("Box.xml", false);
    }

    //This function will be called each frame for any updating code
    public override void Tick()
    {
   //Change the actor location according to the Sin wave on the Y axis
        Location.y = (float)Math.Sin(Environment.TickCount) * 5;
    }
}

Class2.cs

Add another class to your project, name the file class2.cs and write this code into it:

using ScriptingSystem;
using System;

//Use code from Class1.cs
using Class1.cs;

//Our derived actor from MyActor
public class MyActor1 : MyActor
{
    public MyActor1(MWorld world) : base(world)
    {
    }

    public override void Tick()
    {
   //Move the actor on the X axis here
        Location.x = (float)Math.Sin(Environment.TickCount) * 10;
        base.Tick();
    }
}

//Our Main class, note that it has the same name as the script.
public class Class2 : GameScript
{
    //MyActor1 member
    MyActor1 testActor;

    public void Init(MWorld world, MActor actor, MMatrix transform)
    {
   //Intialize a new instance from MyActor1
        testActor = new MyActor1(world);
    }

    public void Tick()
    { }
}

To instantiate your script in the World

If hard-coding in the application, use World::CreateScriptActor(string ClassName), where the script's class name is the same as the ClassName.cs file. To add to the scene via Reality Builder for saving, use the drop-down "Insert Actors" list. The list automatically contains all scripts located within the scripts subdirectory.

ScriptWriting   Edit | Attach | Ref-By | Printable | Diffs | r1.5 | > | r1.4 | > | r1.3 | More