Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Sackboy on Wed 15/05/2013 21:16:08

Title: refering to struct variables inside a function
Post by: Sackboy on Wed 15/05/2013 21:16:08
let's say i create a struct:

Code (AGS) Select
struct MyStruct{

int a;
String b;
DynamicSprite *c;

};


and some new variables for MyStruct:

Code (AGS) Select
MyStruct bla;
MyStruct blub;
MyStruct lala;


now i want to create a function where i am refering to these new variables like this:

Code (AGS) Select
function MyFunction(? var, int sprite){

var.a=var.a+1;
var.b="Hello!";
var.c=DynamicSprite.CreateFromExistingSprite(sprite);

}


instead of:

Code (AGS) Select
function MyFunction(int var1, String var2, DynamicSprite *var3 , int sprite){

var1=var1+1;
var2="Hello!";
var3=DynamicSprite.CreateFromExistingSprite(sprite);

}


is there a way to only refer to the first part of the new MyStruct variables instead of the whole variables?
Title: Re: refering to struct variables inside a function
Post by: Ryan Timothy B on Wed 15/05/2013 21:19:36
Code (ags) Select

struct MyStruct{
  int a;
  String b;
  DynamicSprite *c;
  import void MyFunction();
};

void MyStruct::MyFunction() {
  this.a = 10;
  this.b = "Test";
  this.c.Delete();
}


This should do it for you.

And your last example would only altar a copy of the variables, not the variables themselves.


Edit: I forgot to include saying this, even though the question is answered, I'll add it for anyone else approaching this issue who stumbles upon this thread. You would call the struct function like so:
Code (ags) Select

  MyStruct instance;  // creates the variable of the struct called instance
  instance.MyFunction();
Title: Re: refering to struct variables inside a function
Post by: Sackboy on Wed 15/05/2013 21:48:15
great, that's it! ;-D thank you!