Advanced Tutorial 1: Endless Scripting Basics

* Familiar with the Editor in The Endless Mission
* Familiar with the concepts of variables and functions in programming
* Working in an empty scene or a scene with a backup saved

Getting to the Code1. First, drag the object "TimerLose" into your scene.
2. Locate the "Timer" script on the object and click the settings wheel. This will open a window containing the code for that script.

From this code window you can modify the behavior of this script any way you like using the C# coding language. On top of the basic C#, The Endless Mission has several functions to help make the process of game making that much easier. In this tutorial, you're going to learn about Serializing Fields, creating an EndlessEvent, and making a function EndlessVisible. Each of these concepts will be explained generally, and then through a continous example using the "Timer".

Making Your Variable Appear in the Inspector
If you're familiar with Unity, you know that adding the prefix "public" in front of a variable will make the variable appear in the inspector component of that script. To accomplish this in The Endless Mission, we will use the tag "[SerializeField]".
* Note: Serialized Fields exist in Unity and their function is nearly the same in The Endless Mission, if you are already familiar with them you can skip this section.

Basics

Example
1. Open the "Timer" script on your "TimerLose" object.
2. Create a private float variable called TimeTrigger by pasting the following above "[Hackable]".

```
   [SerializeField]
   private float TimeTrigger = 0f;

```

3. The "[SerializeField]" above the variable declaration allows this variable to be changed from the inspector.
4. Save the script.
Now in the inspector you should see a new field for that variable.

Creating an EndlessEvent
An EndlessEvent allows you to call a function from any script you want. By using an EndlessEvent you are able to streamline the process of calling a function on another object using whatever logic you want.

Basics
1. In a Serialized Field, create the EndlessEvent by declaring a private variable of the type "EndlessEvent".
2. Set this variable equal to "new EndlessEvent()".
A) This creates an inspector field for the event allowing you to drag in objects and call functions from them.
3. To call the event use "myEndlessEvent'sName.Invoke()".

Example
1. In your "Timer" script declare a variable of the type EndlessEvent. (If you do not know where or how to declare a variable please visit: Unity Variables and Functions .) For now you can paste the following code above "[Hackable]" in your "Timer" script.

```
   [SerializeField]
   private EndlessEvent myEndlessEvent = new EndlessEvent();

```

2. Make some logic that checks to see if the current time remaining is equal to the TimeTrigger variable from the above section.
A) declare a boolean variable that will ensure out function only calls once.
1. Under your EndlessEvent variable paste:

```
private bool hasFired = false;

```

B) In the PlayerUpdate() function add the following code:

```
   if (SecondsRemaining == TimeTrigger && hasFired == false)
   {
         myEndlessEvent.Invoke();
         hasFired = true;
   }

```

This code will call your EndlessEvent when the timer is equal to "TimeTrigger" (You can change TimeTrigger's value in the Inspector). Then it will change "hasFired" to true to make sure that your function only calls once. To make sure that everything is set up properly, have a look at the reference picture provided.

For more information on these topics visit: Boolean Variables, If Statements.

Making a function visible to EndlessEvents
Now that you have an EndlessEvent, you may be wondering how to make a custom function to call from it. When you click the plus to add a new event you get a field for your "Target" object. You can drag any object from your hierarchy into this box. The drop down menu will then show every function from every script on that object as long the function has an "[EndlessEventVisible]" Tag.

Basics
1. On any function, either yours or one that comes with The Endless Mission, add the tag "[EndlessEventVisible]" one line above the function. It should look like this:

```
    [EndlessEventVisible] "<--- This tag makes the function appear in the event drop down menu"
    public function aRandomFunction()
    {
        //Code that does something
    }

```

2. Now go to an EndlessEvent and click the plus button to add an event.
3. Drag whatever object has the script that has your function into the "Target" box on the event.
4. You should see your function in the drop down menu. That function will now call when that event fires.

**NOTE:** Endless Events currently can only use these parameters:
int
float
bool
string
Anything else will prevent the event from showing up in the drop down list.

Example
This example assumes you have followed the examples in the above sections.

1. Drag any physical object into your scene (a wall or cube, etc.).
2. Make a new script called "objectDisappear()" with the [EndlessEventVisible] tag above it:

```
   Start()
   {
   }

   Update()
   {
   }

   [EndlessEventVisible]
   public void objectDisappear()
   {
       transform.gameObject.SetActive(false); //This code will make the object disappear when objectDisappear is called
   }

```

3. So now we need to call this function from our EndlessEvent that we made in the previous tutorial.
4. On your TimerLose object, click the plus button on your myEndlessEvent to add an event and drag your new object into the "Target" field.
5. From the dropdown menu select "objectDisappear".
Now when your event is called, that object will disappear.

To recap, you have created a variable TimeTrigger that appears in the inspector. You created an EndlessEvent that is called when the timer's current time is equal to the time you set in TimeTrigger. When this event is called you will execute the function from the object you created dragged into the "Target" field.

* **Tutorials & Guides**