[Help] [C#] Storing information in map tiles, and triggering events on entry/exit.

Just to preface, if anyone can help me in this very specific endeavor I would be very grateful, but anyone who has relevant info or can point me in the direction of relevant info is welcome to post as well!

I have moderate experience with unity and Unityscript, but am making the transition from Unityscript to C# so I’m missing a bit of info in some areas (mostly classes).

I am trying to make a map system where different tiles will contain events (think FTL) and trigger events (again, much like FTL) when the player enters them. The tiles will also contain a biome type but these provide a more passive effects (apparently I’m just making FTL). Normally I would just buckle down and hard code the events in but I want to make it modular in order to be random-generation friendly, and this is where I have no idea how to structure stuff.

In summary:
-Triggering events when a player enters a tile
-Tiles have biomes as well as events
-Biomes and events can be grabbed randomly when the tiles are instantiated
-BIomes and events can be grabbed specifically in editor or when instantiated

Again, although specific code is very welcome, I would also like any relevant info on what I’m trying to do if you have it, such as links to tutorials, videos, or even other threads. Thanks in advance!

I like data-driven design. Prefabs might be a good fit here to do that. Briefly, when you instantiate a tile, add two optional child objects chosen (randomly) from a list: an event prefab and a biome prefab.

Here’s what I’m thinking. Create an empty GameObject with a trigger collider. Set the layers so the player will register collisions with it. Then add a script that implements OnTriggerEnter().

The script below instantiates an object on the tile – for example a poison gas cloud.

public class InstantiationTrigger : MonoBehaviour {
    public GameObject objectToInstantiate;

    public void OnTriggerEnter(Collider other) {
        Instantiate(objectToInstantiate, transform.position, transform.rotation);
    }
}

The script below sends a message to another object. Say the player has boarded a ship with an alarm system. When the player enters this tile, it could send a message to the alarm system to start making the alarm noise.

public class MessageTrigger : MonoBehaviour {
    public GameObject target;
    public string message;

    public void OnTriggerEnter(Collider other) {
        target.SendMessage(message);
    }
}

If you need to share behavior among scripts, you can make them a subclass of the same parent script. In the example below, the parent script adds the ability to delay trigger activation for a specified amount of time.

public abstract class AbstractTrigger : MonoBehaviour {
    public float activationDelay = 1;

    public void OnTriggerEnter(Collider other) {
        Invoke("Activate", activationDelay);
    }

    public abstract void Activate();
}

public class InstantiationTrigger : AbstractTrigger {
    public GameObject objectToInstantiate;

    public override void Activate() {
        Instantiate(objectToInstantiate, transform.position, transform.rotation);
    }
}

You’d do something similar for biomes.

To set up a tile in the editor, just add prefabs as children.

To generate a tile randomly at runtime, you could use a script that has a list of prefabs:

public class TileGenerator : MonoBehaviour {
    public GameObject[] eventTriggers;

    public AddRandomEvent(GameObject tile) {
        int index = Random.Range(0, eventTriggers.Length);
        GameObject eventTrigger = Instantiate(eventTriggers[index], transform.position, transform.rotation);
        eventTrigger.transform.parent = tile.transform;
    }
}

(I just typed this code in. It’s not tested, and you should probably also do error checking.)

1 Like