How do I pass an array to a function?

Get help using Construct 2

Post » Mon Mar 28, 2016 2:33 am

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:

Code: Select all
// 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 );
    }
}


The way I can currently see to do it in CS is like this:

Code: Select all

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);
   }
}



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 :)
B
4
Posts: 2
Reputation: 202

Post » Mon Mar 28, 2016 3:37 am

If I correctly understood...
you need associative array(dictionary) or two-dimensional array...

dictionaryArray.capx
Image
B
64
S
23
G
7
Posts: 1,483
Reputation: 10,827

Post » Mon Mar 28, 2016 3:54 pm

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.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

Post » Mon Mar 28, 2016 5:53 pm

You can pass the UID of the array and pick it in the function.
ImageImageImage
B
71
S
21
G
222
Posts: 3,652
Reputation: 124,733

Post » Mon Mar 28, 2016 9:01 pm

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 )

@gumshoe29: 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?'
B
4
Posts: 2
Reputation: 202

Post » Mon Mar 28, 2016 9:42 pm

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.
B
91
S
31
G
103
Posts: 5,241
Reputation: 67,768

Post » Tue Mar 29, 2016 3:22 pm

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.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Wed Mar 30, 2016 8:41 pm

Bombadin wrote:@gumshoe29: 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.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822


Return to How do I....?

Who is online

Users browsing this forum: duke2704, sebrosen and 28 guests