Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: TheMagician on Sun 29/07/2007 17:13:21

Title: Dynamically calling a struct function (SOLVED)
Post by: TheMagician on Sun 29/07/2007 17:13:21
Hi everybody.

I declared a struct called "Column" in my Module header :


struct Column {
  int x_pos;
  int y_pos[9];
  int weight_total;
  import function update_weight();
 
};


In my Module script I declared the function "update_weight()":


function Column::update_weight() {
  this.weight_total = 0;
}


In my main room script I declared 10 instances of my struct:


Column col_0;
Column col_1;
Column col_2;
...


And here is my problem:

In the room script I want to call


col_X.update_weight();


where "col_X" is the number of the column that the player has selected.
So this should be dynamic. If the player selected column "0" then it should be:


col_0.update_weight();


If the user had selected column "1" it should be:


col_1.update_weight();


and so on.

Is there a way of scripting that without doing:


int selected_column;
if (selected_column == 0) {
  col_0.update_weight();
}
else if (selected_column == 1) {
  col_1.update_weight();
}
...


I tried something with pointers but as soon as I write


Column* column_selected;


I get an error "Cannot declare pointer to non-managed type".

Thank you for reading.
Any help is greatly appreciated.

Many thanks in advance,
Stefan
Title: Re: Dynamically calling a struct function
Post by: SSH on Sun 29/07/2007 17:16:57
How about:


Column col[3];

col[selected_column].some_function();
Title: Re: Dynamically calling a struct function
Post by: TheMagician on Sun 29/07/2007 17:40:08
...  :-\   ok, that was embarrassing.

And it works :)
Thanks SSH!