refering to struct variables inside a function

Started by Sackboy, Wed 15/05/2013 21:16:08

Previous topic - Next topic

Sackboy

let's say i create a struct:

Code: AGS
struct MyStruct{

int a;
String b;
DynamicSprite *c;

};


and some new variables for MyStruct:

Code: AGS
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
function MyFunction(? var, int sprite){

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

}


instead of:

Code: AGS
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?

Ryan Timothy B

#1
Code: ags

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

  MyStruct instance;  // creates the variable of the struct called instance
  instance.MyFunction();


SMF spam blocked by CleanTalk