Modding Help Is There a Way to Affect Existing Food with a LUA Script?

Discussion in 'Starbound Modding' started by EclipticWulf, Aug 3, 2017.

  1. EclipticWulf

    EclipticWulf Scruffy Nerf-Herder

    One of the issues I'm running into with this mod of mine is that the food generated before and after the mod is installed is the food from before installation remains unstackable with the new food. I assume this is because of some kind of Metadata the game uses, but that's just a guess.

    Is there a way I can affect existing food with a LUA script?

    I changed the buildfood script so now I do not have to patch every food and everything is automatically set using the script. But this still remains an issue with food before the mod is installed.

    Edit: I now have the rotting script being called properly. But it still remains unstackable.

    So does anyone know how I could solve this dilemma without needing a manual way to turn pre-generated food into newly-generated food? Thanks!

    Edit 2: Here is the new data in the buildfood.lua script and the rotting.lua script

    Code:
    function build(directory, config, parameters, level, seed)
      config.maxStack = 1000
      config.tooltipKind = ""
      config.rottingMultiplier = 1000
    
      return config, parameters
    end
    
     
    Last edited: Aug 3, 2017
  2. bk3k

    bk3k Oxygen Tank

    Yeah the rotting script does this. I suppose you could run a quest to do it too.

    As for stacking, they won't stack unless they have the same parameters. They must be truly identical. Thus partially rotted food has a parameter for timeToRot and tooltipFields

    I believe you altering the config would likewise stop them from stacking, except if they're all altered in the same way that should be fine.

    Anyhow I'll add one line to your code
    Code:
    function build(directory, config, parameters, level, seed)
      config.maxStack = 1000
      config.tooltipKind = ""
      config.rottingMultiplier = 1000
      parameters = {}
    
      return config, parameters
    end
    
     
    EclipticWulf likes this.
  3. EclipticWulf

    EclipticWulf Scruffy Nerf-Herder

    It worked. Thank you so much!

    On a side note, do you need the "config." parts? As I see you didn't use it for the parameters. Or is it exclusively parameters you don't have to use it for?

    Also a different predicament I'm running into is getting a LUA script to pick up that the item is of the category "food" (or "cookedFood")

    item.category == "food" doesn't seem to work. I'm guessing it's named something else as a parameter. Any ideas?

    What I'm trying to do is make it so you can decompose any food item by putting it into an object similar to the pet healer. This is what I have for the code so far. It's not the full concept but at least it works.
    Code:
    require "/scripts/util.lua"
    
    function craftingRecipe(items)
      if #items ~= 1 then return end
      local item = items[1]
      if not item or item.name ~= "alienmeat" then return end
    
      local rotten = {
          name = "rottenfood",
          count = item.count
        }
    
      return {
          input = items,
          output = rotten,
          duration = 1.0
        }
    end
    

    Namely looking at this line:
    Code:
    if not item or item.name ~= "alienmeat" then return end
    and changing it to something like
    Code:
    if not item or item.category ~= "food" then return end
     
    Last edited: Aug 3, 2017
  4. bk3k

    bk3k Oxygen Tank

    Normally I don't think you would change the config table at all. That's the original data you would see in the files themselves(but with patches applied).

    The parameters table is where you apply changes add additions to these attributes. Sort of like patching but from the lua side... If that's a good explanation.

    You could clear it to remove the food rot, then apply only the changes you want.

    The code I posted was just your code plus one line.

    For your other question, if dealing with the same data as the other script

    config.category is what you'd compare.
     
  5. EclipticWulf

    EclipticWulf Scruffy Nerf-Herder

    Well they're two separate scripts. One is for generating the actual food. The other is for an object that turns only food items into rotten food. It's not all one .lua of code - hopefully that clears any confusion either of us have.

    I tried config.category, but I'll try it again. I may have just did it incorrectly the first time.

    Edit: No, still no luck
     
    Last edited: Aug 4, 2017
  6. bk3k

    bk3k Oxygen Tank

    It is now easier to look at this when I'm at home instead of on mobile like earlier.

    I have to assume some things about your code there, since I don't see all of it. Now the earlier example is using the full item configuration data. That's what you'd get back from the root.itemConfig function. The function in my quote here is (I assume) receiving a table with itemDescriptors. Here is a sample itemdescriptor

    Code:
    local itemDes = {
      name = "translocator",
      count = 1,
      parameters = { 
        shortDescription = "You shouldn't have this"
      }
    }
    
    where as the parameters are me changing the description. Parameters could be empty though, and usually would be. Notice a lack of things like "category" etc? You have the name, the count, and any altered parameters. You could set a new category in parameters, but that doesn't serve your need to read it. So lets expand a bit.

    Code:
    local itemDes = {
      name = "translocator",
      count = 1,
      parameters = { 
        shortDescription = "You shouldn't have this"
      }
    }
    
    local itemConfig = root.itemConfig(itemDes)
    local category = itemConfig.config.category
    
    The table returned by root.itemConfig will have 3 tables inside it. directory, config, and parameters - at least according to the documentation. It may also have builderConfig as well, but not always. That's something they forgot to document, but I have noticed.

    Where getting just the category, pulling it from config is fine. For pulling other data - data likely to be altered - you might like

    Code:
    get_fromConfig = function(whatInfo, itemConfig, default)
      if itemConfig == nil then
        local dump = "\nget_fromConfig() called with nil itemConfig"
        dump = dump .. "\nargument \"whatinfo\" : " .. tostring(whatInfo) .. "\nreturning \"default\" argument"
        if type(default) == "table" then
          dump = dump .. makeString(default, "default", "\nget_fromConfig():default\n")
        else
          dump = dump .. "\nget_fromConfig():default\ndefault : " .. tostring(default)
        end
        sb.logInfo(dump)
        return default
      elseif itemConfig.parameters[whatInfo] ~= nil then
        return itemConfig.parameters[whatInfo]
      elseif itemConfig.builderConfig then
        local bc = itemConfig.builderConfig
        for _, v in ipairs(bc) do
          if bc[whatInfo] then
            return bc[whatInfo]
          end
        end
      elseif itemConfig.config[whatInfo] ~= nil then
        return itemConfig.config[whatInfo]
      else
        return default
      end
    end
    
    You feed it the property name you are looking for ("price" for example), the itemConfig itself - as returned by feeding your itemDescriptor into root.itemConfig(), and a default answer that it will give if the parameter isn't found anywhere in the itemConfig. That can make pulling parameters less tedious.

    Real example code here - https://www.dropbox.com/s/62ke0ilryu8fgez/weapon_vendingMachines.7z?dl=0
    I'm using all these things I'm talking about in that script.


    If any part of what I've said is unclear(very possible), let me know.
     
    EclipticWulf likes this.
  7. EclipticWulf

    EclipticWulf Scruffy Nerf-Herder

    Sorry this is late, but I still haven't had much luck trying different varieties of things.

    First off, I am not planning to alter any data. So no need to worry about that. All I'm trying to do is pick up if an item in a slot of a container is of a certain category or not - then spitting out rotten food.

    Second, so yes - that is all of the code. There isn't anymore to it. I'll put it here again, though.
    Code:
    require "/scripts/util.lua"
    
    function craftingRecipe(items)
      if #items ~= 1 then return end
      local item = items[1]
      if not item or item.name ~= "alienmeat" then return end
    
      local rotten = {
          name = "rottenfood",
          count = item.count
        }
    
      return {
          input = items,
          output = rotten,
          duration = 1.0
        }
    end
    


    Here's the point it's at with no further luck and probably more incorrect.
    Code:
    require "/scripts/util.lua"
    
    function craftingRecipe(items)
        if #items ~= 1 then return end
        local item = items[1]
        if not item then return end
     
        local itemConfig = root.itemConfig(item)
        local category = itemConfig.parameters.category
     
        if category == "food" then
            local rotten = {
                name = "rottenfood",
                count = item.count
            }
    
            return {
                input = items,
                output = rotten,
                duration = 1.0
            }
        end
    end
    


    And for any further clarification, the bolded text below is what I'm trying to detect

    {
    "itemName" : "alienmeat",
    "rarity" : "Common",
    "price" : 4,
    "category" : "food",
    "eventCategory" : "cookingIngredient",
    "inventoryIcon" : "alienmeat.png",
    "description" : "A raw slab of weird, stringy alien meat. Maybe I should cook it.",
    "shortdescription" : "Raw Steak",
    "foodValue" : 10,
    "tooltipKind" : "food",
    "builder" : "/items/buildscripts/buildfood.lua",
    "maxStack" : 1,
    "effects" : [ [
    {
    "effect" : "foodpoison"
    }
    ] ],
    "itemAgingScripts" : ["/scripts/items/rotting.lua"],
    "rottingMultiplier" : 0.5,
    "blockingEffects" : [
    "wellfed"
    ]
    }
     

Share This Page