-
Notifications
You must be signed in to change notification settings - Fork 0
Extending Code
Once you know which script and which part of the script you want to modify, the next step is to create a new script in your mod to extend or modify the behavior of the vanilla script.
The initial step involves creating a new script. We recommend replicating the original project's folder structure when doing so. While not technically necessary, it's good practice and makes working on more complicated mods much easier.
Let's create the new script and the necessary folders. We first need to add the extensions > content > gadgets > drillbot folders.

After this, right-click on the drillbot folder and click New Script. Let's name this script Drillbot.gd.
This script will extend the vanilla Drillbot.gd script.
To instruct Godot that this extension should occur, the first line in our new modded Drillbot.gd should read as follows:
extends "res://content/gadgets/drillbot/Drillbot.gd"You can find the path to the vanilla script by searching for it in the File System, right-clicking, and selecting Copy Path:

To add functionality to our script, we need to overwrite the goToSleep() and wakeUp() functions, which are relevant to our mod:
extends "res://content/gadgets/drillbot/Drillbot.gd"
func goToSleep():
print("Drillbert goes ZzZ...")
func wakeUp():
print("Good morning Drillbert!")Before we can test it in the game, we need to inform ModLoader that we have a file requiring extension. We can accomplish this by modifying some code in our mod_main.gd:
extends Node
const MYMODNAME_MOD_DIR = "Raffa-DrillbertSleepIndicator/"
const MYMODNAME_LOG = "Raffa-DrillbertSleepIndicator"
var dir = ""
var ext_dir = ""
func _init():
ModLoaderLog.info("Init", MYMODNAME_LOG)
dir = ModLoaderMod.get_unpacked_dir() + MYMODNAME_MOD_DIR
ext_dir = dir + "extensions/"
# Add extensions
ModLoaderMod.install_script_extension(ext_dir + "content/gadgets/drillbot/Drillbot.gd")💡 Tip: Testing your mod in Dev Mode will save you a lot of time. Read more about it here.
Upon testing it in the game, the command will print in the bottom center Output tab whenever Drillbert goes to sleep. But you'll also notice that Drillbert doesn't actually go to sleep because we overwrote the original functions from the vanilla script.
To execute the original code from the extended script, we need to call the original methods. To do this, add a super call to the original methods with a prefixed dot.
func goToSleep():
super() # this calls the base class function
print("Drillbert goes ZzZ...")
func wakeUp():
super()
print("Good morning Drillbert!")Now, the original methods will be invoked by the game, following our new code. Excellent! We're all set to add a new HUD element.
✨ With your mod all set up, proceed to the next step: Adding HUD Elements.

