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 /
avatar image
0
Question by KakkoKakko · May 03, 2017 at 10:29 AM · inputmouse2d-gameplayfinite-state-machinehandling

TopDown Mouse Click Handler

Hello Everybody, I'm currently developing a 2D tile-based strategy game and i'm having bad troubles trying to implement some code to be able to handle different actions based on which mouse button is pressed and what "entity" is being pressed.

To be more specific this is how i want it to work:

  • mouseButton(0) -> perform a "selection" (be it "select friendly character" / "select enemy char" / "select chest" / "select trap on the ground" )

  • mouseButton(1) -> perform an "action" (be it "movement" / "heal nearby friendly char" / "attack enemy" / "open chest" / "open door" / "defuse trap" )

Entities are:

  • Friendly Character

  • Enemy Character

  • Ground Tile

  • DoorTile

  • ChestTile

  • TrapTile

Practical example: to "select a friendly character" -> "move it" -> "attack enemy if in range" i would press button(0) on a friendly character -> then press button(1) on a ground tile -> then press button(1) on an enemy character to attack it

I know i could write a kilometer long switch-case to handle all of this but it would not be elegant, and more importantly, if in the future i want to add new actions it would be a mess to do so. i read online i could implement in some way a FiniteStateMachine that handles everything I'm quite familiar with the theory about FSMs but i dont really know how to implement one. Here is how i would like to make my FSM alt text

From any state, if you click mouseButton(0) on an enity -> select that entity selection shows on a GUI-Panel the stats of what is selected if a player is selected it is also shown his reacheable tiles, attack range, etc..

Only Player-Controlled-Characters (allied) can perform "actions" if the target is in range These actions are (for now): Move to a particular tile / Attack an enemy / If an Healer is selected he is able to Heal Allies / If a Rogue is selected, he is able to Defuse Traps / open closed Doors or Chests

A nice final touch would be to check whether the currently selected character has finished his actions for this round and automatically select the next allied character that still has some actions available.

What i am asking for is some hindsights on how to implement that (or similar) FSM every time i try to do so i just return to write endless switch-cases and i know that is not the right way to do so.

Sorry for my bad english, Thank you for your time.

mouseinput-schema003.png (158.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I detect the mouse is moving through code? 3 Answers

Is there a built-in function to determine if any GUI item has been clicked? 2 Answers

Can you access non-standard mouse buttons? 2 Answers

How to disable mouse click detection in game when clicking exit menu button? 1 Answer

does getmousebuttondown counts as physicaly clicking (for mobile games)? 1 Answer


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