Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Item system with custom extensions.

Discussion in 'Scripting' started by zotey, Apr 12, 2017.

  1. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    Hey everybody,

    I am building an dynamic item system in unity.I'll quickly explain what im tying to do.
    Your items can have different parameters like, rarity, weapontype etc... Before you can create any items, you have to setup your parameters in the editor. Here I want people to be able to make for example a parameter called rarity and here they can assing a enum. (You can't dynamicly store classes in C# so here's a problem). or adding a parameter called itemname being a string, where they can input there text. They select the type via an enum, so they can shoose one of the suported types for each parameter. Now my question is how can I overcome the enum problem? What shoud be a different way to do it?

    Regards,

    Zoteygamer
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    It sounds like you might be best off storing your data in something that supports dynamic types, dictionaries, and arbitrary creation of new fields and values. I strongly suspect that you want to be able to save this out to a file at some point, too.

    When described this way, it matches the description of JSON pretty precisely. Unity has a built in JSONSerializer, but it has some limitations. Normally in Unity people just want to grab the JSON and map it directly onto an object, but you want to be able to add arbitrary fields, so you'd be better off accessing the JSON data directly. If you can grab the LitJSON plugin, I think that may be the class you want to use here.
     
  3. zotey

    zotey

    Joined:
    Nov 14, 2012
    Posts:
    26
    Well, actually I wrote a json framework right before this to use with unity. So I was thinking I could use a generic type and let the user select the type and then pass the right type into the generic, wich would just work fine. But not when I want to use an enum because you can't store an enum in a variable, or can you?
    In each case thanks for the answer.