Bombadin's Forum Posts

  • 2 posts
  • thanks!

    korbaach: I don't think Dictionary will work exactly for what I'm doing, unless I can store an Array as a data field in an associative array, or vice versa. Actually what I ideally want is an array of dictionaries, like in JS how you make an array of Objects (since in JS an Object and a Dict are the same thing). One thing I didn't mention is that in my actual use case, the array already is a multi-dimensional array ( data for different decks of event cards that I'm loading from CSV files )

    : Convert to JSON and then parse - I suppose that would work. Seems inefficient to have to serialize an object to a string format and then back again to be able to pass it as a reference or just assign it to another variable, but it's not a performance-critical bit of code so maybe I'll try that.

    blackhornet: ooh, that is sneaky ... one question though, how do I actually pick by UID? I can add System.Object With UID(Function.Param(0)) Exists", but that doesn't seem right.

    Global/local scope isn't really the issue - basically what it comes down to is I'm trying to avoid code duplication. I have some code working with the array, and I want to reuse the same code with each of the arrays, instead of having to duplicate it for each with branching.

    I guess another way to ask the question would be "can a variable refer to an array or other complex object, or variables only String, Float or Bool?'

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Is it possible to pass an array to a function, or reference an array object with a variable?

    I have several different arrays, and I need to select one of them and do something with it. Here's a pseudocode example:

    // Pick a random category of foods to put on display at the supermarket this week
    
    string[] Fruits={"apple","bananna","cherry"};
    string[] Veggies={"celery","daikon radish","rutabaga"};
    string[] Meats={"pork chop","steak sandwich","turducken"};
    
    int foodGroup=Random(0,3);
    
    if  (foodGroup==0) 
      SetupDisplay (Fruits);
    
    else if (foodGroup==1) 
      SetupDisplay(Veggies); 
    
    else if (foodGroup==2) 
      SetupDisplay(Meats); 
    
    void SetupDisplay(string[] foods)
    {
        foreach (string f in foods)
        {
          GroceryStore.AddToDisplay( f );
        }
    }
    [/code:38n9ul8v]
    
    The way I can currently see to do it in CS is like this:
    
    [code:38n9ul8v]
    
    string[] Fruits={"apple","bananna","cherry"};
    string[] Veggies={"celery","daikon radish","rutabaga"};
    string[] Meats={"pork chop","steak sandwich","turducken"};
    
    int foodGroup=Random(0,3);
    
    if  (foodGroup==0) 
    {
       foreach(string f in Fruits)
       {
         GroceryStore.AddToDisplay(f);
       }
    }
    
    else if (foodGroup==1) 
    {
       foreach(string f in Vegges)
       {
         GroceryStore.AddToDisplay(f);
       }
    }
    
    else if (foodGroup==2) 
    {
       foreach(string f in Meats)
       {
         GroceryStore.AddToDisplay(f);
       }
    }
    
    [/code:38n9ul8v]
    
    which is kind of awkward ( I mean, it's ok for this simple example but will start involving a lot of code duplication as it gets more complex).  
    
    thanks in advance for any tips
  • 2 posts