-
Notifications
You must be signed in to change notification settings - Fork 217
Description
This one is weird, so I'm going to need you to bear with me a little bit...
The plan:
I want to integrate Cppia scripts into my level editor. The goal is to be able to assign a class to any instance in my level editor via the UI, and write the class as Haxe code, then compile it all into a cppia module that is loaded alongside the level (kind of like a ghetto SWF). I've already implemented this, and it actually works, but I've run into a bug which seems to be coming from Cppia.
These classes are being derived from a parent class which is defined in the host (a FlxSprite, from HaxeFlixel).
The problem:
The behavior I'm seeing from the Cppia script doesn't make any sense. For example, I am overriding one function which is only ever called by the Host once after the class is instantiated via Type.createInstance. There is no other place in the entire program where this function gets called again, however at runtime I find that this function is actually being called every single frame, as if it were part of the update function.
But what's even weirder is that if I change the order that the function is declared in the parent class (in the host), then the behavior changes. Now, instead of being called every frame, it gets called in lieu of another completely unrelated function.
To illustrate:
class MyHostSprite extends FlxSprite {
public function oninit(){
//do something
}
public function update_sprite(){
//do something
}
}
------
class MyCppiaSprite extends MyHostSprite {
override public function oninit(){
//do something else
}
override public function update(){
update_sprite();
//...
}
}
If I run the above code, I get oninit() called every frame, as if it were in the body of update. However, if I change the host class to this:
class MyHostSprite extends FlxSprite {
public function update_sprite(){
//do something
}
public function oninit(){
//do something
}
}
Then oninit doesn't get called each frame, it instead gets called in lieu of another function. Note that all I did was change the order that the function appears in the class body. Doing this on the cppia side doesn't do anything though, it appears to be a host-only thing.
Maybe looking at the call stack might show what's going on, but I'm not sure how I would do that.
I omitted a lot of code for brevity. The main things I think that are relevant though are the following:
-
The cppia script extends a class defined in the host, and through
export_classes, the host class definition is not included in the cppia script. -
I'm "loading" the cppia script using
cpp.cppia.Host.run, and then usingType.resolveClass/Type.createInstanceto instantiate classes. There also isn't a main function in the cppia script, although even when I add one I get the same behavior. -
The instance of the cppia-defined class is then passed to functions that expect an instance of the parent class. So member functions defined in the parent are called on the cppia instance.
-
I am not using the standard openfl build tool since, as far as I know, it doesn't support building cppia scripts. I'm invoking the Haxe compiler manually to build the cppia script, and excluding openfl/flixel stuff using
-D dll_importand--macro exclude.
Reproducing:
I've reproduced this issue with my project on Haxe versions 3.4.7, 4 rc2, and 4 rc3 on Ubuntu and Windows 10, and Hxcpp 4.0.8 and 4.0.19
If it'll help fix this, I'm open to uploading this entire project. However, it's part of a custom asset pipeline that isn't fully finished yet, requires some environment configuration, and is designed to work with a commercial product. So it's messy to say the least, but it so far can reproduce the issue on two different machines.