Jump to content

Any ideas on where my mistake is? Trying to implement an activetable item.


Recommended Posts

Alright so I'm trying to make an item that spawns a creature when it's rightclicked inside of the inventory. My code for the spawning is

 

Spoiler

function Random(inst)
    if inst then
        local ghost = SpawnPrefab("ghost")
        local pos = Point(inst.Transform:GetWorldPosition())
        pos.x = pos.x -.3
        pos.z = pos.z -.3
        if ghost then
            ghost.Transform:SetPosition(pos.x, pos.y, pos.z)
        end
    end
end

which is called by

inst:AddComponent("dbspawncomp")
inst.components.dbspawncomp:Ondbspawncomp(Random(inst))

^ is probably wrong/doesn't work as intended** but I've tried various other calls and the issue doesn't stem from there. The issue that I get is that Transform is a nil value, which shouldn't happen. I copied the code from mound.lua, which looks like this:

Spoiler

if worker then
   if worker.components.sanity then
      worker.components.sanity:DoDelta(-TUNING.SANITY_SMALL)
   end       
   if math.random() < .1 then
      local ghost = SpawnPrefab("ghost")
      local pos = Point(inst.Transform:GetWorldPosition())
      pos.x = pos.x -.3
      pos.z = pos.z -.3
      if ghost then
         ghost.Transform:SetPosition(pos.x, pos.y, pos.z)
      end

My guess is that Transform isn't a child of the inst. that I want (the player), but I could be wrong. Can anyone spot my mistake and/or a working substitute for what I intend to do?

 

** side question: I'm only half-sure on how to make items rightclick-able, got an error when I tried to use UseableItem regarding equipable items, which makes me assume it only works for equippable items?

Link to comment
Share on other sites

in your second test, shouldn't "inst" be "worker"?

in your first test, I'm not sure what you're trying to do that that code, What is Ondbspawncomp? As it is, at the moment you're using Random(inst) as it's argument, which will be nil because your Random function returns nothing.

If I remember correctly, useitem does indeed only work on equipped items

Edited by Aquaterion
Link to comment
Share on other sites

2 hours ago, Aquaterion said:

In your first test, I'm not sure what you're trying to do that that code, What is Ondbspawncomp? As it is, at the moment you're using Random(inst) as it's argument, which will be nil because your Random function returns nothing.

I didn't know how to make it so the item is rightclick-able so I figured I'd have to create a new component to do so. I think that pinpointed the issue, as it's just an empty component right now:

Spoiler

local dbspawncomp = Class(function(self, inst)
   self.inst = inst
end)
return dbspawncomp

My lua is fuzzy in regards to calling functions so my best attempt was to use the line: inst.components.dbspawncomp:Ondbspawncomp(Random(inst))

As it's not defined in dbspawncomp, that solves why it doesn't do anything right now. If my logic is correct, am I wrong in that if I add a function similar to onEquip in dbspawncomp and I right click on the item in my inventory, the game will call Random with the above line (which currently spawns a ghost) next to the user (inst)?

 

2 hours ago, Aquaterion said:

in your second test, shouldn't "inst" be "worker"?

That was a functionality change that could've been wrong on my part, my logic was that since instead of worker digging up the mound it would be the user activating an item (which might be doer and not inst now that I think about it). Am I correct or wrong here?

Edited by Rootbeer
Link to comment
Share on other sites

8 minutes ago, Rootbeer said:

That was a functionality change that could've been wrong on my part, my logic was that since instead of worker digging up the mound it would be the user activating an item (which would be doer and not inst now that I think about it). Am I correct or wrong here?

well the whole code isn't there for me to know, but depending on what variables you have access to, you would be able to work with. I assumed worker was the player.

7 minutes ago, Rootbeer said:

As it's not defined in dbspawncomp, that solves why it doesn't do anything right now. If my logic is correct, am I wrong in that if I add a function similar to onEquip in dbspawncomp and I right click on the item in my inventory, the game will call Random with the above line (which currently spawns a ghost) next to the user (inst)?

That is correct, if there was an action to handle that component's functionality.

 

If you're using the useableitem component you'd just leave the Random() function as is (assuming its in the item.lua) and assign it as such;

inst.components.useableitem:SetOnUseFn(Random)

 

Link to comment
Share on other sites

@Aquaterion

I ended up just using the childspawner component (not sure why I didn't do that in the first place) and it works to a degree. Here's the code for it, pretty basic:

 

Spoiler

local function fn(Sim)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)


    anim:SetBank("twigs")
    anim:SetBuild("twigs")
    anim:PlayAnimation("idle")
    -----------------
    inst:AddComponent("stackable")
    inst:AddComponent("fuel")
    inst.components.fuel.fuelvalue = TUNING.MED_FUEL
    inst.components.fuel:SetOnTakenFn(FuelTaken)
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/dyingbranchinventory.xml"
    inst:AddComponent("repairer")
    inst.components.repairer.repairmaterial = "wood"
    inst.components.repairer.healthrepairvalue = TUNING.REPAIR_STICK_HEALTH*4
--------------------------------------------
         inst:AddComponent("childspawner")
    inst.components.childspawner:SetSpawnPeriod(1)
    inst.components.childspawner:SetMaxChildren(1)
    inst.components.childspawner.childname = "ghost"

         inst.components.childspawner:StartSpawning()


    return inst
end

 

However now the issue is that I don't know how to make the item rightclickable -- currently it just spawns a ghost when I pick it up from the ground. My goal is to duplicate what right clicking a spider gland does: right clicking it will cause the character to do the "murder rabbit/bird" animation but instead of healing after that's done, it'll spawn a random monster (currently only set to only a ghost for testing purposes).

I guess the question I need to answer is: how do I set the right click functionality of an item to inst.components.childspawner:StartSpawning() and afterwards consume 1 unit of the item up in the inventory after right clicking?

 

Thanks for all the help by the way, I'm learning LUA by modding don't starve and you've helped answer the few small questions that I've formed along the way.

Link to comment
Share on other sites

You will need to create a USEITEM action and then create an component action for childspawner that uses the previously created action.

I think using childspawner will probably not be ideal, since it has capacity and things can go home

Edited by Aquaterion
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
 Share

×
  • Create New...