Jump to content

Can anyone please explain getModData() and toModData()?


Nymall

Recommended Posts

Hi all, I'm back, and taking another crack at my mod. I've been sifting through some of the game files for both the Camping Section and the Farming Section of the lua scripts, trying to understand how the game stores custom data on objects, and I've run into two functions that I don't quite understand.

 

I can see that these mods are using the getModData() function to read data from the object, and I'm supposing that toModData() serializes that data for storage, but I'm a little blank on how I'd add a piece of data to a new object and serialize it for storage. Is there anything else I'd need to do other then call toModData() on the object? It can't be that simple, can it?

 

On the flip side, I'm seeing some declarations that appear to just be dumping data straight into toModData(), and I'm wondering if anyone knows if that data can be stored using a key system, ex. "light" => 32.

 

I've been having trouble locating these declarations in the Javadocs, if anyone could point me in the right direction that would be awesome as well.

 

Thank you!

Link to comment
Share on other sites

Here is a script I wrote for you that you can hopefully glean some info from. Should actually be multiplayer compatible too if you execute it client-side by placing it in "MYMOD/media/lua/client/"

 

Hope it helps :)

function placeTheMicrofilm()    local player = getPlayer(); -- This is me    local myModData = player:getModData(); -- This is our reference to our modData    local secretMessage; -- This is a string that we will save in the modData, it is nil now                if (myModData.secret == nil) then --If we have nothing stored in this modData then we place the microfilm and then fill in the modData            print("=========== NO MICROFILM DETECTED ===========");        local RNG                    = ZombRand(1, 4); -- For fun we make a random number between 1 and 4 (Not sure if it is inclusive atm)                if RNG == 1 then            secretMessage = "The microfilm is under the seat.";    -- Based on the RNG we have our string of where the microfilm is placed.        end                if RNG == 2 then            secretMessage = "The microfilm is under your cup.";    -- ditto        end                if RNG == 3 then            secretMessage = "The microfilm is under the table.";        -- ditto        end                if RNG == 4 then            secretMessage = "The CIA is onto me! The microfilm is gone!";        -- ditto        end                myModData.secret = secretMessage; -- Set our secret message        print("============ MICROFILM IS PLACED ============"); --Print to console so we can see if it executed.            elseif (myModData.secret ~= nil) then --If we have something in the modData then we just print console stuff and end            print ("============ MICROFILM IS ALREADY PLACED, SKIPPING PLACEMENT ============");            end    endfunction whereIsTheMicrofilm()    local player = getPlayer(); -- This is me again    local myModData = player:getModData(); -- This is our reference to our modData, again    if (player) then --This is to check if we have a player.        player:Say(myModData.secret); -- if so, let him/her say where the microfilm is.    endendEvents.OnLoad.Add(placeTheMicrofilm); --This is a safety net in case the mod gets activated on a character that already exists. I am unsure if it executes on a new game.Events.OnNewGame.Add(placeTheMicrofilm); --When we start a new game, hide the microfilm comradeEvents.EveryTenMinutes.Add(whereIsTheMicrofilm); --This calls our event
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...