|
| 1 | +[tags]: / "advanced,hscript,stage" |
| 2 | + |
| 3 | +# Scripted Stages |
| 4 | + |
| 5 | +This chapter will walk you through the process of adding a script to a Stage, and giving examples of the kind of custom behavior which can be implemented with this functionality. |
| 6 | + |
| 7 | +Start by creating a scripted class file with the `.hxc` extension (in the `mods/mymod/scripts/stages` if you want to keep things organized) |
| 8 | + |
| 9 | +```haxe |
| 10 | +// Remember to import each class you want to reference in your script! |
| 11 | +import funkin.play.stage.Stage; |
| 12 | +
|
| 13 | +// Choose a name for your scripted class that will be unique, and make sure to specifically extend the Stage class. |
| 14 | +// This class's functions will override the default behavior for the stage. |
| 15 | +class WhittyAlleywayStage extends Stage { |
| 16 | + public function new() { |
| 17 | + // The constructor gets called once, when the game loads. |
| 18 | + // The constructor takes one parameter, which is the stage ID for the stage you are applying the script to. |
| 19 | + super('alleyway'); |
| 20 | + } |
| 21 | +
|
| 22 | + // Add override functions here! |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +You can then add override functions to perform custom behavior. |
| 27 | + |
| 28 | +## Custom Behavior when adding a Character |
| 29 | + |
| 30 | +Considering characters get applied later in the initialization process, you would need to override the function `addCharacter` if you want to perform custom behavior involving characters. With this, you can set a shader to the added character immidiately after they're added. An example of this is found in the script for the stage Mall (Erect)[^mall] |
| 31 | +```haxe |
| 32 | +// ... |
| 33 | +
|
| 34 | +// Line 35 |
| 35 | +override function addCharacter(character:BaseCharacter, charType:CharacterType):Void |
| 36 | +{ |
| 37 | + // Apply the shader automatically to each character as it gets added. |
| 38 | + super.addCharacter(character, charType); |
| 39 | + trace('Applied stage shader to ' + character.characterName); |
| 40 | + character.shader = colorShader; |
| 41 | +} |
| 42 | +
|
| 43 | +// ... |
| 44 | +``` |
| 45 | + |
| 46 | +## Custom Behavior when building a Stage |
| 47 | + |
| 48 | +By overriding the function `buildStage`, you can set up custom behavior to activate when stage props get initialized. By doing this, you can immidiately apply a shader effect to props or instantly add scripted props to the stage. This is done in the stage script for Tankman Battlefield[^tank] |
| 49 | +```haxe |
| 50 | +// ... |
| 51 | +
|
| 52 | +// Line 21 |
| 53 | +override function buildStage() |
| 54 | +{ |
| 55 | + super.buildStage(); |
| 56 | +
|
| 57 | + clouds = new FlxTiledSprite(Paths.image('tankClouds'), 3200, 235, true, false); |
| 58 | + clouds.setPosition(-1100, 20); |
| 59 | + clouds.scrollFactor.set(0.25, 0.25); |
| 60 | + clouds.zIndex = 12; |
| 61 | +
|
| 62 | + clouds.velocity.x = 8; |
| 63 | +
|
| 64 | + PlayState.instance.currentStage.add(clouds); |
| 65 | + PlayState.instance.currentStage.refresh(); // Apply z-index. |
| 66 | +
|
| 67 | + tankAngle = FlxG.random.int(-90, 45); |
| 68 | + tankSpeed = FlxG.random.float(5, 7); |
| 69 | +} |
| 70 | +
|
| 71 | +// ... |
| 72 | +``` |
| 73 | + |
| 74 | +[^mall]: <https://github.com/FunkinCrew/funkin.assets/blob/main/preload/scripts/stages/mallXmasErect.hxc> |
| 75 | +[^tank]: <https://github.com/FunkinCrew/funkin.assets/blob/main/preload/scripts/stages/tankmanBattlefield.hxc> |
| 76 | + |
| 77 | +> Author: [KoloInDaCrib](https://github.com/KoloInDaCrib) |
0 commit comments