Tutorial: Creating Area Triggers

New Toolset-based Trigger Area Sample


Please checkout the "Lost Isle 3" scene included with Visual3D v9.5 (Beta 5) release (when available), where you can see an example of a Trigger Area placed in the scene and setup using the toolset, instead of via C# code.

Older Tutorial on Creating Trigger Areas via C# Code

 

This is a short tutorial about how to create a triggered event in your game. With this you could easily have a wall collapsing, turn the enemies from aggressive to friendly and reverse, or anything else you want to happen. There are still a few things to look out for and change in here, but it’s a good starting point for other programmers in different game teams.

 

Adding objects to a scene

 

There are several ways to add objects to a scene.   Three of the ways are: 

 

1.  You can add objects from the BaseDemoRegion  in OnLoadStaticContent by Spatial.AddChild(scene_object_name);

 

2. You can add objects from the Architect  see

http://game-engine.visual3d.net/wiki/tutorial-adding-asset-your-world

 

3.  Using Triggered Areas

 

 

Setting up ScriptedCTF.cs

 

The current ScritedCTF.cs without any changes when selected as a scene will look like the following:

 

 

I made the following changes to ScritedCTF.cs make the scene look more like My First Tutorial:

 

1.  Change camera, focus, and sky.

 

            public ScriptedCTFScene()

            {

            InitialCameraPosition = new Vector3(-10f, 3f, -10f);

            InitialCameraFocusPoint = new Vector3(0f, 0f, 0f);

 

            InitialCameraSpeed = 1f;

            InitialAmbientLight = new VColor(128, 128, 128);

            InitialSkyDefinitionName = "Afternoon.sky.xml";

            }

 

2.  Change terrain

 

And I changed the terrain in BaseDemoRegion OnLoadStaticContent to:

 

InfiniteTerrain terrain = InfiniteTerrain.SetSceneTerrain("GrassyFlat.InfiniteTerrain.xml");

 

 

3.  Change call to scripted tasks

 

Now to call my own scripted tasks, I made the following change to ScritedCTF.cs.

 

Change in ScriptedCTFScene. OnStarted change:

 

                  // С# tasks:

                  //tm.LoadTasks(new StaticSupport(typeof(Visual3D.ScriptedCTF.CTFTasks)));

 

to:

 

            // С# tasks:

            tm.LoadTasks(new StaticSupport(typeof(Visual3D.ScriptedCTF.MyTasks)));

 

4. Create MyTasks.cs

 

      On the Solution Explorer right click on DemoAssets.ScriptedCFT.Taks  - add new class

 

 

Cut and paste the following code into MyTasks.cs created by the new class:

 

 

using System;

using System.Collections.Generic;

using System.Text;

using Visual3D.Demo.Actors;

using Visual3D.Scripting.API;

using Visual3D.Demo;

using Microsoft.Xna.Framework;

using Visual3D.Scripting.BehaviorSystem;

using Visual3D.Scripting.Tasking;

using Visual3D.Scripting.Tasking.Events;

 

 

namespace Visual3D.ScriptedCTF

{

    public sealed class MyTasks : StaticScript

    {

 

        public MyTasks()

        {

 

        }

 

        public override void Registration()

        {

            // One more variant of registration of a task

            //ScriptLanguagesSupport.AddTask("StartScene", new SceneLoad(), true);

        }

 

        static TaskMethod StartSceneDesc = new TaskMethod(true, new SceneLoad());

        public void StartScene(SceneLoad e)

        {

 

 

            SEntitySystem.Create<AssaultDroid>("my demo droid", new Vector3(0f, 0, 0));

 

        }

 

    }

}

 

 

5.  Build the solution

 

 

 

 

6. Add triggered Event

 

In this step we will add a home location for the droid to move to.  When the droid moves to the home location, it will trigger an event in this case display a message on the screen.

 

 1.  Add new object “droid_home” 

 

To MyTasks.cs StartScene add:

 

SEntitySystem.Create<DroidHome>("TestHome", new Vector3(10f, 0f, 10));

 

 

2.  Create a new routine that will handle the trigger.

 

After the StartScene routine add the following code:

 

        static TaskMethod TestTriggerDesc = new TaskMethod(new AreaTrigger("TestHome", "", IntersectType.Enter));

        public void TestTrigger(AreaTrigger e)

        {

            SSystem.ShowCaption("Trigger activated");

        }

 

3.  Build Solution

 

Complete code of MyTasks.cs will look like:

 

using System;

using System.Collections.Generic;

using System.Text;

using Visual3D.Demo.Actors;

using Visual3D.Scripting.API;

using Visual3D.Demo;

using Microsoft.Xna.Framework;

using Visual3D.Scripting.BehaviorSystem;

using Visual3D.Scripting.Tasking;

using Visual3D.Scripting.Tasking.Events;

 

 

namespace Visual3D.ScriptedCTF

{

    public sealed class MyTasks : StaticScript

    {

 

        public MyTasks()

        {

 

        }

 

        public override void Registration()

        {

            // One more variant of registration of a task

            //ScriptLanguagesSupport.AddTask("StartScene", new SceneLoad(), true);

        }

 

        static TaskMethod StartSceneDesc = new TaskMethod(true, new SceneLoad());

        public void StartScene(SceneLoad e)

        {

 

 

            SEntitySystem.Create<AssaultDroid>("my demo droid", new Vector3(0f, 0, 0));

 

            SEntitySystem.Create<DroidHome>("TestHome", new Vector3(10f, 0f, 10));

 

        }

 

 

        static TaskMethod TestTriggerDesc = new TaskMethod(new AreaTrigger("TestHome", "", IntersectType.Enter));

        public void TestTrigger(AreaTrigger e)

        {

            SSystem.ShowCaption("Trigger activated");

        }

    }

}

 

4. Run Tech Demos/scriptedCTF and double click on the droid and walk him onto the home location and see the “Trigger activated” message come up.

 

 

  Mike Hoisington, based on work by Tashi Duane, Eliot Freedman, Mike Wetherill

 

Other Notes on Creating Trigger Areas via C# Code

Tut has been updated and explained on forums.
Original Source

tested with beta 3.3 still working.

This is a short tutorial about how to create a triggered event in your game. With this you could easily have a wall collapsing, turn the enemies from aggro to friendly and reverse, or anything else you want to happen. There are still a few things to look out for and change in here, but its a good starting point for other programmers in different game teams.

Triggered Area Tutorial

Triggered Areas are activated through the engine's task manager, so in part learning how to create and use them will also demonstrate how to use the task manager itself.

ScriptedCTF.cs, line 49:

// C# tasks:
tm.LoadTasks(new StaticSupport(typeof(Visual3D.ScriptedCTF.CTFTasks )));

This loads a file into the task manager, which then runs all of the tasks therein.

For the purposes of a triggered area, we first need an entity with a bounding box/"triggered area" class as a child.

We add the following to:
CTFTasks.cs, line 68

SEntitySystem.Create<DroidHome>("TestHome", new Vector3(0f, 0f, 0));

When the task list is interpreted, this will create an instance of "DroidHome" (an entity class with a triggered area, mesh, textures, etc...) at vector 0,0,0.

Now, we just need to add something to the task manager that tells it to fire a function each time something happens to our new entity.

Add the following to:
CTFTasks.cs, line 163

static TaskMethod TestTriggerDesc = new TaskMethod(
new AreaTrigger("TestHome", "", IntersectType.Enter));
public void TestTrigger(AreaTrigger e)
{
SSystem.ShowCaption("Trigger activated");
}

The first two lines of this code add a new task method to the task manager, named "TaskTriggerDesc". The name is very important- whenever the task is called, it will run a function with the name of the task, minus the "Desc" at the end. Therefore, adding a taskmethod "foobarDesc" will fire a function "foobar" whenever it is called. The second part of the new task method is what is actually making it happen- in this case, a new AreaTrigger. AreaTrigger has three params, but for the purposes of this example, all we need to pass it is the name of the entity/area we are testing for (in this case, "TestHome"), a blank string, and the intersection type we're testing for (Enter, Leave, or Any).

Once the taskmethod is established, we only need a properly named function (the name of the task sans the "Desc" at the end) in the format shown above. The above example will output a message to the console every time an actor enters into the "TestHome" entity's triggered area.

Mike Wetherill
Eliot Friedman

Additional note:
static TaskMethod TestTriggerDesc = new TaskMethod(
new AreaTrigger("TestHome", "", IntersectType.Enter));

the blank string in there can be altered to have it only fire when this string matches the target named.

-Project Asylum Programming team