Pass an array to a function?

Started by aedwards00, Mon 15/03/2010 15:05:12

Previous topic - Next topic

aedwards00

Maybe I'm being stupid but is this possible in AGS? I've tried the method I'm used to from C/C++ by just passing the address of the first element but this gives me a type mismatch error. Also, it's not possible to create pointers to ints? I've looked in the manual for arrays and pointers but neither seem to have too much useful information.

About my closest attempt so far goes like this

Code: ags

// In globalscript.asc

function Init_Safe(int *newcombination)
{
    DO STUFF HERE
}


// in room script file

import function Init_Safe(int*);
int combination[6];

function room_AfterFadeIn()
{
    Init_Safe(combination);
}



This tells me that I can't create a pointer to an unmanaged type.
I have also tried using "int combination[]" in the function declaration to no avail.

Also, is it not possible to initialize a whole array when you declare it? It seems a bit tedious to do each element manually.


Thanks for any help

Crimson Wizard

This works for me in my own game:

in header:
Code: ags

import function SG_InitEnemyRails(int Count, int Ys[], bool FirstForMelee = true);


in source:
Code: ags

function SG_InitEnemyRails(int Count, int Ys[], bool FirstForMelee)


usage:
Code: ags

int Ys[] = new int[6];
...
SG_InitEnemyRails(6, Ys, true);

aedwards00

#2
Perhaps you have some modules that I dont?
The AGS manual says: on pointers - "there is no new or delete keyword"
on importing functions -
"import function my_function_name (parameters);

Where my_function_name is the name of the global script function, and parameters is a list of the TYPES ONLY of the parameters it takes".

Having said that, I'll give it a go :p

EDIT:  Using your method i get the following error

"Failed to save room room1.crm; details below
room1.asc(3): Error (line 3): Expected integer value after '='"

The line in question being
Code: ags

int combination = new int[6];


Crimson Wizard

Erm... perhaps you are using older version of AGS? You should tell which one.
My code should work for AGS 3.+, I believe.

aedwards00

Sorry missed that out above, version 3.1 SP1

monkey0506

#5
You are correct that it is not possible to create pointers to the basic data type int. The only types you can create pointers to are AGS's managed types which are easily identified as the complex types that start with a capital letter such as Button, Character, DrawingSurface, DynamicSprite, File, GUI, GUIControl, Label, etc.

Not all of the managed types are intended to have pointers. For example Game, Room, String, and System are all managed types that you can't create a pointer to (rather, you can create a pointer (except for String which internally uses an automatic pointer already) but you can never assign it a value so the pointer will always be null and thereby useless to you).

That doesn't really answer your primary concern though, so on to your question.

You can't pass a statically sized array as a function parameter in AGS. You can pass a dynamic array as a function parameter however. It is important to note though that since AGS does not currently have a way to retrieve the size of a dynamic array you will be dependent on an additional size parameter (which if the user specifies a size that is too large will crash the game).

The way you can do that is like this:

Code: ags
// GlobalScript.asc

function Init_Safe(int newcombination[]) { // we will assume that the size will always be six for simplicity's sake
  // DO STUFF HERE
}

// roomX.asc

import function Init_Safe(int[]);

int combination[];

function room_FirstLoad() { // make sure this is properly linked in the room's Events pane
  combination = new int[6];
  Init_Safe(combination);
}


Wow..4 replies while I was typing..I guess that's what I get for wondering off and doing other things in the mean time. :P

Edit: Regarding the new keyword it is reserved for use in respect to dynamic arrays and cannot be used for creating new instances of a type (other than the arrays of course). There is no delete keyword, if you want to get rid of a dynamic array you can set the array to null and AGS's garbage collection will clean it up automatically:

Code: ags
combination = null;


Oh, and just FYI aedwards, I wanted to point out that the function keyword is really just #define'd as int (with a special exception regarding return). If you ever need to return a non-integer value you can just replace "function" with the return type you need.

Crimson Wizard

Quote from: aedwards00 on Mon 15/03/2010 15:35:24
EDIT:  Using your method i get the following error

"Failed to save room room1.crm; details below
room1.asc(3): Error (line 3): Expected integer value after '='"

The line in question being
Code: ags

int combination = new int[6];


You must write this:
Code: ags

int combination[] = new int[6];


You missed [] after variable name. That is - you declared simple integer instead of array and tried to apply array reference to it.

aedwards00

Aha thanks both, I think I pretty much get it now, you'll hear from me soon if not :)

monkey0506

#8
Note that you only use the brackets when you're defining/declaring/accessing an index in the array. So, just a few examples:

Code: ags
import int combination[]; // import dynamic array

int combination[]; // define dynamic array (outside a function or inside a function without initial value)

export combination; // export dynamic array (so it can be imported to other scripts)

int combination[] = new int[6]; // define a dynamic array (inside a function, with initial value)

combination = null; // delete dynamic array

combination = new int[7]; // assign a new array into an existing dynamic array

function display_array(int arrayCount, int array[]) {
  if (array == null) AbortGame("WTF? You gave me a null array you jerk!");
  if ((arrayCount < 0) || (arrayCount > 1000000)) AbortGame("Invalid array size specified.");
  int i = 0;
  while (i < arrayCount) {
    Display("array\[%d]: %d", i, array[i]);
    i++;
  }
}

display_array(combination); // displays the contents of the array

Crimson Wizard

Monkey, you should give all your examples to those guys, who write AGS Book  ;D

SMF spam blocked by CleanTalk