Example¶
This is a mod that replaces the normal player entity with a Hämis by using the
noitapatcher.SetPlayerEntity()
function.
It’s kind of jank but also fun. :^)
This should be the layout of the mod:
Hamis/
├── mod.xml
├── init.lua
└── NoitaPatcher
├── load.lua
└── noitapatcher
├── noitapatcher.dll
└── nsew
└── ...
mod.xml¶
<Mod
name="Hämis Mod"
description="Play as Hämis!"
request_no_api_restrictions="1"
>
</Mod>
init.lua¶
1-- All the examples share the same instance of NoitaPatcher that you have to
2-- manually put in your mods folder.
3dofile_once("mods/NoitaPatcher/load.lua")
4-- In real mods you would put it inside your mod folder and the `dofile_once`
5-- would look something like this:
6-- dofile_once("mods/Hamis/NoitaPatcher/load.lua")
7
8local np = require("noitapatcher")
9
10function OnPlayerSpawned(player_id)
11 local x, y = EntityGetTransform(player_id)
12 local hamis = EntityLoad("data/entities/animals/longleg.xml", x, y)
13
14 -- Need to add a bunch of components for the entity
15 -- to function more like the player.
16 local player_comps = {
17 "AudioListenerComponent",
18 "GunComponent",
19 "Inventory2Component",
20 "InventoryGuiComponent",
21 "ItemPickUpperComponent",
22 "PlatformShooterPlayerComponent",
23 "WalletComponent",
24 }
25
26 for _, comp in ipairs(player_comps) do
27 EntityAddComponent2(hamis, comp)
28 end
29
30 local controls = EntityGetFirstComponent(hamis, "ControlsComponent")
31 local damage = EntityGetFirstComponent(hamis, "DamageModelComponent")
32 local inventory = EntityGetFirstComponent(hamis, "Inventory2Component")
33
34 -- Setup inventory
35 local inv_quick = EntityCreateNew("inventory_quick")
36 local inv_full = EntityCreateNew("inventory_full")
37 EntityAddChild(hamis, inv_quick)
38 EntityAddChild(hamis, inv_full)
39
40 ComponentSetValue2(inventory, "full_inventory_slots_x", 16)
41 ComponentSetValue2(inventory, "full_inventory_slots_y", 1)
42
43 -- Controllable by player
44 ComponentSetValue2(controls, "enabled", true)
45
46 -- Extra HP to stop low HP flashing
47 ComponentSetValue2(damage, "hp", 4)
48 ComponentSetValue2(damage, "max_hp", 4)
49
50 -- Player tag
51 EntityAddTag(hamis, "player_unit")
52
53 -- Use NoitaPatcher to change what entity is considered
54 -- to be the player.
55 np.SetPlayerEntity(hamis)
56 EntityKill(player_id)
57end