Skip to content

Commit ecc71ae

Browse files
committed
Refactor modules and start on that tutorial series
1 parent 976f568 commit ecc71ae

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

assets/content/cookbook/Expert/01.ScriptedModules.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Most articles about scripting will cover basic functionality of overriding alrea
66

77
A module is a class which runs very similarly to global scripts in other game engines. They are basically mini-states that run parallel to the original state.
88

9+
You can use these modules to create entirely custom functionality, examples include:
10+
- Custom HUDs
11+
- Custom Menu's (Mod Menu, Pause Menu, Main Menu, etc)
12+
- Retroactively affect gameplay (Creating chances of sprites appearing on screen, and adding your own version of ghost tapping. Among other things)
13+
914
## Creating a Module
1015

1116
```haxe
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
[tags]: / "expert,hscript"
2+
3+
# Mod Pacakges
4+
5+
As your mods complexity increases, with many different scripts (modules, custom objects/states, etc) it is vital to eventually organize this code. You can do this in the same way as regular haxe: **Package names**.
6+
7+
```haxe
8+
package kade.hex.modules;
9+
10+
import funkin.modding.module.Module;
11+
12+
// ...
13+
14+
class Hex_Hud extends Module
15+
{
16+
// ... some code here ...
17+
}
18+
```
19+
20+
As seen in the code snippet, the module 'Hex_Hud' has the package of 'kade.hex.modules' which will allow for better organization of your code, and to keep stack traces nice and clean.
21+
22+
# Referencing modules/scripted classes
23+
24+
Referencing other modules (and also scripts), can be done by using the ModuleHandler, or by creating a scripted version of that object.
25+
26+
```haxe
27+
28+
import funkin.modding.module.ModuleHandler;
29+
30+
// ...
31+
32+
public override function onStateCreate(event:ScriptEvent):Void
33+
{
34+
// The first argument is the 'id' of the module (the one used in the constructor *OF* that module)
35+
var hexHud = ModuleHandler.getModule("Hex_Hud");
36+
37+
// ... some code using it, the variable acts like a singleton of that module.
38+
}
39+
40+
// ...
41+
```
42+
43+
# Instantiating your own custom scripted class
44+
45+
Since modules are created (instantiated) by the game, they are already accessible via the game itself. But if you want to create your own scripted classes, you can do a couple of things.
46+
47+
## Game provided class
48+
49+
If the game already provides it, and your script extends it. It is very simple:
50+
51+
```haxe
52+
// The FlxTypeSpriteGroup class (base game provided)
53+
54+
import funkin.modding.base.ScriptedFlxSpriteGroup;
55+
56+
// ...
57+
58+
class HEXDialogueBox extends FlxTypedSpriteGroup
59+
{
60+
// ... dialogue code
61+
}
62+
```
63+
64+
Then you can simply create this class in a song like so:
65+
66+
```haxe
67+
import funkin.play.song.Song;
68+
import funkin.play.PlayState;
69+
import funkin.modding.base.ScriptedFlxSpriteGroup;
70+
import funkin.play.PlayStatePlaylist;
71+
72+
class RamSong extends Song {
73+
var cutscenePlayed = false;
74+
75+
public override function onCountdownStart(event:CountdownScriptEvent):Void {
76+
if (!PlayStatePlaylist.isStoryMode)
77+
cutscenePlayed = true;
78+
79+
if (!cutscenePlayed) {
80+
event.cancel();
81+
82+
PlayState.instance.camHUD.visible = false;
83+
playDialogue();
84+
}
85+
super.onCountdownStart(event);
86+
}
87+
88+
89+
public function playDialogue() {
90+
var hexDiag:HEXDialogueBox = ScriptedFlxSpriteGroup.init("HEXDialogueBox"); // this line with instantiate this class (based on its name, or name and package)
91+
92+
// ... setup dialogue code
93+
}
94+
}
95+
```
96+
97+
## Custom class
98+
99+
If you created the class yourself, you can simply instance itself with the 'new' operator.
100+
101+
```haxe
102+
package kade.hex.states;
103+
104+
import funkin.ui.MusicBeatState;
105+
106+
class HexMainMenu extends MusicBeatState {
107+
108+
// ... surely there is code here
109+
}
110+
```
111+
112+
```haxe
113+
import kade.hex.states.HexMainMenu;
114+
import flixel.FlxG;
115+
116+
// ...
117+
118+
public function someFunction() {
119+
var mm:HexMainMenu = new HexMainMenu();
120+
FlxG.switchState(mm);
121+
}
122+
123+
// ...
124+
```
125+
126+
With this knowledge you are now able to reference modules dynamically, and create custom objects.
127+
128+
> Author: [Kade](https://github.com/Kade-github)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[tags]: / "expert,hscript,module"
2+
3+
# Scripted States
4+
5+
todo :P
6+
7+
> Author: [Kade](https://github.com/Kade-github)
File renamed without changes.

0 commit comments

Comments
 (0)