How do I pass an array to a function?

0 favourites
  • 8 posts
From the Asset Store
Assets for creating mountains and ravines environments
  • 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
  • If I correctly understood...

    you need associative array(dictionary) or two-dimensional array...

    dictionaryArray.capx

  • Convert it to a JSON string and pass it then parse it inside the function?

    You shouldn't have to pass arrays anyway though, they are globally (within your application) accessible.

    But in your example, in order for it to be compatible with JSON you need to change your {} to [ ] as a string. Or you can just convert it using json2.js like this.

  • You can pass the UID of the array and pick it in the function.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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?'

  • [quote:2qlqyoaa]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.

    The array object has a pick by uid condition.

  • Also, if you actually have different array object types, put them all together in a family aptly named "Arrays". You could then use this family to easily access any array provided you have its UID.

  • : 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.

    Yea, which is why you should be able to just use the Array as a global object within a given C2 application. You should just be able to access the Array knowing the index of various data, or just loop through it and look for specific data types. Looping is far less CPU intensive than actually passing data as JSON arrays.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)