Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by OtreX · Oct 03, 2015 at 02:37 PM · enumtargetingflying

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OtreX · Oct 04, 2015 at 05:17 PM 0
Share

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.

avatar image NoseKills · Oct 05, 2015 at 04:46 PM 0
Share

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.

avatar image OtreX NoseKills · Oct 05, 2015 at 08:03 PM 0
Share

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 :)

avatar image
0

Answer by OtreX · Oct 10, 2015 at 02:34 AM

@NoseKills

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges