How do I work with two ENUMS
Well, I'm making a space shooter. Sometimes my ships create a formation. They do this by targeting a gameobject MainFormationObject that stores an array of empty gameobject WayPoints. The ships target the MainFormationObject and this object tells the ship which WayPoint will it target. For the sake of keeping it short, I'll call the ship that is targetting the leader waypoint the Leader and the ships that are targeting the escort waypoints the Escorts. Now I'm trying to break the formation when certain conditions are met (by breaking the formation I mean: tell the ships to not target these WayPoints anymore / change their target).
I thought of using two enums:
1) What triggers the break of the formation {
leader gets fired at,
escort gets fired at,
leader dies,
escort dies,
formation changes target,
formation's target gets destroyed }
and
2) How does the formation breaks {
escorts will target the ship that is firing at leader (only used if the leader is getting fired at),
make all the ships get a different target,
leader gets a different target,
escorts get a different target,
do nothing }
Basically, in the inspector I want a way of having the drop down menus for "What triggers the break of the formation" and then selecting by another drop down menu "How does the formation breaks" so its kind of user friendly.
I know how to code all that by using bools, but its a mess. I just don't know how to relate one trigger of breaking the formation to the way I want the formation to break (I know how to hard code one trigger to one way of breaking the formation, but i don't want that). Also, is this the most efficient way to do it? If you think of a more efficient way feel free to share it.
Thank you in advance.
Answer by NoseKills · Oct 04, 2015 at 01:21 AM
A custom editor might be a good option but if you don't want to go that path, something like this would probably work (if i understood you correctly)
public enum BattleEvent
{
LeaderFiredAt,
EscortFiredAt,
LeaderDied,
EscortDied,
FormationTargetChanged,
FormationTargetDestroyed
}
public enum Reaction
{
DefendLeader,
FireAtWill,
LeaderRetarget,
EscortRetarget,
None
}
public Reaction LeaderFiredAtReaction;
public Reaction EscortFiredAtReaction;
public Reaction LeaderDiedReaction;
public Reaction EscortDiedReaction;
public Reaction FormationTargetChangedReaction;
public Reaction FormationTargetDestroyedReaction;
public Dictionary<BattleEvent, Reaction> ReactionMap =
new Dictionary<BattleEvent, Reaction>();
void Start () {
ReactionMap.Add(BattleEvent.LeaderFiredAt, LeaderFiredAtReaction);
ReactionMap.Add(BattleEvent.EscortFiredAt, EscortFiredAtReaction);
ReactionMap.Add(BattleEvent.LeaderDied, LeaderDiedReaction);
ReactionMap.Add(BattleEvent.EscortDied, EscortDiedReaction);
ReactionMap.Add(BattleEvent.FormationTargetChanged, FormationTargetChangedReaction);
ReactionMap.Add(BattleEvent.FormationTargetDestroyed, FormationTargetDestroyedReaction);
}
public Reaction GetReactionTo(BattleEvent bEvent)
{
var react = Reaction.None;
ReactionMap.TryGetValue(bEvent, out react);
return react;
}
Thank you for replying @Nose$$anonymous$$ills.
I've never done a custom editor, sounds interesting and I'm willing to do it if I find it not that challenging. I wanted this project done a while ago, but I had to stop for a while.
What I want is that the designer is able to choose how to break the formation when one of those conditions are met, thats why I'd like to have the two enums or some other way to allow the designer to chose how to break the formation on the inspector.
Im a bit lost with the Reaction$$anonymous$$ap. What is it, a variable of a custom class? It seems that youre more experienced than me with unity scripting.
Also, I don't see how or where are the conditions being detected on the Update() function.
I have the 2 enums set up like you described. Then i have the public Reaction variables named in a way that indicates when that reaction should be chosen... The designer can set the public Reaction $$anonymous$$erFiredAtReaction;
to for example Reaction.Defend$$anonymous$$er
to indicate the leader should be defended in that case.
The map is just a structure to link events to reactions so you don't have to make a long if-else to choose the right reaction when you know the event.
How and where to use this (Update()?) is a bit out of the scope of the problem. It can be done in a million ways. But for example when a leader of a formation gets shot (OnCollisionEnter()?), you can call GetReactionTo(BattleEvet.$$anonymous$$erFiredAt)
on the leader's formation and it will return the Reaction the designer set for that formation for that event.
I see, I've never used the map structure. On the Update() function, thats exactly what I meant... what to use and when to use it. I have working code that detects all of those variables, but then I didnt know how to call the reaction using this system. I'll give it a shot later today and post my results.
Thank you again @Nose$$anonymous$$ills :)
Answer by OtreX · Oct 10, 2015 at 02:34 AM
So I almost have it working, I think. Everytime that I meet any of the conditions I call
public Reaction GetReactionTo(BattleEvent bEvent)
{
var react = Reaction.None;
ReactionMap.TryGetValue(bEvent, out react);
return react;
}
and it returns the Reaction that I set on the inspector. But now, the problem that I have is that I actually don't know what to do with it. I think its simpler if I explain it with an example:
Lets say that the designer built the behavior on the inspector in this way: The "Battle Event: Leader Gets Fired At" and the "Reaction: Everybody Breaks Formation"
I detected the Battle Event and called "public Reaction GetReactionTo(BattleEvent bEvent)" method. I have the code for telling all the ships to break formation, but... WHERE DO I PUT IT?! What makes that code run? That's what I'm missing.
Again, I'm super grateful for your time. I didn't know about dictionaries and this is teaching me a lot about them. Thank you. thank you, thank you.
Your answer
Follow this Question
Related Questions
Where do you find camera target on tank game? 0 Answers
Accesing enum type from another script 1 Answer
C# naming related enum, private member and public accessor. 2 Answers
transform.rotation interferes with transform.Rotate 1 Answer
Calculate UP Distance Displacement for 3D Infinity Runner Game 4 Answers