-
Notifications
You must be signed in to change notification settings - Fork 0
Glossary of Important Methods
This page provides an overview of commonly overridden methods when creating custom bros, organized by functionality.
- General
- Primary Methods
- Melee Methods
- Special Methods
- Movement and Physics Methods
- Animation Methods
- Damage and Health Methods
- Input and Control Methods
- Misc Methods
Awake()
is called once when the Bro is created, before Start()
. You can use this method for initializing any variables / custom projectiles your bro uses, but generally I prefer to keep most of my initialization in the Start() method.
Note that your bro's sprite / gunsprite / avatar materials won't be loaded by BroMaker yet when Awake()
is called. If you want to store those, in case you wanted to swap to different materials, then you'll want to place that in Start()
instead.
Start()
is called once when the Bro is created, after Awake()
. You can use this method to set up any additional sprites that your bro uses, custom projectile prefabs, your bro's meleeType (needed if you want to have custom melee functions called), or any other variables that need initializing.
If you want to store your bro's original sprite materials (in case you'd like to swap them later) you should make sure you call base.Start() first, to allow BroMaker to load your sprites.
Update()
runs every frame during gameplay. You can use this method to handle ongoing logic, timers, and any mechanics that need constant updating.
Examples:
You might use it to track if a custom keybinding you have set is pressed:
// Switch Weapon Pressed
if ( playerNum >= 0 && playerNum < 4 && switchWeaponKey.IsDown( playerNum ) )
{
StartSwitchingWeapon();
}
Or you can use it to countdown any cooldowns / timers you have set:
// Count down to becoming sober
if ( this.drunk )
{
this.drunkCounter -= this.t;
// If drunk counter has run out, try to start becoming sober animation
if ( this.drunkCounter <= 0 )
{
this.TryToBecomeSober();
}
}
OnDestroy()
is called when your bro's GameObject is being destroyed. Override this if you want to clean up any spawned objects that you don't want to remain after your bro's death.
PreloadAssets()
is a BroMaker method that runs when the game starts up and the mod is being loaded. Use this to load custom sprites and sounds to avoid lag spikes when spawning in custom bros later.
Note:
The sprite, gunSprite, SpecialIcons, Avatar, cutscene sprites, and cutscene sounds that you have set in your bro's JSON file are automatically loaded, so you don't need to load them here. But they are cached, so it won't really be a problem if you do.
Example:
public override void PreloadAssets()
{
string directoryPath = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location );
CustomHero.PreloadSprites( directoryPath, new List<string> { "spriteStealth.png", "gunSpriteStealth.png", "avatar.png", "avatarStealth.png" } );
CustomHero.PreloadSprites( Path.Combine( directoryPath, "projectiles" ), new List<string> { "TranqDart.png", "Explosive.png", "ExplosiveGum.png" } );
directoryPath = Path.Combine( directoryPath, "sounds" );
CustomHero.PreloadSounds( directoryPath, new List<string> { "gun1.wav", "gun2.wav", "gun3.wav", "gun4.wav", "gun5.wav", "gun6.wav", "gun7.wav", "Click_Metal1.wav", "Click_Metal2.wav", "Click_Metal3.wav", "Click_Metal5.wav", "Click_Metal6.wav" } );
}
PrefabSetup()
is called when BroMaker creates your bro's prefab. This is only called once when your bro spawns for the first time, but after that BroMaker will use the cached prefab instead. So you can use this method to load assets once so you don't have to load them every time your bro spawns.
Example:
public override void PrefabSetup()
{
// Make sure you include this
base.PrefabSetup();
// Load Audio
string soundPath = Path.Combine( directoryPath, "sounds" );
this.crossbowSounds = new AudioClip[4];
this.crossbowSounds[0] = ResourcesController.GetAudioClip( soundPath, "crossbowShot1.wav" );
this.crossbowSounds[1] = ResourcesController.GetAudioClip( soundPath, "crossbowShot2.wav" );
this.crossbowSounds[2] = ResourcesController.GetAudioClip( soundPath, "crossbowShot3.wav" );
this.crossbowSounds[3] = ResourcesController.GetAudioClip( soundPath, "crossbowShot4.wav" );
}
Important Notes:
You need to make sure to include a call to base.PrefabSetup()
, because this method handles some initialization as well.
The base method also stores the path to your bro's directory in the this.directoryPath
variable, so you can use it for loading assets.
Any assets that you load here need to be assigned to public variables, otherwise Unity will ignore them when instantiating GameObjects using the prefab, which means they'll be null when your bro actually spawns. Another option to get around this is to use the SerializeField
attribute.
HarmonyPatches()
lets you apply harmony patches to your bro. Most bro's probably won't need to use any harmony patches, but if you want to create an effect that modifies the behavior of enemies then it can be useful. For example, Brostbuster's ghost trap and Furibrosa's grab both use them.
Parameters:
-
harmony
- Harmony instance
Example:
public override void HarmonyPatches( Harmony harmony )
{
Assembly assembly = Assembly.GetExecutingAssembly();
harmony.PatchAll( assembly );
}
UIOptions()
allows you to add custom options to the BroMaker menu for your bro. You can see this page for information on the different methods you have available.
Example:
public override void UIOptions()
{
GUILayout.Space( 10 );
// Only display tooltip if it's currently unset (otherwise we'll display BroMaker's tooltips
switchWeaponKey.OnGUI( out _, (GUI.tooltip == string.Empty) );
GUILayout.Space( 10 );
if ( doubleTapSwitch != ( doubleTapSwitch = GUILayout.Toggle( doubleTapSwitch, "Double Tap Down to Switch Weapons" ) ) )
{
// Settings changed, update json
this.SaveSettings();
}
}
Note:
A new instance of your bro is created to dispay these options, so it will be unconnected to your actual bro in-game. Therefore you'll need to use static variables to have the variables set by your menu options available to your actual bro in-game.
If you'd like to have the options in this menu saved between game sessions, you can use the [SaveableSetting] attribute.
If you have questions or need help with creating custom bros, you can join the Free Lives Discord Server and post your questions in the bf-mods channel.