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 purplekjw · May 18, 2018 at 03:10 PM · propertiescustom class

How to retrieve properties from a class without prior knowledge of what those properties are?

I'm sure I'm missing a trick here. I think I may have even done this before, but right now I'm stumped and have been for the last few hours.

Here's the code snippet I wish to change:

 void populateList (List<Item> itemList) {
         for(int i = 0; i < itemList.Count; i++){
             GameObject item = Instantiate(listEntry);
             UnityEngine.UI.Text output = item.AddComponent<Text>();    
             output.text = (itemList[i].name + " Minimum damage: " + itemList[i].minDamage);
             output.font = listFont;
             item.transform.SetParent(transform);
         }
     }

It's the "output.text... etc." line that I'm concerned with. I'm trying to stick to the single responsibility principle and it irks me that I'm referring to an item's properties explicitly in a script which shouldn't be concerned with such things. Also, although the itemList currently only contains weapons it will soon contain other items which will need to display a variety of information dependent on their type (defense values for armour, etc).

I was thinking along the lines of changing this method to display any information which is passed to it, and then choosing the information to be passed in my Item class and its children. How would I achieve this, or what other solutions would you recommend?

I feel like I'm overthinking it and have overlooked an obvious solution at this point.

Apologies if there are no paragraphs in this. I'm not quite sure how to insert them.

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 Oliver1135 · May 18, 2018 at 03:37 PM 0
Share

I am making several assumptions but it sounds like you want to offload the responsibility of stringing together these properties in order to display a sort of "item blurb" if you will. in my line of thinking I reckon an item should know its own properties, it would be peculiar if it did not. You might want to consider pushing this functionality onto Item as abstract (no implementation) assu$$anonymous$$g weapon or whatever implements Item and item is itself abstract, then you can define what this property or function then returns, then in your populateList function you can simply call itemList[i].GetBlurbDescription(). if this is not suitable you could offload the responsibility to a service that builds up the blurb you want to display and just call into that.

2 Replies

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

Answer by ImpOfThePerverse · May 18, 2018 at 03:26 PM

In most C++ classes, you write a ToString function for your class that lets you output objects of the class via cout. What you'll probably want to do here is create an interface with a GetStatString() function. In each class that you want to print out, implement the interface. The implementation of GetStatString() will be different for each class. I can't really think of a way to automate writing the GetStatString() function, since you won't necessarily be wanting to print every variable in the class, and you need some way to associate a string name with the variable that won't necessarily be the same as the variable name.

Looking over your code, it looks like your items are all inheriting from an Item class, so you could add the GetStatString() function there and implement it in the derived classes instead of using an interface. Line 5 would end up looking like:

 output.text = item.GetStatString();

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 purplekjw · May 18, 2018 at 06:40 PM 0
Share

This does indeed answer my question, but it also makes me rethink my approach. A single string is probably not ideal in the long term, as I'll likely have multiple fields to fill on my UI. I believe I know how I will achieve this.

$$anonymous$$y question was flawed, but both of the answers I received have aided my comprehension and I thank you.

avatar image
1

Answer by Happeloy · May 18, 2018 at 04:09 PM

I'm not totally sure if this is what you are looking for, but you can iterate through all fields in a class like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TestScript : MonoBehaviour {
 
     void Start () {      
         System.Type type = typeof(ExampleClass);
 
         System.Reflection.FieldInfo[] fieldInfo = type.GetFields();
             
         foreach(System.Reflection.FieldInfo f in fieldInfo){
             Debug.Log("Field: " + f.Name);
         }
   }
 
 }
 
 public class ExampleClass
 {
     public string testString;
     public int testInt;
     public Vector3 testVector;
 
 }

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 purplekjw · May 18, 2018 at 06:15 PM 0
Share

Ah. I wanted the values themselves, not the names of the properties. The example you gave is something I didn't know was possible though, and I'm happy to have learned about it.

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

85 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

Related Questions

How do I create public read-only properties? 3 Answers

iPhoneKeyboard Set Text after validation 1 Answer

How do I expose public properties in the same way the inspector does via a gui in game? 1 Answer

Setting a property, and then making it readonly. (c#) 2 Answers

What is the correct way to set FBX user properties for import in Unity ? 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