Jump to content
Sign in to follow this  

Linking item to lua code

Recommended Posts

Sorry, didn't come up with a better name for the topic.

I'm currently struggling with the linkage of items and lua code altering them. I try to give an example to better illustrate my problem:

I was looking at the shotgun and how it's moddata is used to store info with the item object itself, but I just can't get it. Somehow the item "Shotgun" defined in items.txt is generating an instance in lua, which then get's altered and this alterations are stored in the moddata of that specific instance of the shotgun item. How does this all work and when exactly does the shotgun instance reload the stored info from moddata?


Why am I asking this:

For the past couple of weeks I'm struggling with my cooling box mod. The cooling itself works fine, but it's the reload and persistents that's giving me headaches.

Share this post

Link to post
Share on other sites

I realize this was asked a month ago, but hopefully this helps someone in the future.


I'm not sure if I completely understand your issue. Are you having issues with the saving/loading moddata? I have included some sample code that should help, each school bag has it's own unique moddata value of "Test".


local bagItem = InventoryItemFactory.CreateItem("Base.Schoolbag") --Create the item in code
getSpecificPlayer(0):getCurrentSquare():AddWorldInventoryItem(bagItem, 0, 0, 0) --spawn the item on the ground
bagItem:getModData()["Test"] = ZombRand(100) --set "Test" to a random value between 0-99

local hasBag = getSpecificPlayer(0):getInventory():contains("Schoolbag") --check if player has container

if (hasBag) then --if they have container
  local moddataBag = getSpecificPlayer(0):getInventory():FindAndReturn("Schoolbag") --find the container
  print("*****schoolbag value:*****")
  print(moddataBag:getModData()["Test"]) --print the value of the container


Edited by nater

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...