- Home /
The best way to trigger game events in a list
I'm trying to create a system that can read through a list of "game events" and activate them accordingly, but am having difficulty creating a solid, efficient approach that's easy to expand and work with. A pseudo-code example of what I mean is
manager.Text(player_portrait, "Hello!")
manager.Move( npc, 2, -2)
manager.ShowImage("random bottle")
manager.Text(npc_portrait, "Do you like my bottle?")
switch (manager.Choice(Yes, No))
{
case 1: {
manager.Reward(Reward.Gold, 100);
}
case 2: {
manager.StartBattle("crazy person");
}
}
manager.Text(player_portrait, "That was odd.")
}
My question is: what's the best way to achieve this?
I've tried having a base class "gameEvent" that all events (text, move, reward, battle, etc.) derived from, but Unity is not keen on polymorphism and I couldn't get a List where I could edit each member of the list inside the Unity Editor (I tried custom editors and property drawers, I had issues with Unity's serialization).
I'm currently toying with the idea of creating a Unity Script (C#/JavaScript, whatever) for every "event thread" as it were, which would just call lots of functions in a "manager" class, but I feel this would seriously bloat my project file, as it's an RPG and I imagine using this game event system for talking to all NPCs, controlling cutscenes, random or scripted battles, or the like.
I've thought about using XML - or even a binary file, created external to Unity - that could be read in, but I worry about the performance issues for that? First there's looking up the file and then I believe also that parsing strings is not very desirable for performance.
From reading around this site I feel I might be able to do this with delegates, but I can't see how I could customise the inputs (such as varying text) for each delegate in the list?
Anyway, I understand this question is kind of vague, but I'm just curious what other people would try? I've only been working with Unity for a little over a year and I know there's a lot more I can do with learning.
P.S. This is my first question on here, apologies if it's badly formatted! I've tried searching for existing answers, but am having difficulty coming to a conclusion.
EDIT: I had some formatting issues with my pseudo-code, so I fixed it to look a bit more like C#.
Answer by Smitlord · Apr 15, 2016 at 07:51 PM
there is a much more simple way to do this just put these into booleans but have the results in an array so heres an example
var attack:int;
var powerboost:boolean;
var damaged:boolean;
var damage:int;
var enemy health:int;
var health:int;
var results:[];
Function Start (){
enemyhealth=100;
attack=10;
health=100;
damage=10;
if(results.Random.Range(1,100)<49)
{
damaged=true;
}
if(damaged==true)
{
health=health-damage;
} else { powerboost=true; } if(powerboost==true) { enemyhealth=enemyhealth-powerboost; }
Your answer
Follow this Question
Related Questions
foreach Element Problem 1 Answer
C Sharp Messenger Extended Warning 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
A node in a childnode? 1 Answer
Need advice for C# Freelance-Work 1 Answer