Arrays as a parameter?

Started by Intangible, Wed 24/08/2011 13:25:34

Previous topic - Next topic

Intangible

I can't seem to use the forum's search feature right now, so hopefully this isn't a duplicate question.

I'm having a terrible time with arrays. I've made a "book" GUI, and all I want is to pass an array of Strings (one per intended page) into a function, which then sets up the "book" GUI for you to see. I couldn't find any way to determine the length of an array, which already means that I need an additional parameter in the function for the page count (otherwise, the function won't know when it's on the last page). I've just finished setting that up, but now I can't figure out the syntax for actually passing an array as a parameter into the function. When I do this:

    String pages[5];
    pages[0] = "this is page 1";
    pages[1] = "this is page 2";
    pages[2] = "this is page 3";
    pages[3] = "this is page 4";
    pages[4] = "this is page 5";   
   
    ShowBook(5, pages);

...the compiler says that the array index wasn't specified. Is what I'm trying to do even possible, or is there a different way I should be doing this? The compiler didn't complain about the method parameter itself:

function ShowBook(int pageCount, String pages[], int startingPage)

Khris

Not sure if this is possible, there's no need to though.

Code: ags
// header

struct book {
  String pages[];
  int length;
};

import _book books[10];

// main script

str_book _book[10];
export _book;


Now you can set up a book like this:

Code: ags
  books[0].length = 5;
  books[0].pages = new String[book[0].length];
  books[0].pages[0] = "this is page 1";
  books[0].pages[1] = "this is page 2";
  books[0].pages[2] = "this is page 3";
  books[0].pages[3] = "this is page 4";
  books[0].pages[4] = "this is page 5";

Calin Leafshade

You can't pass staticly sized arrays but you can pass dynamic arrays

function myFunction(string[] theArray) { }

monkey0506

Structs cannot contain dynamic arrays, and the appropriate convention in AGS is to have the brackets after the array name (unless it's the return type of a function), not the array type. So it would be more like:

Code: ags
function myFunction(String theArray[]) { }


Although structs can't contain dynamic arrays, they can take them as a parameter to a member function, or return them:

Code: ags
struct MyStruct
{
  import void DoSomething(int array[]);
  import int[] DoSomethingElse();
}


Structs can also have static functions that take a dynamic array as a parameter, but not static functions that return a dynamic array. Extender methods work with dynamic arrays too.

Oh, and since there's not currently any way to get the size of a dynamic array, you should probably consider using one of these two methods: 1) pass the size of the array as a parameter to any functions using them, or 2) pad the array by 1 to store the size in the first index.

And also, keep in mind that dynamic arrays are internally treated as pointers, so they can be null, and reassigned using pointer logic. That can be either useful or problematic depending on what you might expect. If you're familiar with the terminology, they are passed by reference rather than value.

Intangible

I figured out why I couldn't call my original function, which had an array in the signature... I was trying to pass in a fixed-length array instead of a dynamic array. As soon as I made it a dynamic array, the method call worked just fine.

I'm still finding arrays tricky to use (I'm used to C++/C#), but I can work with what I have right now. Thanks for all your input; it's actually news to me that a struct can even have function.

SMF spam blocked by CleanTalk