Jump to content

[(should be)Fairly Simple]Function to return string name of headgear?


Somnei

Recommended Posts

Means the compiler cant see the 'equipslots' variable  (it said its nil).  Meaning you probably just need to prefix it with GLOBAL.  as in

 

GLOBAL.EQUIPSLOTS.HEAD

 

I wish it worked like that, but you can't reference to GLOBAL inside the prefab - it just says it's undeclared.

; - ;

 

Any other way I could help it find the right value? A reference inside modmain, a require? I don't really understand how everything works yet s:

Link to comment
Share on other sites

@SophieTheDog, try adding this to your modmain instead:

local function custom_desc(inst)    local desc = "Basic desc."    local headgear = inst.components.inventory.GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)    if headgear ~= nil then        desc = desc .. " " .. headgear.name    end    return descendAddPrefabPostInit("prefab_name_here", function(inst)    inst.components.inspectable:SetDescription(custom_desc)end)

But make sure your prefab has:

inst:AddComponent("inspectable")

 

I might make it so that upon equipping an item it will change the description, but I am not sure how to do this once the equiment breaks... Got any ideas? 

 

If the above code works as intended, that won't be needed. Though, I think it should be possible to do something when an equipment breaks.

 

Link to comment
Share on other sites

@SophieTheDog, try adding this to your modmain instead:

local function custom_desc(inst)    local desc = "Basic desc."    local headgear = inst.components.inventory.GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)    if headgear ~= nil then        desc = desc .. " " .. headgear.name    end    return descendAddPrefabPostInit("prefab_name_here", function(inst)    inst.components.inspectable:SetDescription(custom_desc)end)

But make sure your prefab has:

inst:AddComponent("inspectable")

 

 

If the above code works as intended, that won't be needed. Though, I think it should be possible to do something when an equipment breaks.

 

"...on/dont_starve/data/scripts/components/inventory.lua:442: attempt to index field 'equipslots' (a nil value)"

full error:

...on/dont_starve/data/scripts/components/inventory.lua:442: attempt to index field 'equipslots' (a nil value)
LUA ERROR stack traceback:
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/inventory.lua(442,1) in function 'GetEquippedItem'
        D:/Games/Steam/steamapps/common/dont_starve/data/../mods/Custom_Mod_2/modmain.lua(174,1) in function 'description'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/inspectable.lua(64,1) in function 'GetDescription'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/actions.lua(244,1) in function 'fn'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/bufferedaction.lua(19,1) in function 'Do'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/entityscript.lua(915,1) in function 'PushBufferedAction'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/locomotor.lua(215,1) in function 'PushAction'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/playercontroller.lua(1016,1) in function 'DoAction'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/playercontroller.lua(1056,1) in function 'OnLeftClick'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/playercontroller.lua(68,1) in function 'OnControl'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/components/playercontroller.lua(14,1) in function 'fn'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/events.lua(46,1) in function 'HandleEvent'
        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/input.lua(165,1) in function 'OnControl'

        D:/Games/Steam/steamapps/common/dont_starve/data/scripts/input.lua(337,1)

Pretty sure we're just trying the same sort of things over and over though... x:

 

@Mobbstar I need your help </3

Link to comment
Share on other sites

Mobbstar I need your help </3

 

Thee, who has summoned me, shallet live error-free.

 

I have had that kind of issue before, where the component would reference an own value, but it'd be nil, I never figured that out...

 

But you should try to do this modification:

local function custom_desc(inst)

    local desc = "Basic desc."

        if inst.components.inventory.equipslots then

    local headgear = inst.components.inventory.GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)

        end

    if headgear ~= nil then

        desc = desc .. " " .. headgear.name

    end

    return desc

end

 

AddPrefabPostInit("prefab_name_here", function(inst)

    inst.components.inspectable:SetDescription(custom_desc)

end)

 

I honestly don't understand extending inspection in particular. I struggled a while to do something similiar for the debug hat in my mod IR, until I gave up.

Link to comment
Share on other sites

 

Thee, who has summoned me, shallet live error-free.

 

I have had that kind of issue before, where the component would reference an own value, but it'd be nil, I never figured that out...

 

But you should try to do this modification:

local function custom_desc(inst)

    local desc = "Basic desc."

        if inst.components.inventory.equipslots then

    local headgear = inst.components.inventory.GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)

        end

    if headgear ~= nil then

        desc = desc .. " " .. headgear.name

    end

    return desc

end

 

AddPrefabPostInit("prefab_name_here", function(inst)

    inst.components.inspectable:SetDescription(custom_desc)

end)

 

I honestly don't understand extending inspection in particular. I struggled a while to do something similiar for the debug hat in my mod IR, until I gave up.

 

Thanks, Mobbstar! At least it doesn't crash me now... but it still won't work =/ now it always returns the GLOBAL.STRINGS.CHARACTERS.GENERIC.DESCRIBE.[my prefab], instead of whatever I input as the local desc or desc string ss: this code works in mysterious ways haha

Link to comment
Share on other sites

"...on/dont_starve/data/scripts/components/inventory.lua:442: attempt to index field 'equipslots' (a nil value)"

 

Pretty sure we're just trying the same sort of things over and over though... x:

 

Yes we are, but with slight modifications, possibly getting to a better solution each time. A sort of trial and error, if you will.

Although, the instance seems to be working correctly and also has an inventory component.

 

With the modifcation @Mobbstar made, it would simply skip that portion of the code when equip slots are not found. While it prevents a crash, it doesn't quite get past the problem of changing the description.

 

It seems to me, from what the error says, that the prefab you're trying to inspect does not support equipments the same way. Nevermind, I just checked it, that's not it.

 

 

I wonder if this is another one of those "self" scope problems. Give this a try.

 

Change:

local headgear = inst.components.inventory.GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)

To:

local headgear = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)

If I'm not mistaken, this will pass in a valid "self" variable which does in fact contain the correct equipslots.

 

 

If that doesn't work, also try changing it to this:

local headgear = inst.components.inventory.GetEquippedItem(inst, GLOBAL.EQUIPSLOTS.HEAD)

Mind the dots and colons, they are important.

 

Link to comment
Share on other sites

Yes we are, but with slight modifications, possibly getting to a better solution each time. A sort of trial and error, if you will.

Although, the instance seems to be working correctly and also has an inventory component.

 

With the modifcation @Mobbstar made, it would simply skip that portion of the code when equip slots are not found. While it prevents a crash, it doesn't quite get past the problem of changing the description.

 

It seems to me, from what the error says, that the prefab you're trying to inspect does not support equipments the same way. Nevermind, I just checked it, that's not it.

 

 

I wonder if this is another one of those "self" scope problems. Give this a try.

 

Change:

local headgear = inst.components.inventory.GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)
To:

local headgear = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)
If I'm not mistaken, this will pass in a valid "self" variable which does in fact contain the correct equipslots.

 

 

If that doesn't work, also try changing it to this:

local headgear = inst.components.inventory.GetEquippedItem(inst, GLOBAL.EQUIPSLOTS.HEAD)
Mind the dots and colons, they are important.

 

Switching to

    local headgear = inst.components.inventory:GetEquippedItem(inst, GLOBAL.EQUIPSLOTS.HEAD)

or

local headgear = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)

 

prevents the game from crashing, and actually returns desc - but the item equipped is still a nil, so it still won't retreive the correct item.

Link to comment
Share on other sites

... prevents the game from crashing, and actually returns desc - but the item equipped is still a nil, so it still won't retreive the correct item.

 

 

Have you tested it when an item is equipped on that entity? This is the intended result, if I remember correctly. You wanted to change the description dynamically when the entity is wearing an item. This can be done by testing "headgear.name" now.

 

Edit: Remember to test "headgear" for nil before trying to check the name, though.

Link to comment
Share on other sites

Have you tested it when an item is equipped on that entity? This is the intended result, if I remember correctly. You wanted to change the description dynamically when the entity is wearing an item. This can be done by testing "headgear.name" now.

 

Edit: Remember to test "headgear" for nil before trying to check the name, though.

 

Yeah, I always test without and with the item equipped!

Link to comment
Share on other sites

@Blueberrys,

local function custom_desc(inst)    local desc = inst.name .. ", " .. "Basic desc."        if inst.components.inventory.equipslots then    local headgear = inst.components.inventory:GetEquippedItem(inst, GLOBAL.EQUIPSLOTS.HEAD)       end    if headgear ~= nil then        desc = inst.name .. ", " .. desc .. " " .. headgear.name     end    return descend AddPrefabPostInit("spiderchan", function(inst)    inst.components.inspectable:SetDescription(custom_desc)end)
Link to comment
Share on other sites

@SophieTheDog, I'm not sure if this is causing the problem, but this line may be passing in "inst" as the equipment slot, because the colon should already be sending an instance variable as the first parameter. :

inventory:GetEquippedItem(inst, GLOBAL.EQUIPSLOTS.HEAD)

Either keep the colon ( ":" symbol before the function name ) in there, or remove the "inst" parameter, then see if it works. (don't do both, you need one or the other)

 

Link to comment
Share on other sites

@SophieTheDog, I'm not sure if this is causing the problem, but this line may be passing in "inst" as the equipment slot, because the colon should already be sending an instance variable as the first parameter. :

inventory:GetEquippedItem(inst, GLOBAL.EQUIPSLOTS.HEAD)

Either keep the colon ( ":" symbol before the function name ) in there, or remove the "inst" parameter, then see if it works. (don't do both, you need one or the other)

 

I've already tried removing the inst parameter ;;___;; no difference at all

Link to comment
Share on other sites

I've already tried removing the inst parameter ;;___;; no difference at all

Ahaha okay, sorry. xD

 

I'm not sure what's causing it to fail now, so either we do some debugging or wait for someone who already has experience with a similar issue. I can provide steps for debugging, doing it yourself will be beneficial in learning how to find and fix bugs. Or if you prefer, you can upload the files and I'll attempt to debug it myself.

 

Link to comment
Share on other sites

Ahaha okay, sorry. xD

 

I'm not sure what's causing it to fail now, so either we do some debugging or wait for someone who already has experience with a similar issue. I can provide steps for debugging, doing it yourself will be beneficial in learning how to find and fix bugs. Or if you prefer, you can upload the files and I'll attempt to debug it myself.

 

I can upload it, just not sure where lol

This site won't let me upload zips, got a good site?

Anyhow, I appreciate any help I can get! <3

Link to comment
Share on other sites

@SophieTheDog, well since I already typed it all.. I'm gonna post it, haha.

 

Debugging,

 

Put this in "custom_desc".

for k, v in pairs(inst.components.inventory.equipslots) do    print(k .. ": " .. v)end

Make the entity equip something, and equip things yourself.

Inspect the entity, then note the printed values.

 

If they are, for some reason, corresponding to your own equipment, it means "inst" is referring to the "viewer" variable passed to custom_desc. The GetDescription function in Inspectable.lua passes a "self.inst" and "viewer".

I haven't fully grasped the concept of the "self" scope passed around by lua yet, but I think this can be fixed by replacing "inst" with "self" as so:

local headgear = self.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)

 

There's a possibility they will all be nil, too.

Either way, I'm guessing it's either looking in the wrong inventory component, or wrong parent prefab.

 

 

Also, what does "inst.name" display?

 

 

As for uploading.. File Dropper? First result in Google "upload file".

 

Link to comment
Share on other sites

Using the debug-printer I get this error related to it: "attempt to concatenate local 'v' (a table value)"

Whoops.

Change the debug part to this:

for k, v in pairs(inst.components.inventory.equipslots) do    local v_name    if v ~= nil then v_name = v.name end    print(k .. ": " .. v_name)end 

 I'll have a look at the files.

 

 

Edit:

Using self instead of inst results in crash: "attempt to index global 'self' (a nil value)"

Figures. That's still the strangest part of lua for me.

 

 

Edit2:

Added "local v_name" to code above. Don't think it's required, though.

 

Link to comment
Share on other sites

@SophieTheDog, that means the slot input parameter was probably incorrect. See what this says?

print(GLOBAL.EQUIPSLOTS.HEAD)

If it's not "head", then that's the problem.

 

You could use "head" (with quotes) instead of GLOBAL.EQUIPSLOTS.HEAD, but that's not an ideal solution.

 

Well, it prints head for me... So I'm guessing it's not the problem? I'm really not sure what this proves, sorry </3

Link to comment
Share on other sites

Well, it prints head for me

I'd say that's good, because it'd be harder to find out why that's messed up (if it was).

 

Try using this:

local headgear = inst.components.inventory.equipslots[GLOBAL.EQUIPSLOTS.HEAD]

I don't think it was intended to be used this way, but..

 

 

Edit: Just tested your mod.. How did you even run it without setting a "forumthread" in modinfo? Mod crashed for me until I added it.

It can be blank, btw. So just throw this in modinfo somewhere.

forumthread = ""

Edit2: And the game froze.

Incompatible versions, perhaps. I suppose we're almost done debugging anyways.

 

 

Link to comment
Share on other sites

I'd say that's good, because it'd be harder to find out why that's messed up (if it was).

 

Try using this:

local headgear = inst.components.inventory.equipslots[GLOBAL.EQUIPSLOTS.HEAD]

I don't think it was intended to be used this way, but..

 

 

Edit: Just tested your mod.. How did you even run it without setting a "forumthread" in modinfo? Mod crashed for me until I added it.

It can be blank, btw. So just throw this in modinfo somewhere.

forumthread = ""

Edit2: And the game froze.

Incompatible versions, perhaps. I suppose we're almost done debugging anyways.

 

Yeah, not sure. It works for me haha

 

local headgear = inst.components.inventory.equipslots[GLOBAL.EQUIPSLOTS.HEAD] still returns nothing

Link to comment
Share on other sites

local headgear = inst.components.inventory.equipslots[GLOBAL.EQUIPSLOTS.HEAD] still returns nothing

 

Interesting.

 

Where did you put the debug part when it correctly printed "head: Top Hat"? Was it right before this line (inside the custom_desc function)?

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...