Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: inmortal on Wed 25/02/2009 19:30:34

Title: Struct ... class
Post by: inmortal on Wed 25/02/2009 19:30:34
Hey guys!

I come from C++ and I wonder how to do object oriented programing in AGS.

The most similar thing I've seen is struct but you cannot define the class constuctor and other things like this:


class myclass{

//my private variables

public:
myclass(my parameters);//constructor
my function1(...);
};


Thanks!!
Title: Re: Struct ... class
Post by: DoorKnobHandle on Wed 25/02/2009 19:40:45
Just use struct like you'd use it in C++ and leave out the private, protected and public keywords because AGS does not (yet) support them.

Member functions also require the import keyword.

Take a look at this example and the syntax should become very clear if you are already knowledgeable in C++:


struct Window
{
      int width, height;
      int x, y;

      import function Init ( int x, int y );
      import String GetTitle ( );
};


However, custom structs can never be the type of members of another struct unfortunately. There are some further limitations but you'll find out about them soon enough.
Title: Re: Struct ... class
Post by: Pumaman on Wed 25/02/2009 20:32:25
AGS scripting isn't fully OO -- you can't have constructors, for example, and you can't pass custom structs by reference (only built-in types can be passed that way).

The "protected" keyword is supported to prevent external access to member variables, and AGS also has a "writeprotected" keyword which allows external code to read, but not write, the variable.

To declare a member function you must use the "import" keyword in the struct declaration in the .ash file, and then put the function body separately in the .asc file.
Title: Re: Struct ... class
Post by: Joe on Wed 25/02/2009 22:51:02
That's it, inmortal. As they say you have to use alternative keywords to pretend OOP.

Anyway I've got a question:

How can I access to a private variable from my code? Example:


//Script header

struct Maker{

protected bool S;
import bool IsMaking();

};




bool Maker::IsMaking(){

//code to determinate S value, My question: What do I have to do here to access S??

return S;
}



Title: Re: Struct ... class
Post by: Pumaman on Wed 25/02/2009 23:44:35
Use "return this.S" instead of just "return S" inside the function body.
Title: Re: Struct ... class
Post by: Joe on Wed 25/02/2009 23:57:49
Yes, that works for return, but inside the function, e.g.:


if(condition) S=value;

return this.S;


If I use this code it gives error :(:

if(condition) this.S=value;

return this.S;

Title: Re: Struct ... class
Post by: Khris on Thu 26/02/2009 00:01:24
Right. Which error...?
Title: Re: Struct ... class
Post by: Joe on Thu 26/02/2009 00:03:07
Undefined token 'this'
Title: Re: Struct ... class
Post by: Khris on Thu 26/02/2009 00:12:57
Could you post the complete code of the function?
Title: Re: Struct ... class
Post by: Joe on Thu 26/02/2009 00:17:44
Yes...


static function Disparo::Shoot(int charID, int speed, int oX,  int oY,  int fX, int fY){
 
  int clX, clY;
  float n;
  clX=fX-oX;
  clY=fY-oY;
  n = IntToFloat(clX^2+clY^2)/100.00;//100 is the square of 10(the laser misil module)
  if(n==0.00)n=1.00;//Prevent division by 0
  Weapon[charID].lX=oX+FloatToInt(IntToFloat(clX)/7.00);
  Weapon[charID].lY=oY+FloatToInt(IntToFloat(clY)/7.00);
  Weapon[charID].Speed=speed;
  this.S=true;
 
}


But I think it doesn't help very much...
Title: Re: Struct ... class
Post by: Khris on Thu 26/02/2009 00:46:47
You can't use 'this' in a static function.
'this' refers to the instance of the struct, but if it's static, there's only "the struct itself", not several instances.

As a workaround, you can declare the variable in the script, then add a function to the struct that gets/sets the value.

bool S;

static function Disparo::Set(bool a) {
  S = a;
}

static function Disparo::Get() {
  return S;
}
Title: Re: Struct ... class
Post by: Joe on Thu 26/02/2009 00:49:43
Ok, thanks all you!! ;D
Title: Re: Struct ... class
Post by: inmortal on Thu 26/02/2009 12:23:29
Right!

I'm quite pleased now, thanks!

;)
Title: Re: Struct ... class
Post by: Joe on Thu 26/02/2009 12:44:52
Ok two more questions... hmm maybe I'm being "like a pain in the neck"  but here they go:

Whtas the difference between writeprotected and readonly?

What's the diference between function and void? // Can function return any value?
Title: Re: Struct ... class
Post by: SSH on Thu 26/02/2009 13:02:30
Quote from: Joe Carl on Thu 26/02/2009 12:44:52
Whtas the difference between writeprotected and readonly?

Read only is read only: it really only applies to internal AGS values (i.e. AGS engine itself can change them but the scripter can't)

Writeprotected variables can only be modified via the this pointer, i.e. by non-static methods.

QuoteWhat's the diference between function and void? // Can function return any value?

function is an alias for "int". void returns nothing, function returns ints.