Creating a FS Script Setup - First Steps

Posted by Thursday, July 14, 2016 12:45:00 AM Categories: basics c# Easy Freedom Script Unity Windows

The Interpreter Object

Rate this Content 1 Votes

The Interpreter Object

We are going to simply create a gamemanager and use it to print to the Unity console. A gamemanger is a  non-visible element that could control objects in a  unity scene. Cameras, lights, scores or heath levels are examples of objects a gamemanger could control. A level setup script is another example of a function of a gamemanger and a use for an external script.

We will setup Freedom Script (FS) in unity scene. In this project it will only load a FS script and not run it. We need to make some choices on how to run the script, but we will get to that later.  We will build on this setup to a fully functioning FS instance.  Baby steps first.

  • Create a new empty unity scene.
  • Create a scripts folder under assets
  • Drag and drop the interp.dll into the scripts folder
  • Create an empty c# script in the same folder and name it SimpleSubroutine

Ok, now that all that basic unity stuff is set up, let’s get to work inside the unity scene. Right click in the Hierarchy window and create an empty game object, rename it to GameManager. Now drag the SimpleSubroutine script you created before onto the gamemanger. This attaches the script to this in game object. Notice the class name will match the script file name.

Double click on the SimpleSubroutine ,  script.  Clicking on the name opens the script up in monodevelop or visual studio. (I use visual studio)

Now we need to add the using statements to our C# script. These statments allow us to access the interp.dll or other dll(s) from our code. We always need the interp object to use FS and in this project we also need system IO also. We use this later to get the path to our data directory. You may use different methods at different times.  Let’s add them to our script.  Simple enough:

using System.IO;
using Interp;

 

we need a variable to hold our FS object. Let’s create one in our class. If we declare the variable at the class level it will stay in scope as long as our gamemanger is in scope. Notice we derive this class from MonoBehavior. This is not required but very useful.   

public class SimpleSubroutine : MonoBehaviour
{
Interpreter interp ;
}

 

There are many built in Unity functions that we have access to now.  I want to use the Awake() function.  This function is called by unity when it creates the gamemanager object. It is only called once. This is a perfect place to create the Interpreter Object for FS.

void Awake ()
       {
        interp = new Interpreter();
       }

 

Now that was simple.  In my project I created a folder called Freedom Demo and a sub folder named Freedom Script Demo. I plan on installing and keeping my FS scripts in this folder. So we need  a way for unity to find this folder. We don’t know where the user installed your program but unity does. We use the datapath function in unity to find the asset folder then we tack on the other path elements to create a string holding the file path named '_filename'. There are many ways to do this or even to hard code the path in some cases but the following code is a simple way.

Before we code the load function, lets create a simple text file to hold some FS script.

Using any text editor create a file named SimpleSubrouteDemo.txt

In the newly created text file add these lines.

Type:

name = "Billy"

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

Then save the file and add it to that folder. (You can drag it into unity onto that folder) My folder structure looks like this:

Folder setup

 

If you want to use a different folder structure or file name, feel  free but then you will need to adjust  the path in the following code.

 

 

 

Now our script looks like this:

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));
}

 

Now as you can see we created the FS interpreter object and constructed and passed a string containing the full path to the file containing the FS script. Using C# we created a streamreader object and passed it to the FS interpreter load function. This loads a text file named SimpleSubroutineDemo.txt.  

If you did it correct it should compile fine. Save your c# code and click back to unity. If you have no errors we are ready to move forward. I added a simple unity print statement at the end of our code to show that the awake() function did trigger once.

       print("We are printing this from regular c# scripting inside unity");

If you have no errors run the program and look at the console window. You should see your message. If you received an error it most likely is in the file path you created.

Add this code to help track down the error.

print("The Freedom Script Named: " + _filename + " has been loaded.");

 

Let us pause here. The code we wrote does not do much yet, but it should give you a good foundation on how create the interpreter object. It really just boils down to two lines of real code.

        interp = new Interpreter();
        interp.Load(new StreamReader(_filename));

Review the code to make sure you understand what we did. Get a cup of coffee and get ready to meet the Subroutine object in the next lesson.

Without comments added this is what my script looks like.

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/"; // this gets the current data directory

_filename += "SimpleSubroutineDemot.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.");
  }
}
2016

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