Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by celadon · Feb 06, 2011 at 10:08 PM · selectionaction

Where do character-object interactions live?

I am working on adding selection and interaction to my game. The Selection part (for my player) is not too difficult - I'm using the raycast-from-camera method to click-select. Selectable objects have a Selectable script attached to them so I can tell whether they can be interacted with.

My question is about how people structure their code to manage this interactivity. Different objects have different actions (e.g. pick up, open, press). Different characters may be able to perform different actions. Some actions may have other constraints (e.g. need a specific key or other item).

The selection code will need to combine all of these data to determine the available actions and offer options to the player. When an action is executed, it may need to affect the object (e.g. the door opens), the character, or both (e.g. the character's inventory gets the item, the item's game object is destroyed).

I am wondering if anyone has pointers, suggestions, tutorials, etc. that help keep this manageable since it can easily become a complicated mess... Do you have some kind of separate data structure that defines all possible actions and assigns them to specific items and characters? Subclassed behavior classes that define each possible action? How do you match up the "actions item offers" with the "actions character can perform" and then execute the correct code on each end?

I don't want to keep all of this code in my player-character, because I'd like to use the core functionality for NPCs as well (just add the GUI interface for the player)...

Comment
Add comment · Show 1
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 celadon · Feb 07, 2011 at 01:07 AM 0
Share

...also, I realize that the details of things like an inventory system or NPC AI are way out of scope for this question. I am just wondering about the architecture of a selection- and action-management system that would be flexible enough to interface with other systems like an action selection GUI, or an inventory management system, or an NPC's AI depending on the situation.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by _Petroz · Feb 07, 2011 at 01:15 AM

Different characters may be able to perform different actions. Some actions may have other constraints

There are a number of approaches:

One method is double-dispatch http://en.wikipedia.org/wiki/Double_dispatch Using a visitor you can determine the correct set of actions based on the types of the classes.

Another method would be some kind of conditional action, which contains a delegate which evaulates to true if that action is possible. Both objects could add all of the possible actions and then you can draw the actions based on which are possible.

pseudocode:

possibleActions.add(new ConditionalAction( "Look for potion", delegate { return (player.hasPotion == false); } );
Comment
Add comment · Show 2 · 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 celadon · Feb 07, 2011 at 02:58 AM 0
Share

thanks, I was not familiar with the Visitor pattern before. Definitely worth looking into - I don't know if it will provide a complete solution, but it will get me on the right track.

avatar image _Petroz · Feb 07, 2011 at 04:09 AM 0
Share

visitor is a good alternative to RTTI, we use it at work quite a lot.

avatar image
0

Answer by reissgrant · Feb 06, 2011 at 11:00 PM

I set up my items as prefabs (doors, triggers, health etc) with scripts attached to them so it leaves my main character free from these scripts. The scripts on the prefabs just have colliders on them to check if they are interacted with by anything with the "player" tag and execute and script associated with them if the "player" conditions are met. Ideally, I'd like to set it up with a more sophisticated design pattern, but as it is now, it works just fine like this with minimal clutter.

Comment
Add comment · Show 1 · 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 celadon · Feb 07, 2011 at 12:59 AM 0
Share

Sorry, this does not really answer my question. Triggering "automatic" actions is pretty straightforward. $$anonymous$$y question is for situations where there may be multiple possible actions (e.g. Open door or Lock door), or when you want to give the character a choice (do you want to open the door?), or when the action affects both the "target" and the "actor" (maybe you have a single-use key that disappears after the door opens).

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

No one has followed this question yet.

Related Questions

Level selection from main menu? 4 Answers

Having to select an object multiple times to continue 1 Answer

Grouping objects in level editor 0 Answers

Why Is the game object not selected? I'd like to understand why. 2 Answers

How do I set the current script as the selection in the project view? 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