Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 mahdiii · Sep 16, 2017 at 11:01 AM · unity 5interfacenpccommandinteractive

Interactive with item using condition and command interfaces

Hi all. I would like to implement an interactive item system that the player can interact with items. I program interfaces condition and command and implement them like below:

Code (CSharp): public class CItemScene : MonoBehaviour {

         CBaseCondition[] m_conditions;      
         CBaseCommand[] m_commands;
         CBaseCommand[] m_failCommands;
         CPlayerInputController m_playerinputController;      
         [SerializeField]
         CBaseItemData m_interactiveItemData;
         Transform m_failCommandChildTr;
         Transform m_commandChildTr;
         public int m_id { get; private set; }
        
         public Sprite m_sprite { get; private set; }
         public Animator m_animator { get; private set; }
         // Use this for initialization
         void Start() {
             m_playerinputController = CGameManager1.GetInstance().m_player.GetComponent<CPlayerInputController>();
             m_id = m_interactiveItemData.id;
             m_sprite = m_interactiveItemData.sprite;
             m_animator = m_interactiveItemData.animator;
             m_conditions = transform.FindChild("Conditions").GetComponentsInChildren<CBaseCondition>();
  
             m_commandChildTr=transform.FindChild("Commands");
             if (m_commandChildTr != null) {
                 m_commands = m_commandChildTr.GetComponentsInChildren<CBaseCommand>();
             }
  
             m_failCommandChildTr = transform.FindChild("FailCommands");
             if (m_failCommandChildTr != null) {
                 m_failCommands = m_failCommandChildTr.GetComponentsInChildren<CBaseCommand>();
             }
            
            
         }
         bool CheckAllConditions() {
             for (int i = 0; i < m_conditions.Length; i++) {
                 if (!m_conditions[i].CheckCondition()) {
                     return false;
                 }
             }
             return true;
         }
         void ExecuteCommands() {
             if (m_commands!=null) {
                 for (int i = 0; i < m_commands.Length; i++) {
                     m_commands[i].Command();
                 }
             }
          
         }
         void FailCommands() {
             if (m_failCommands != null) {
                 for (int i = 0; i < m_failCommands.Length; i++) {
                     m_failCommands[i].Command();
                 }
             }
         }
         void Interact() {
             bool checkedFlag = CheckAllConditions();
             if (checkedFlag) {
                 ExecuteCommands();
             }
             else {
                 FailCommands();
             }
  
         }
         void OnTriggerStay2D(Collider2D _coll) {
             if (_coll.tag=="Player") {
                 if (m_playerinputController.CheckPressedKeyDown(EKeyType.Action)) {
                     Interact();
                 }
             }
         }
     }
 }
  
  


if conditions are true then commands are executed else fail commands are executed. I want to use general conditions(combination conditions like or and not etc) rather than only "And" conditions, so I implemented OR,And,Not module conditions like below: Code (CSharp):

 public class CORCondition:CCondition{
 CCondition c1;
 CCondition c2;
 public override bool CheckCondition(){
 return c1.CheckCondition() || c2.CheckCondition();
 }
 }

The first way I can combine conditions and get only result condition like (cresult=(c1 or c2) and (c3 and c4)) and assign it to m_conditions inside my code. The second way I can put them all as gameobjects(condition components postfix or prefix orders) like below:(children of Conditions)

Conditions

-condition1

-condition2

-orcondition

-condition3

-condition4

-andcondition

-andcondition

then compute the result condition using postfix or prefix order or can be

-andcondition

---orcondition

-----condition1

-----condition2

---andcondition

-----condition3

-----condition4

m_conditions = transform.FindChild("Conditions").GetComponentsInChildren();

Is it better way that a designer can add and remove these conditions without knowing extra knowledge and concepts? How can we extend it to contain more generic states for commands too? for example they are executed in OnTriggerStay or OnCollisionEnter or other sections

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

139 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 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 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 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 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

How to connect Unity object with Beagebone Black board? 0 Answers

how to use custom word as KeywordRecognizer in Unity 0 Answers

Calling Command Function from Client results in "NetworkServer not active..." 1 Answer

Rendering Command Buffer issue while trying to apply postprocessing effects 0 Answers

How to make an interactive App to play on 4 different monitor? 0 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