Here, I want to present a tutorial about dipping your toes into the world of events. Though that means none of those professional-grade tools such as AWS SQS or such, it’ll still help beginners get used to the principles of how a software might work when using them.
Now, let’s get started. Project Zomboid has a very specific folder structure. Under C:\Users\Your Username\Zomboid\mods you’ll create your mod folder. Name it whatever you want, in this case I’ll name it tutorial.
You’ll want a file called mod.info, where you’ll tell everyone about your mod. It has the following structure:
name=Tutorial
poster=
id=UniqueTutorialId
description=Does the things the tutorial told me to do.
url=
Now, we create the following file inside the mod folder: tutorial\media\lua\client\boredom.lua
This’ll make the game load the lua script we’re going to make next. For this tutorial, we’re going to get rid of the boredom mechanic in the game. Honestly, if I get bored I’ll just go do something as a player, and hoarding unnamed books isn’t my idea of fun. Just let me watch the cabbages grow in peace!
Open up the lua file we created. We’re also going to need two websites, the Project Zomboid javadoc website and the wiki page about lua events. This is the documentation that holds the key to making just about anything in Project Zomboid. 2-in-1 tutorial, now we read documentations too, though if you’re reading this you probably already do that.
You might be thinking, so it’s made in Java? Yes it is, and most of it is accessible in the lua scripts too. Search the javadoc for IsoPlayer, it’s the player object that holds information on what your character is doing and “feeling”. You’ll notice there is no boredom there. It’s actually hidden in an object that IsoPlayer holds, called Stats. Alright, how do we get a player and then it’s stats?
for i = 0, getNumActivePlayers() - 1 do
local player = getSpecificPlayer(i)
local stats = player:getStats()
end
While there is a simpler way to get a player, this will ensure your mod works in multiplayer. It iterates through every player and gets their stats. Now, use stats:setBoredom(0.0) and we’re set. That’s great and all, but where are the events? Lua works top to bottom, so first we need to define our functions and then we can register them to listen to a lua event. In this case, we’re going to reset boredom every ten in-game minutes. Which event does that? In the list of events in the wiki, we can find EveryTenMinutes which is perfect for our use-case. Structure your code the way you like best, but have one function that will be called when ten minutes pass. I structured it like this:
local function YOHAULTICETL_resetBoredom(player)
local stats = player:getStats()
stats:setBoredom(0.0)
player:Say("...")
end
local function YOHAULTICETL_resetBoredomAll()
for i = 0, getNumActivePlayers() - 1 do
local player = getSpecificPlayer(i)
YOHAULTICETL_resetBoredom(player)
end
end
Events.EveryTenMinutes.Add(YOHAULTICETL_resetBoredomAll)
There you go, the last line is the one responsible for registering your method to be called every time the event (ten minutes) comes to pass. I’ve also made the character say something. The reason for this, according to what I saw on the debugger, is that the set function isn’t instantaneous, but slowly decreases boredom, or so it seems to me. This way, it lowers boredom as the character says something and then stops once he’s done.
Let’s do it instantaneously.
local function YOHAULTICETL_resetBoredom(player)
local bodyDamage = player:getBodyDamage()
bodyDamage:setBoredomLevel(0.0)
end
local function YOHAULTICETL_resetBoredomAll()
for i = 0, getNumActivePlayers() - 1 do
local player = getSpecificPlayer(i)
YOHAULTICETL_resetBoredom(player)
end
end
Events.EveryTenMinutes.Add(YOHAULTICETL_resetBoredomAll)
BodyDamage? Why does it have boredom? No idea, but this way it gets reset and works instantaneously.
If you want to see more of what’s available for you to access, besides IsoPlayer, check GlobalObject on the javadocs.
So there you have it, the simplest way to listen to an event and run a method when it happens, but in Project Zomboid. If you need further reference, this mod is also available on my Github.
