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

Random item generation and inventory slots

Discussion in 'Scripting' started by NerdRageStudios, Dec 11, 2015.

  1. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Hi, I am tying my brain up in knots here trying to work out the best way to approach this scenario. For simplicity, lets take the random items from diablo or borderlands etc.

    They tend to follow a structure:

    Prefix, Item, Suffix
    eg. Rusty Shotgun of Slaughter

    So, I was trying to work out the best approach for this.

    It seems to me that assigning prefixes and suffixes to a number is the simplist, ie. 1 - 10.
    Then when generating the weapon, you simply randomise the numbers for prefix, item and suffix and hey presto you have a weapon.

    So my question is how best to handle this?

    I thought it would be easy to store the 3 variables in an array, for example

    Code (CSharp):
    1. int[] weapon1;
    2. weapon1[0] = 6;
    3. weapon1[1] = 3;
    4. weapon1[2] = 4;
    But if you then had a 10 slot inventory for example as an array, I dont think you can place an array inside an array, so I am totally confused on the best way to construct this.

    I can do this easily when you handle inventory items as prefabs, but I have no idea how to handle random item generation like this.

    Has anyone done something similar to this? Any help would be greatly appreciated :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why not?

    .. but it would probably make sense to wrap the item information into a class and store those class objects in the inventory.
     
  3. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    To make a good weapon system you really need something a bit more complex imho.

    I'd build a weapon base class that has a type (pistol, rifle, shotgun) then a set of component classes. For example, Sight, Ammo / magazine, Stock, Barrel.

    Each of these components would then have a number of modifiers such as zoom, accuracy zoomed and unzoomed, range, damage , extra effects (like poison or explosives), recoil, spread. The possibilities are many, dependant on your game and what you want to achieve.

    When you spawn a weapon you can randomly assign these values based on a modifier by rarity.

    There is other stuff on the forum about it as well you can probably find more

    http://answers.unity3d.com/questions/298480/how-would-i-go-about-making-randomised-guns-border.html
     
  4. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Thanks for the feedback, I get the ideas here, and I think I have come up with a solution... although I suspect the code wont be pretty and could be optimised!