get what data? getModData()!


When I made the pillowsilencer mod, I decided that a pillow should get torned and loose its ability to reduce noise. To achieve it I had two choices. One was to alter the pillow item shipped with the game and make it drainable like a battery, the other was to make my own pillow health property and attach it to the pillow. Since I hate altering existing code, the last became the obvious answer.

The answer to my prayers was getModData().

getModData() which litterally any object in the game has. Items, characters, you name it. Now what it does is giving you a LUA table that will be saved with the item. Let's take a quick example that demonstrates this.

An example

function getAllPersonalWithMyPrimary()
local meh = getSpecificPlayer(0); -- dis is meh
local mahPrimary = meh:getPrimaryHandItem();
local mahMuddyData = mahPrimary:getModData();
mahMuddyData.petname = "Purrfus"
-- Imma dun here
-- Your then unequip the item, drop it, save the game, quit, fry your entire motherboard and dramatically die in huge flames, that you never realized because you were reading this guide.
-- Some other fireman salvage your computer, switch it on and plays your Project Zomboid, loads the game, picks up the item and there you go... (just saying the petname is persistant)

andThePetNameIs = getSpecificPlayer(0):getPrimaryHandItem():getModData().petname;
-- Surprise "Purrfus"

An explanation

Now the observant reader might think "what the hells is up with the code comments". But the even more observant reader might wonder "Something is wrong. We never used a setter, how was the data actually save?". Well actually it beats me too. The getModData() function is actually a java method. My best guess is it returns the object moddata it self or a reference/pointer to the objects memory address. LUA does not handle a table representing the object. It is handling the very object it self.

What can be saved into getModData?

According to RobertJohnson - the messiah of indie stones community forums - only strings and floats (yes that includes integers) can be saved into the LUA table. Read more about it in this thread.



  • Austin

    Good read.

    Aug 6. 13
You need to be logged in to write comments