Modding Help Is it possible to make Augments removable and interchangeable?

Discussion in 'Starbound Modding' started by Tuesday's Eyebrow, Jul 28, 2016.

  1. Tuesday's Eyebrow

    Tuesday's Eyebrow Phantasmal Quasar

    Just what the title says. I don't understand what the corresponding scripts mean, as my understanding of LUA is... well I don't have one.

    Anywho, here's what I was looking at:


    "scripts/augments/augment.lua"
    Code:
    require "/scripts/augments/item.lua"
    
    function apply(input)
      local augmentConfig = config.getParameter("augment")
      local output = Item.new(input)
      if augmentConfig then
        if output:instanceValue("acceptsAugmentType", "") == augmentConfig.type then
          local currentAugment = output:instanceValue("currentAugment")
          if currentAugment then
            if currentAugment.name == augmentConfig.name then
              return nil
            end
          end
    
          output:setInstanceValue("currentAugment", augmentConfig)
          return output:descriptor(), 1
        end
      end
    end
    

    "scripts/augments/item.lua"
    Code:
    Item = {}
    Item.__index  = Item
    
    function Item.new(...)
      local self = setmetatable({}, Item)
      self:init(...)
      return self
    end
    
    function Item:init(descriptor)
      self.name = descriptor.name
      self.count = descriptor.count or 1
      self.parameters = descriptor.parameters or {}
      self.config = root.itemConfig(descriptor).config
    end
    
    function Item:type()
      return root.itemType(self.name)
    end
    
    function Item:descriptor()
      return {
          name = self.name,
          count = self.count,
          parameters = self.parameters
        }
    end
    
    function Item:instanceValue(name, default)
      return sb.jsonQuery(self.parameters, name) or sb.jsonQuery(self.config, name) or default
    end
    
    function Item:setInstanceValue(name, value)
      self.parameters[name] = value
    end
    
    Any help would be wonderful.
    Preferably, I'd like to be able to just split the augment from the item. But I definitely need a way to switch the augment without destroying the old one.
     
  2. Inf_Wolf14

    Inf_Wolf14 Parsec Taste Tester

    I can't help much code wise unless I go and make try this, but for an idea on how to go about this:
    Have a local variable store the value of currenAugment.name before applying the new one, then add an augment of the type recorded to the player's inventory after application if it i s successful. (It does nothing if you try to apply the same augment, so without care, this could become duplication)
     
  3. Naddox

    Naddox Cosmic Narwhal

    I am also interested in figuring this out and my lua understanding is very basic.
     
  4. Ricowan

    Ricowan Scruffy Nerf-Herder

    Inf_Wolf14 put you on the right path, you need to modify the first function you posted. The current augment is already initialized for you, so you only need to add the code to put the currentAugment into the player's inventory. I don't know the game code for that, so I put in some pseudo code in the right spot. You don't need to worry about duplication, because the function already exits early if the augments match.

    This would be a handy mod to have! I hope someone familiar with inserting items into the inventory can help fill in the rest.

    "scripts/augments/augment.lua"

    Code:
    require "/scripts/augments/item.lua"
    
    function apply(input)
      local augmentConfig = config.getParameter("augment")
      local output = Item.new(input)
      if augmentConfig then
        if output:instanceValue("acceptsAugmentType", "") == augmentConfig.type then
          local currentAugment = output:instanceValue("currentAugment")
          if currentAugment then
            if currentAugment.name == augmentConfig.name then
              return nil
            end
          end
    
          -- Insert currentAugment into Player's inventory here, this probably involves creating a new item of type currentAugment.
          output:setInstanceValue("currentAugment", augmentConfig)
          return output:descriptor(), 1
        end
      end
    end
    
     
  5. Stroomschok

    Stroomschok Void-Bound Voyager

    I'm a total newbie at this and I have no idea how to add items to your inventory,

    But I think /scripts/actions/container.lua has the code to put stuff in a container. I suspect the code for you character's inventory is similar?
    Code:
    function containerAddItem(args, output)
      args = parseArgs(args, {
        entity = nil,
        itemName = nil,
        amount = 1,
        parameters = {}  
      })
      local entityId = BData:getEntity(args.entity)
      if args.itemName == nil or entityId == nil then return false end
    
      if world.containerAddItems(entityId, {name = args.itemName, amount = args.amount, parameters = args.parameters}) then
        return true
      else
        return false
      end
    end
    
     
  6. Inf_Wolf14

    Inf_Wolf14 Parsec Taste Tester

    I recommend you check the player documentation in "/docs/lua/player.md".
    That'll have the function to add items to a player your looking for.

    Also I said something about duplication because if you added the current augmentation at the end of the script, even if the current and applying augments matched and nothing happened, you'd end up duplicating the augment you were applying. (Have both the one you attempted to apply and the one given by the script.)

    Sorry I can't help more, but I'm a little busy in my own mod to check anything firsthand.
     
  7. Stroomschok

    Stroomschok Void-Bound Voyager

    Oh wow, never noticed that documentation o_O

    I think this is it:
    Code:
    #### `void` player.giveItem(`ItemDescriptor` item)
    
    Adds the specified item to the player's inventory.
    
     
    Inf_Wolf14 likes this.
  8. Ricowan

    Ricowan Scruffy Nerf-Herder

    I've been playing with this, but I've run into a problem. player.giveItem() fails in augment.lua because player is nil. I haven't been able to figure out how to instantiate the player object in this file.
     

Share This Page