Getting array index in struct function (SOLVED)

Started by Akumayo, Wed 09/08/2006 20:24:13

Previous topic - Next topic

Akumayo

Is there a way to access the array index a function was called from in a struct?  Like if you have:

Code: ags

//Header
struct My_Struct {
  import my_function();
};


Code: ags

//Main
My_Struct Mine[10];


Let's say that my_function() does something depending on which Mine[index] it was called from.  We would call it like this:

Code: ags

//Within some function
Mine[4].my_function();


But then, how would my_function know which index it was called from?  I know there must be a way to access this, because character[index] and various others have functions which access the index called from.
Does anyone know how to access this index number?

Thanks in advance.

-Regards, Akumayo
"Power is not a means - it is an end."

DoorKnobHandle

The only way to do this that I can think of right now, is through a simple parameter to your function.

For example:

Code: ags

// header
struct my_struct
{
   import my_function ( int index );
};


Code: ags

// main
my_struct mine[10];


Code: ags

// within some function
mine[4].my_function ( 4 );

// or
mine[i].my_function ( i );

Akumayo

Well, I realized that workaround, but it seems odd that you don't have to supply parameters like this when you change a character's view (or anything else):

Code: ags

//Normal
character[SOME_GUY].ChangeView(4);

//Parameter
character[SOME_GUY].ChangeView(SOME_GUY, 4);


I'm currently using a parameter as a workaround, but I think, based on the example above, that there is a hidden variable that would get the index the function was called from.
"Power is not a means - it is an end."

Besh

I'm sorry, but I'm (99.99%) sure that there is no way to do that.Ã,  :'(

Character (but also Object, Region, ecc..) isn't a script struct but a "built-in struct" that comes with the script and has different access rules.

You could use a plugin struct.function (that has different access rules from the script function) where the first argument passed to the function is the pointer to the obj that call the function.

"Spread our codes to the stars,
You can rescue us all"
- Muse

monkey0506

#4
What you want is the this keyword.

It won't allow you direct access to the value of the index, but you could do something like this:

Code: ags
struct My_Struct {
  int Value;
  import function my_function();
  };

function My_Struct::my_function() {
  this.Value = 5;
  }


Or some-such. If you really need the array index, you could add an ID parameter that would hold it:

Code: ags
struct My_Struct {
  int ID;
  import function my_function();
  };

My_Struct Mine[10];

// game_start
int i = 0;
while (i < 10) {
  Mine[i].ID = i;
  i++;
  }


For added security, you could do this as well so that the ID parameter is initialized invisibly and the user can't edit it:

Code: ags
struct My_Struct {
  writeprotected int ID;
  import function my_function();
  import static function initID(); // $AUTOCOMPLETEIGNORE$
  };

My_Struct Mine[10];

function My_Struct::initID() {
  int i = 0;
  while (i < 10) {
    Mine[i].ID = i;
    i++;
    }
  }

// game_start
My_Struct.initID();


"// $AUTOCOMPLETEIGNORE$", does as it says, and makes autocomplete ignore the line. Although the user could still call the function, it won't autocomplete, so they probably won't know it's there.

[EDIT:]

But, just to clarify, even if you use the ID property, you still use this (this.ID) to access the array index from within your functions.

Akumayo

Thanks for the tips monkey.  I've never heard of the 'this' keyword until now, I'll have to look it up and find out more.
The ID idea I had not thought of yet, it seems far more like what I'm aiming for than my current workaround.
So, thanks again.
"Power is not a means - it is an end."

monkey0506

#6
this is a pointer to the object calling the function. So basically, if you type:

Mine[2].my_function()

Then inside of the "my_function" definition you put:

this.Value = 5;

As in the above example, then "this" is a pointer to "Mine[2]".

[EDIT:]

this is also a temporary pointer which goes out of scope (i.e., you can't use it anymore because it doesn't exist) at the end of the function definition (the closing brace). It is automatically assigned when the function is called (i.e., when you call "Mine[2].my_function()", this is assigned to point to Mine[2]), and is read-only (you can't reassign it to point to anything else).

Akumayo

Ahh, I think I understand.Ã,  So 'this' re-directs to Mine[index]?Ã,  If that is the case, then it fits my needs perfectly.Ã,  I was attempting to get the index number to use it in the function as Mine[index_ID_Gotten].
Thanks again!

EDIT:  Tested, it works perfectly!
"Power is not a means - it is an end."

monkey0506

Yes, 'this' "redirects" to Mine with whatever index you used to call the function. Just curious though, what do you need the index for? Perhaps I could offer more assistance (if you need it, you edited your post and it seems you've gotten it working).

Akumayo

I'm scripting something that assigns a few variables from one struct to variables in another struct, and the function is imported into one of the structs (which is called as an array).  After calling it, I wanted the engine to automatically find the index, rather than the scripter having to type it in.  As you showed, an ID parameter (which I didn't think of) would've worked around this.  However, I thought that surely there was another way, since, like I said, the 'character' and other functions need no scripter input as to their index number.  Now, thanks to you, I've learned how to use the 'this' keyword ^_^
"Power is not a means - it is an end."

Besh

This is also an important lesson:

sometimes 99.99% isn't a good percentageÃ,  ;D

Good work man!!!
"Spread our codes to the stars,
You can rescue us all"
- Muse

SMF spam blocked by CleanTalk