Hello, everyone!
Over the past few weeks I've been working on a game using
Kha.
The game is mainly meant to be played on Android / iOS, however I've been using the Html5 target for development.
This is my first Haxe project, and I've run into a couple questions.
- How can named, optional arguments be done efficiently?
I've been creating a ui framework for this game, and I've ended up with many anonymous config objects being passed around:
public function new(?config: {
?layer: Int,
?w: Size,
?h: Size,
?left: Position,
?right: Position,
?top: Position,
?bottom: Position
}) {
if (config != null) {
if (config.layer != null) {
layer = config.layer;
}
if (config.w != null) {
w = config.w;
}
if (config.h != null) {
h = config.h;
}
if (config.left != null) {
left = config.left;
}
if (config.right != null) {
right = config.right;
}
if (config.top != null) {
top = config.top;
}
if (config.bottom != null) {
bottom = config.bottom;
}
}
}
This works alright, aside from the potential performance hit.
The other problem is that classes which extend this class will need to include all of its config parameters.
Extending class:
public function new(?config: {
?drawable: Drawable,
?sOff: Float,
?xOff: Float,
?yOff: Float,
?wOff: Float,
?hOff: Float
}) {
if (config != null) {
if (config.drawable != null) {
drawable = config.drawable;
}
if (config.sOff != null) {
scaleOffset = config.sOff;
}
if (config.xOff != null) {
xOffset = config.xOff;
}
if (config.yOff != null) {
yOffset = config.yOff;
}
if (config.wOff != null) {
widthOffset = config.wOff;
}
if (config.hOff != null) {
heightOffset = config.hOff;
}
}
super(config);
}
So far this paradigm hasn't worked out too bad, but I figured I'd check with you guys to see if there was a better way.
I'd prefer to avoid anonymous structures for performance reasons, but perhaps I'll just hack together a macro to automatically inherit parents' config parameters and be done? Any thoughts?
- How can I manage memory allocation?
I don't have much history in memory management, but I'm starting to realize its importance on this project.
Currently in my game (running on Android), if I go to a scene (think Menu -> Settings), the entire scene is created (tables, buttons, widgets, etc.). This process takes some time when memory hasn't been allocated yet (as far as I can tell), however once I've gone to that scene, then back to the previous, I can now load this new scene very quickly.
So,
Menu -> Settings (5 seconds)
Settings -> Menu (instant)
Menu -> Settings (instant)
What could be the cause of this? My only assumption is that it's due to memory having been allocated already for all the Settings screen's widgets.
- Is there a way for a function to accept ADT's without enums?
I'd like to be able to create a function which accepts either a String or an Int (for example)
However, I don't want to have to indicate which type I'm passing it with an enum:
enum Param {
INT(int: Int);
STRING(string: String);
}
function func(param: Param): Bool {
switch Param {
case INT(int): if (int == 0) return true;
case STRING(string): if (string == "test") return true;
}
return false;
}
While this is doable, the syntax is far too verbose. Perhaps a macro could automatically generate something like this. (Though it would have to modify calls to the function, since haxe doesn't support function overloads)
Yes, I know some of this can be done with optional parameters, but this isn't perfect. (a function could be passed both optional parameters or none) Also, I'd prefer to not have to null check all optional parameters.