Psych Engine Logo
T-Bar Engine - Documentation

A lot of this information originated from Codename Engine's wiki page on custom classes.

Custom Classes - HScript

HScript-Improved allows for the ability to make custom classes in your haxe scripts.

Note: while you do have the ability to make custom classes in HScript, it is far more limited than making classes in source code. A list of limitations will be provided at the bottom of this section.

Making custom classes in HScript is fairly simple, and you can either make it in the same script that you plan to use it in or you can add it to a separate script. Both will be covered here.

Lets make a simple FunkinSprite class that automatically sets the sprite's antialiasing based on your settings. To do this, we need to extend FlxSprite, as shown here:

Creating a Custom Class

Please note that all custom values added to the created class need to be public in order to be accessable. It is also important to note that if you extend a class, you MUST have the same arguments for it's constructor, which is why the "x", "y". and "graphic" arguments remained the same.

Now let's actually use it:

Creating a Custom Class

Now when you run the script, your sprite will automatically set it's antialiasing depending on if you have antialiasing turned on.

Creating a Custom Class

If you don't want your sprite to automatically switch it's antialiasing, simply set the 4th argument in it's constructor to "true". And if you ever want to access whether the object has "respectAntialiasing" enabled, you can do "spr.respectAntialiasing" to get it's property.

Making Custom Class Available

If you made your custom class in another script or in a library script, it can be made available by putting this in your script:

Making a Custom Class Available

You can then import the class using the `import` keyword (script libraries) or using the `@:include` metadata.

Final Classes

When making a custom class that you don't want other classes extending, you can simply mark your class as final like so:

Creating a Final Class

Also note that source classes can be blocked from being extended by a scripted class by marking a class with the @:noCustomClass metadata.

Static Values

Starting From T-Bar Engine version 2.0, static functions and variables can be added to custom classes:

Creating a Final Class

They act exactly as they do in regular haxe.


Limitations

As mentioned earlier, HScript custom classes have a lot of limitations compared to source code classes, and provides a lot of limitations/defects, including (but not limited to):

  • You cannot extend typed classes (FlxTypedGroup and FlxTypedSignal as an example).
  • Classes that extends a class needs to have the same arguments on the constructor as the class it overrides. Same when creating the instance of the class.
  • Classes that do not override a function cannot have that function overridden by a custom class. For example: if you make a custom class that extends FlxText, you cannot override the "update" function in it since FlxText does not override that function itself.
  • A variable/function MUST be public in order to be accessed, both in or outside the class.
  • Private variables/functions are not supported, they instead act as public variables/functions.
  • Classes that can be extended have to be in the following packages: flixel, backend, shaders, psychlua, options, and objects.
  • Older Version Limitations

    For T-Bar Engine versions under 2.0, there are more limitations to hscript classes. This is due to older versions using a deprecated branch of hscript-improved with the old custom class system. Extra limitations for these versions include (but not limited to):

  • While you can extend classes with custom classes, you CANNOT extend custom classes using a custom class.
  • Static variables/functions are not supported.
  • Marking a class as final is not supported.

  • Information for Source code mods

    If you are making a source code mod, it is recommended to remove the "CUSTOM_CLASSES" flag in the Project.xml, as it reduces compile time. Only keep this enabled if your source code mod uses HScript custom classes in some way. If you are extending classes, edit the "ALLOWED_CUSTOM_CLASSES" variable in "source/hscript/Config.hx" to only allow the packages that contain the classes that you are going to extend.