• 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 malkere · Feb 21, 2015 at 09:53 AM · inventoryrpgtypetostring

ToString() returns Type. Can get hard coded Items name?

I have an RPG system I'm working on that uses progressively increase items which are hardcoded into a WeaponDatabase, ArmorDatabase, prior to modifiers being applied. For example,

 public static Item Dagger00 = new
         Weapon(..., ..., ..., ...);

is the basic crafting tutorial dagger and where it's stats are stored.

Now when using Resources.Load() for my items sprites in the UI I can call WeaponDatabase.Dagger00.name and I get "Training Dagger" or whatever, and can then call my sprite "Training Dagger.png" to have it automatically find my sprites for all of my items, but I was trying to rewrite it so that it uses the Dagger00 instead.

In the long run this method will make it easier for me to sort my sprites as there are over 100 daggers alone, not to mention swords/boots/whatever, and sorting things nicely is going to be important... I don't need a different sprite for all 100+ daggers but you know...

calling Dagger00.ToString() returns "Weapon". Is there a way to reference

public static Item This part

without wrapping another variable in the Weapon class?

Thanks for any input.

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 Bonfire-Boy · Feb 21, 2015 at 01:05 PM 1
Share

Having your resources named for variable names seems like a really fragile method to me. But I think this looks relevant... http://stackoverflow.com/questions/9801624/get-name-of-a-variable-or-parameter

2 Replies

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

Answer by malkere · Feb 21, 2015 at 02:22 PM

following Bonfire Boy's link and lookin around a bit I found this:

http://stackoverflow.com/questions/716399/c-sharp-how-do-you-get-a-variables-name-as-it-was-physically-typed-in-its-dec

I wrote out this:

     string GetName<T>(T item) where T : class {
         return typeof(T).GetProperties()[0].Name;
     }

and called it like this:

 Debug.Log(GetName(new { WeaponDatabase.Dagger00 }));

to get "Dagger00"

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
avatar image
1

Answer by RudyTheDev · Feb 21, 2015 at 01:50 PM

Unfortunately, no. A class cannot know what "name" you use to reference it elsewhere, because you can reference it via >1 variable or not at all. You could conceivably parse your XxxDatabases with reflection to find the variable name (like Bonfire Boy comments). But this is very poor code design, especially for your needs.

The quickest solution would be to just add some string resourceName variable to the Weapon class.

(However, a proper method is to not create the items in code at all, rather make them as prefabs/assets and put them in Resources/, then Resource.LoadAll<>() them all. When the number of your items goes above 2-5, you should start keeping them in a collection. It it really hard to change and refactor hundreds of items, you essentially lock yourself to your first decision. It is very prone to errors and you cannot perform anything on all items (like if you want to drop a random dagger, you need to write a hundred lines just to choose one).)

Comment
Add comment · Show 5 · 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 malkere · Feb 21, 2015 at 02:19 PM 0
Share

I can see where a lot of your answer is co$$anonymous$$g from and it's given me some things to consider. However following Bonfire Boy's comment I did find an easy solution to what I was trying to do I will post as an answer. I suppose yours is answer too, though more advice than a solution, we can let others vote either up though. Thanks for the reply.

avatar image malkere · Feb 22, 2015 at 03:25 AM 0
Share

I've been thinking about your advice and I'm thinking of setting up a base set of stats for say a dagger, and then formulas for each stat that can be calculated depending on the level input when calling for a dagger(int level). That way I could run a for loop and generate 1-50 easily, or just pull a random level out etc.

also it's a crafting game so there are lots of possible additions to each item. this way I can just attach a list of buffs on each item which can be random or specific easily enough... sadly I'm busy today and have to go outside now >_< haha, but in the long run I think this will turn into a much better system thanks to your advice. I can just tweak the formulas if needed. Thanks again.

avatar image RudyTheDev · Feb 22, 2015 at 03:59 PM 0
Share

Glad it helps.

avatar image Bonfire-Boy · Feb 22, 2015 at 10:23 PM 0
Share

I probably should have stressed the first sentence of my comment a bit more.

avatar image malkere · Feb 23, 2015 at 09:54 AM 0
Share

in the case that they're hard coded it basically becomes the recipe or item's global static name which was making a _name a redundant factor. I rewrote everything to be functions returning weapons/armor based on level input with a _code variable for the type/level for which I can easily assign sprite to. it's all working nicely now I just need to make the level formulas more interesting. Thanks again.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How can I store game data outside of scripts.(e.g Total list of inventory items) 2 Answers

Text RPG inventory 1 Answer

Getting started: Inventory script 0 Answers

How to call value by key when key is an object? 1 Answer

New UI Is active but not drawing 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