Search Unity

How do you include a list as a parameter?

Discussion in 'Scripting' started by Draconic, Apr 16, 2014.

  1. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    Hi, I am quite new to unity, and have started making an item and skills system for my game. What I have done in my scripts is make each item constructed with 3 parameters (a name, a 2d icon and a 3d texture). Then, each item is given a list of actions that you can do with it. I called them functions in the script, so please don't get them confused with actual functions! Each new function is added to the list of functions that the item has with 3 parameters, an action type, a magnitude and a list of skills that the action uses.

    What I wanted to know is, when I am creating new items, how do I put the list of skills as a parameter? I'm really stuck here, so help would be great !

    What I want to do is make a new action, then add it to a list of actions that the item has. Each action has it's own list, which contains the skills that are used. When I construct an item and it's actions, I want to have the list as a parameter, so that each time I create an action I create a new list that the action uses.

    To put it without the context of my game, if I want to pass have a list as a parameter of a constructor, how do I represent a list when I create a new instance of that class?

    Here is the code:

    Item script
    Skill script
    Skill Database
    Item Database
     
    Last edited: Apr 16, 2014
  2. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    I think you should post some code here, because it's really hard to help you if we don't know anything about the structure of your code.
     
  3. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3.  
    4. public class Example {
    5.  
    6.         public List<Actions> actions;
    7.  
    8.         public Example (List<Actions> actions) {
    9.                  this.actions = actions;
    10.         }
    11.  
    12. }
    13.  
    14. Example example = new Example(new List<Actions>());
    15.  
    Hope that helps.
     
    NSokolovskyi likes this.
  4. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Not really, I'm still confused.
    I simply can't wrap my head around what exactly it is that you want.

    Please tell me if I'm correct:
    -You have items (as in: certain objects the player can interact with or use)
    -All of those items have certain attributes (name, icon, texture) and different sets of actions they can perform

    What confuses me the most is the part about the skills.
    Are those skills like "skill-requirements"? (You need to unlock/have some skills to use specific actions)

    Sorry that I can't really help you, but the code you provided hasn't cleared up anything. I'd love to know about the structure of your code. Something like a class diagram would be sweet.
    The thing is that while there's tons of ways to implement the same thing, only a fraction of them is actually good.
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Draconic posted the question.

    You're quoting an answer from Diviner.
     
  6. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Oops, lol. :oops:

    Anyway, I'd still love to know more about the surrounding code structure. I just wrote an inventory system myself (with items and all this stuff) and it took me some tries to get it all right. Now I have a system that allows me to implement complex behavior for unique items easily.
    Maybe the OP should/could restructure his code.
     
  7. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    I edited the post to include my code!
     
  8. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Use the
    Code (csharp):
    1.  tag instead of [QUOTE]. ;)
    2.  
    3. Anyway.
    4. Including a list as a parameter is as easy as with any type.
    5. [CODE]List<string> example = new List<string>();
    6. example .Add("Stuff");
    7. example .Add("More Stuff");
    8.  
    9. items[addedCount].functions.Add(new Function(Function.functionTypes.Attack, 1f, example));
    However, I think you shouldn't add the skills in form of a List<string>, but rather List<Skill>. That way you can't add nonsense like non-existent skills.