Skip to content

Extending Haxe classes in Hscript

ashley edited this page Aug 7, 2026 · 1 revision

To make a Haxe class extendable for scripting, simply make an extension of it and implement the insanity.IScripted interface!
Here's a simple example...

class Animal { // your base class' fields and methods go here...
	var name:String;
	
	public function new(name:String) { this.name = name; }
	
	public function vocalize():Void {}
}

class ScriptedAnimal extends Animal implements insanity.IScripted {
	/*
	this makes a template class that will allow HscriptInsanity to recognize Animal as extendable in scripted classes!
	
	do note ScriptedAnimal doesn't necessarily have to be located in the same module as Animal,
	so you can make a single module including all of your scripted classes!
	
	the template class must not have any fields or methods of its own as this might have unexpected results.
	*/
}

class Dog extends Animal { // then, this should be possible for a scripted class!
	public override function vocalize():Void { trace('woof'); }
}

Limitations

  • If you attempt to extend a class that doesn't have an IScripted template, the error "Class [class name] can't be extended for scripting" will be thrown.
  • Abstract classes (not to be confused with Abstracts) are not currently supported
    • This is a planned feature and will be added in subsequent updates!
  • Properly supporting type parameters in methods is a hard task and, in case the code can't infer the types, the field will be "omitted" and overriding it in a scripted class will be impossible (the error "Field [field name] is unexposed and cannot be overridden" will be thrown if you attempt this).
    • However, you should still be able to call the method.
    • if you know a way to fix this, please consider making a pull request!

Clone this wiki locally