Wrapping up our first script setup

Posted by Friday, July 15, 2016 3:41:00 PM Categories: basics c# Easy Freedom Script Unity Windows
Rate this Content 1 Votes

Wrapping up our first script

I have done a lot of typing in this series to explain the basics of Freedom Script but our projects needs to be wrapped up. If you recall our goal was to create a print function in our FS script that would print to the unity console.  We have set up our FS interpreter, created a Subroutine Object that defines the function in our FS script and created the unity function that will perform the work.

Let’s review the project without all the background information.

Our FS script file should look similar to this. It is just a regular text file. (FS script is not case sensitive)  My file location was "Assets/Freedom Demo/Freedom Script Demo/”  Make your location where ever you want but then you need to reflect that in the c# code.

FS Script:

'Simple Subroutine Freedom Script
name = "Billy"

print("My name is " & name)
name = "Felicia"
print("Now my name is " & name)

 

We created an empty unity game object named “GameManager”  and attached a c# script. In our example we called the script  “SimpleSubroutine”.  In this script we created a simple function that accepts a string and prints it to the unity console. We named it FreedomPrint().

We used the Awake function of the script to create and store an FS Interpreter object. Then we pieced together the filename of our FS script. We loaded the FS script using the Interpreter’s  Load function.

Let’s  review the C# code up to this point.

public class SimpleSubroutine : MonoBehaviour {
    Interpreter interp  ;
       void Awake ()
    {
        interp = new Interpreter();
        string _filename = null;
        _filename = UnityEngine.Application.dataPath + "/Freedom Demo/Freedom Script Demo/";
        _filename += "SimpleSubroutineDemo.txt";
 
        interp.Load(new StreamReader(_filename));
     
        print("We are printing this from regular c# scripting inside unity ");
        print("The Freedom Script Named: " + _filename + " has been loaded.");
 
public void FreedomPrint(string vtext)
    {
        print(vtext);      
    }

      ...

To create the link from the FS script, the keyword print, and the unity based function we want to call ,FreedomPrint(), when our FS script see the keyword (“print”) we created a Subroutine Object.  We define that it will accept one parameter which is the string we want to print. Then using a reference to the SimpleSubroutine Class (attached to the gamemanager game object)  we call the FreedomPrint() function passing it the string from the FS script.

Here is the complete Subroutine listing: (without comments)

 

public class PrintFromScriptSubroutine : Subroutine
    {
    private SimpleSubroutine _parent = null;  
    public PrintFromScriptSubroutine(SimpleSubroutine vparent)
        {
            _parent = vparent;
            num_parameters = 1;
        }
    public override Value Execute(Value[] args, VarCollection vars)
        {
            string whatToprint = args[0].content.ToString();
            _parent.FreedomPrint(whatToprint);
            return args[0];
        }
    }

 

Back to our unity script class, we registered our subroutine with the interpreter Object and named it for our FS script.  We used “print”. We put some test unity print statements to demonstrate what and when these are called. Then we ran the unity project thus running the FS script.

Here is the complete listing for the SimpleSubroutine class we attachted to our unity gamemanager:

using UnityEngine;
using System.Collections;
using System.IO;
using Interp;
 
public class SimpleSubroutine : MonoBehaviour {
 
    Interpreter interp  ;
 
       void Awake ()
    {
        interp = new Interpreter();
        string _filename = null;
        _filename = UnityEngine.Application.dataPath + "/Freedom Demo/Freedom Script Demo/";
        _filename += "SimpleSubroutineDemo.txt";
     
        interp.Load(new StreamReader(_filename));
 
        print("We are printing this from regular c# scripting inside unity ");
        print("The Freedom Script Named: " + _filename + " has been loaded.");
    
        interp.RegisterSubroutine("print", new PrintFromScriptSubroutine(this));
 
        interp.Run(StepByStep.NO);
        print("Run Once");
 
    public void FreedomPrint(string vtext)
    {
        print(vtext);
       
    }
 
}

 

After we run our unity project it will print the defined messages from unity and from FS script in the console window. Since we ran this from the Awake() function, it should only run once and the FS script should run all at once.

We will talk about different ways to run FS scripts such as line by line and error checking in another post.

 

Bill R

 

2016

Site Map | Printable View | © 2008 - 2024 GoodCarma.net | GoodCarma.net | Community Tools since 2008 | SmithTown New York