Struct ... class

Started by inmortal, Wed 25/02/2009 19:30:34

Previous topic - Next topic

inmortal

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:

Code: ags

class myclass{

//my private variables

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


Thanks!!

DoorKnobHandle

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++:

Code: ags

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.

Pumaman

#2
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.

Joe

#3
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:

Code: ags

//Script header

struct Maker{

protected bool S;
import bool IsMaking();

};



Code: ags

bool Maker::IsMaking(){

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

return S;
}



Copinstar © Oficial Site

Pumaman

Use "return this.S" instead of just "return S" inside the function body.

Joe

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;

Copinstar © Oficial Site

Khris

Right. Which error...?

Joe

Undefined token 'this'
Copinstar © Oficial Site

Khris

Could you post the complete code of the function?

Joe

Yes...

Code: ags

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...
Copinstar © Oficial Site

Khris

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.

Code: ags
bool S;

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

static function Disparo::Get() {
  return S;
}

Joe

Copinstar © Oficial Site

inmortal

Right!

I'm quite pleased now, thanks!

;)

Joe

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?
Copinstar © Oficial Site

SSH

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.
12

SMF spam blocked by CleanTalk