Basically it is using the syntax of C# and Java for declaring and using classes.
The main motivation for this feature:
- it will make it easier to port code from Java and other similar languages
- it makes it possible to write code in more object-oriented manner
Currently some limitations:
- there is no destructors. The reason for this is that the lifespan of a class instance is determined by the garbage collector and as such it may be destroyed after the code has finished running.
Attached is a ZGE build and also a demo project that use classes.
Please try it and see what you think about it. I'm sure there are bugs too so don't hesitate to report any crashes or weird syntax errors.
Here is an example of a class-declaration:
Code: Select all
class TPlayer {
//This is the data of the class (a.k.a. "fields"). When they are declared private then only the code in the class can access them.
private int score, lives;
private int scoreRemainingToNextExtraLife = 10000;
//This field is not private so outside code can access it
model theModel;
TPlayer(int initialLives) { //This is a constructor. It has the same name as the class and no returntype.
lives = initialLives;
}
TPlayer() { //There can be several constructors as long as they have different amount of arguments
lives = 3;
}
//This is a function in the class (a.k.a. a "method").
void giveScore(int amount) {
score += amount;
scoreRemainingToNextExtraLife -= amount;
if(scoreRemainingToNextExtraLife<0) {
lives ++;
scoreRemainingToNextExtraLife += 10000;
}
}
inline int getLives() { //Methods can be declared inline
return lives;
}
}
Code: Select all
TPlayer p = new TPlayer(4);
p.theModel = null; //non-private fields can be accessed directly
p.giveScore(100); //call a method
trace(intToStr(p.getLives()));
Code: Select all
class TBase {
virtual void method1() { //mark methods "virtual" if they should be overridden
}
}
class TChild : TBase { //specify ": TBase" to inherit from TBase class
override void method1() { //mark with "override" when overriding base class method
//...
}
}