Skip to content

Commit 027890d

Browse files
committed
Custom Classes Docs + other features docs
1 parent 110ed26 commit 027890d

7 files changed

Lines changed: 185 additions & 46 deletions

File tree

wiki/modding/scripting/custom-classes.md

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
author: Jamextreme140
3+
desc: This page explains how to use scripted enums for your mod!
4+
lastUpdated: 2025-09-12T08:53:13.034Z
5+
title: Enums
6+
---
7+
# Enums
8+
9+
Enums are a good choice if only a finite set of values should be allowed. It can be made inside the script you need it in.
10+
11+
Here is a basic Enum Script example:
12+
13+
```haxe
14+
enum TypeValue {
15+
NUMBER(n:Int);
16+
DECIMAL(d:Float, ?p:Int);
17+
CHARACTER(s:String);
18+
BOOLEAN(b:Bool);
19+
}
20+
21+
var type:TypeValue;
22+
23+
function create() {
24+
type = TypeValue.DECIMAL(10.1234, 2);
25+
26+
// You need to type the full enum field for each case
27+
// i.e. you can't type the enum field directly (limitation for now)
28+
switch(type) {
29+
case TypeValue.NUMBER(number):
30+
trace("number: " + number);
31+
case TypeValue.DECIMAL(decimal, precision):
32+
if(precision != null)
33+
trace("decimal: " + decimal + " | rounded decimal: " + roundDecimal(decimal, precision));
34+
else
35+
trace("decimal: " + decimal);
36+
case TypeValue.CHARACTER(char):
37+
trace("character: " + char);
38+
default:
39+
trace("unknown type");
40+
}
41+
}
42+
43+
44+
function roundDecimal(Value:Float, Precision:Int) {
45+
var mult:Float = Math.pow(10, Precision);
46+
return Math.fround(Value * mult) / mult;
47+
}
48+
```
49+
50+
Supports Enum matching with arguments for switch statements (for real and scripted enums).
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
author: Ne_Eo, Frakits & Jamextreme140
3+
desc: This page explains how to create custom classes for your mod!
4+
lastUpdated: 2025-09-12T09:36:28.820Z
5+
title: Custom Classes
6+
---
7+
# Custom Classes
8+
9+
Custom classes can be made either inside the script you need it in or you can make a script file corresponding with the name of the class in ``./source``, and using <syntax lang="haxe">import</syntax> to import it.
10+
11+
Here is a basic Song Script code that uses it:
12+
```haxe
13+
package; // Allow `package` declaration. Ignored by the interpreter.
14+
15+
class SpecialSprite extends FlxSprite {
16+
public static function getSprite(v:String) {
17+
return new SpecialSprite(v);
18+
}
19+
20+
public var customValue(default, set):String = null;
21+
private function set_customValue(v:String):String {
22+
if(v == "powerful")
23+
trace("I'm powerful!");
24+
loadGraphic(Paths.image(v));
25+
return customValue = v;
26+
}
27+
28+
public function new(customValue:String) {
29+
super(200, 400, null);
30+
this.customValue = customValue;
31+
//other code stuff
32+
}
33+
34+
public override function update(elapsed) {
35+
super.update(elapsed);
36+
}
37+
}
38+
39+
function create() {
40+
var spr = SpecialSprite.getSprite("powerful");
41+
add(spr);
42+
}
43+
```
44+
45+
## Particularities
46+
As of writing this, this system is limited and also presents some defects. For example:
47+
- You cannot extend FlxGroups or other typed classes *(the ones that end with a ``<T>``)*. This will be implemented in the future.
48+
- Compiled Classes that do not override a function in their code cannot have that function overridden by custom classes. For example, you can't override the `draw` method in a custom class that extends <syntax lang="haxe">FlxParticle</syntax>, because <syntax lang="haxe">FlxParticle</syntax> does not override `draw`. This will be fixed in the future.
49+
- private variables and functions has not effect. They can be accessed as public variables.
50+
- You can only extend a class that is in the packages, `flixel`, `funkin` and `modchart`.
51+
52+
## More HScript Features
53+
54+
- [Enums](./enums.md)
55+
- [Property Fields](./property-fields.md)
56+
- [Static Extension](./static-extension.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
author: Jamextreme140
3+
desc: This page explains how to use property fields for your mod!
4+
lastUpdated: 2025-09-12T09:49:51.453Z
5+
title: Property Fields
6+
---
7+
# Property Fields (get/set variables)
8+
9+
Next to variables, properties are the second option for dealing with data on a class. However, unlike variables, they offer more control of which kind of field access should be allowed and how it should be generated.
10+
11+
Here is a basic Property Field usage example:
12+
13+
`./songs/script1.hx`
14+
15+
```haxe
16+
public var myvar(default, set):Int;
17+
18+
function set_myvar(val:Int):Int {
19+
if(val > 10) return myvar = val;
20+
return val;
21+
}
22+
```
23+
24+
`./songs/[SONG NAME]/scripts/script2.hx`
25+
26+
```haxe
27+
function create() {
28+
myvar = 20; // This will trigger the set call stored on the field.
29+
}
30+
```
31+
32+
More information about property fields [here](https://haxe.org/manual/class-field-property.html).
33+
34+
## Particularities
35+
As of writing this, this system presents some limitations. For example:
36+
- `(get, null)` or `(null, set)` has no effect for writing nor reading, it acts like (get, default).
37+
- Calling the get/set function directly will trigger that function twice due to accessing the property field on call (if is a real variable, like `(get, null)` or `(default, set)`). For example, calling directly `set_myvar` will call the same function again when tries to write on `myvar`. Avoid doing this unless you have a full property field (a non-real variable, i.e. `(get, set)` or `(get, never)`). This will be fixed in the future.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
author: Jamextreme140
3+
desc: This page explains how to use static extension for your mod!
4+
lastUpdated: 2025-09-12T09:50:05.405Z
5+
title: Static Extension
6+
---
7+
# Static Extension
8+
9+
Static extensions can be a powerful tool which allows augmenting types without actually changing them.
10+
11+
Here is a basic Static Extension example:
12+
13+
```haxe
14+
using StringTools;
15+
16+
class IntExtender {
17+
static public function triple(i:Int) {
18+
return i * 3;
19+
}
20+
}
21+
22+
// need to create/import the custom class
23+
// before setting the extension (limitation for now)
24+
using IntExtender;
25+
26+
var str = " Hello World! ";
27+
28+
function create() {
29+
trace(str.trim()); // "Hello World!"
30+
trace(12.triple()); // 36
31+
}
32+
33+
```
34+
35+
More information about static extension [here](https://haxe.org/manual/lf-static-extension.html).

wiki/modding/scripting/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
author: Frakits
33
desc: This page explains how to script the engine
4-
lastUpdated: 2025-01-08T23:47:04.206Z
4+
lastUpdated: 2025-09-12T09:36:28.820Z
55
title: Scripting
66
---
77
# Scripting
@@ -85,6 +85,6 @@ And if you wanna go advanced, follow the rest of the articles here:
8585
- <a href="./3d-rendering.md">3D rendering</a>
8686
- <a href="./hxvlc.md">Using hxvlc for videos</a><br><br>
8787
- <a href="./scripted-assets-libraries.md">Scripted Assets Libraries</a>
88-
- <a href="./custom-classes.md">Custom Classes</a>
88+
- <a href="./custom-classes/index.md">Custom Classes</a>
8989
- <a href="./ndll-scripting.md">NDLL Scripting</a>
9090
- <a href="./custom-transitions.md">Custom Transitions</a>

wiki/wiki.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
["hxvlc", "Using hxvlc for videos"],
3737
["custom-options", "Custom Options"],
3838
["custom-controls", "Custom Controls"],
39-
["custom-classes", "Custom Classes"],
39+
["custom-classes/index", "Custom Classes", [
40+
["enums", "Enums"],
41+
["property-fields", "Property Fields"],
42+
["static-extension", "Static Extension"]
43+
]],
4044
["3d-rendering", "3D rendering - UNFINISHED"],
4145
["script-calls", "All of the script calls - UNFINISHED"],
4246
["script-snippets", "Useful script snippets for modders - UNFINISHED"],

0 commit comments

Comments
 (0)