|
| 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) |
0 commit comments