Jump to content

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


Somnei

Recommended Posts

I want to return the name of an item worn by a character, but not sure how to do this.

I know the item names are stored as STRINGS.NAMES.[prefab] but how do I access this with a variable?

 

For example, using local cat = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD), how would I use cat to return the name of this item?

 

Thank you in advance! I really appreciate all help I can get!

Link to comment
Share on other sites

I want to return the name of an item worn by a character, but not sure how to do this.

I know the item names are stored as STRINGS.NAMES.[prefab] but how do I access this with a variable?

 

For example, using local cat = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD), how would I use cat to return the name of this item?

 

Thank you in advance! I really appreciate all help I can get!

 

Try cat.name?

Link to comment
Share on other sites

How would I go about returning the string of "inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)" from outside the prefab?

I'm trying to access it from my modmain, but it's only giving me nil! ; - ;

Link to comment
Share on other sites

How would I go about returning the string of "inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)" from outside the prefab?

I'm trying to access it from my modmain, but it's only giving me nil! ; - ;

 

Hmm.. try this:

GLOBAL.GetPlayer().components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD).string
Link to comment
Share on other sites

That will crash if the player isnt wearing something on their head.  Always check if the object exists before you access a variable of it. 

local desc = "empty"local headgear = GLOBAL.GetPlayer().components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)if headgear ~= nil then    desc = headgear.stringend

Its safe to check the value of a variable that is nil.  Its not safe to pretend its not nil and check a variable inside it.  So looking at headgear is fine if headgear doesnt exist. Looking at  headgear.ANYTHING is not safe if headgear doesnt exist.

Link to comment
Share on other sites

 

Hmm.. try this:

GLOBAL.GetPlayer().components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD).string

 

local testing = GLOBAL.inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD) gives me this error:

"variable 'inst' is not declared"

though I'm pretty sure that is because of GLOBAL..?

Link to comment
Share on other sites

That will crash if the player isnt wearing something on their head.  Always check if the object exists before you access a variable of it. 

local desc = "empty"local headgear = GLOBAL.GetPlayer().components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)if headgear ~= nil then    desc = headgear.stringend

Its safe to check the value of a variable that is nil.  Its not safe to pretend its not nil and check a variable inside it.  So looking at headgear is fine if headgear doesnt exist. Looking at  headgear.ANYTHING is not safe if headgear doesnt exist.

 

It still crashes me with this error: "variable 'inst' is not declared", despite being safer ; ^ ;

Link to comment
Share on other sites

local testing = GLOBAL.inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD) gives me this error:

"variable 'inst' is not declared"

though I'm pretty sure that is because of GLOBAL..?

 

Err.. did you try the code I gave?

 

"inst is not declared" means there's no such thing as "GLOBAL.inst" in the game. Which also means there's no such thing as GLOBAL.inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)

 

Usually, the inst variable is used to carry information about the instance of an entity. The GLOBAL variable contains (almost?) everything you can mess with in the game.

As far as I can tell, it won't make sense for GLOBAL to contain an inst variable at all.

 

The code I provided finds the same value using a different method.

 

 

*facepalm* After reading my code again, it appears I mixed up the end bit. Sorry about that.

 

It should have been:

GLOBAL.GetPlayer().components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD).name

Also, EQUIPSLOTS.HEAD might need to be GLOBAL.EQUIPSLOTS.HEAD here. Not sure about that one.

 

GLOBAL.GetPlayer() is equivalent of the "inst" you were using before.

 

 

 

Edit:

Sorry @seronis, missed your post. Thanks for the correction, I completely forget about these things sometimes.

Link to comment
Share on other sites

Err.. did you try the code I gave?

 

"inst is not declared" means there's no such thing as "GLOBAL.inst" in the game. Which also means there's no such thing as GLOBAL.inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)

 

Usually, the inst variable is used to carry information about the instance of an entity. The GLOBAL variable contains (almost?) everything you can mess with in the game.

As far as I can tell, it won't make sense for GLOBAL to contain an inst variable at all.

 

The code I provided finds the same value using a different method.

 

 

*facepalm* After reading my code again, it appears I mixed up the end bit. Sorry about that.

 

It should have been:

GLOBAL.GetPlayer().components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD).name

Also, EQUIPSLOTS.HEAD might need to be GLOBAL.EQUIPSLOTS.HEAD here. Not sure about that one.

 

GLOBAL.GetPlayer() is equivalent of the "inst" you were using before.

 

but can you really use GetPlayer() for a prefab in the game (that is not a playable character)? That doesn't make any sense...?

Link to comment
Share on other sites

@SophieTheDog, GetPlayer() returns the instance of the character in the game.

 

Basically what you are doing when you use GetPlayer().components is essentially returning the instances of the player and instantly looking at it's components without wasting memory or processing power. It's exactly the same as the following:

local inst = GLOBAL.GetPlayer()inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD).name 
Link to comment
Share on other sites

but can you really use GetPlayer() for a prefab in the game? That doesn't make any sense...?

 

Technically you can. Think of it this way, you're getting the name of the thing the player is wearing on their head. It's like saying, "Hey, what's your hat called?"

 

You've been using GetEquippedItem(EQUIPSLOTS.HEAD) already, it's the same thing just using a different route.

Link to comment
Share on other sites

@Blueberrys,@Kzisor,  just to make myself clear, this is not about the player but about a prefab in the game not controlled by the player - for example a pig.

 

I see. Then it's not the player who's wearing the hat?

 

It's difficult to know exactly what you're trying to do like this, can we have a look at your code please?

Link to comment
Share on other sites

@Kzisor,

@Blueberrys,

 

this segment of my code:

AddComponentPostInit("inspectable", function(inst)
    local oldGetDesc = inst.GetDescription
    function inst:GetDescription(viewer)
        local desc = oldGetDesc(self,viewer)
        if self.inst and self.inst.components and self.inst.components.named and self.inst:HasTag("spiderchan") then
 
local desc_adder = "empty"
local headgear = GLOBAL.inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD).string
if headgear ~= nil then
desc_adder = headgear.string
end
 
print(testing)
        desc_adder = "\nIt has no items equipped."
        local current = self.inst.components.inventory:GetEquippedItem()
       if testing then desc_adder = "\nIt is wearing a " .. testing .. "."
       end
 
            if type(desc)~="string" then
                desc=""
            end
            desc = self.inst.name..", "..(desc=="" and "" or (desc))..desc_adder
        end
        return desc
    end

end)

 

of course, not working at all right now for obvious reasons ;)

Link to comment
Share on other sites

@SophieTheDog, try swapping GLOBAL to self and see if it works.

 

"self" isn't being passed to that function. I don't think that would work, unless I'm missing something about the variable scopes here.

 

 

@SophieTheDog

But "inst" is being passed in. Have you tried it without the GLOBAL, like this?:

local headgear = inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)
if headgear ~= nil then
    desc_adder = headgear.name
end

 

(Remember to change "string" to "name", that was a mistake from earlier..)

 

 

There are also a few other logical errors in the code you provided. Such as, "testing" is not declared anywhere, but you're attempting to use it.

 

Try using this.

AddComponentPostInit("inspectable", function(inst)
    local oldGetDesc = inst.GetDescription
    function inst:GetDescription(viewer)
        local desc = oldGetDesc(self,viewer)
        if self.inst and self.inst.components and self.inst.components.named and self.inst:HasTag("spiderchan") then
 
            local desc_adder = "empty"
            local headgear_name = nil
            
            local headgear = inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)
            if headgear ~= nil then
                headgear_name = headgear.name
            end
            
            print(headgear_name)
            
            desc_adder = "\nIt has no items equipped."
            local current = self.inst.components.inventory:GetEquippedItem()
            if headgear_name then desc_adder = "\nIt is wearing a " .. headgear_name .. "."
            end
 
            if type(desc)~="string" then
                desc=""
            end
            desc = self.inst.name..", "..(desc=="" and "" or (desc))..desc_adder
        end
        return desc
    end
end)

 

Link to comment
Share on other sites

AddComponentPostInit()

 

I havent used this postinit yet.  Are you sure its even a prefab instance that is passed into your callback function?  My instinct says it should be a reference to the component that is passed in and NOT a reference to a prefab because some components dont even belong to prefabs.  The krampus, hounded, seasonmanager and clock components for some examples.

Link to comment
Share on other sites

@seronis, I believe you are correct. (I haven't used it either, though)

 

And after looking over it a couple times, I'm even more confused about the whole logic of this code.

 

@SophieTheDog, from what I can tell, you are you trying to append to the description of every creature. Do you want to do that only when; you are using a certain character, you are wearing a specific item, or the creature is wearing a specific item?

 

I think there may be a better way to do either of those without modifying the inspectable component.

Link to comment
Share on other sites

I am trying to add something to the description of this specific prefab, is it possible to do from within itself or something? c:

And also, I want it to always modify the description of this character - and when it is wearing something, print out what!

Link to comment
Share on other sites

I am trying to add something to the description of this specific prefab, is it possible to do from within itself or something? c:

And also, I want it to always modify the description of this character - and when it is wearing something, print out what!

 

I think this might work. Give it a try.

 

Inside your prefab file,

In the local function fn(Sim) function,

After inst:AddComponent("inspectable")

Use inst.components.inspectable:SetDescription(desc)

 

It should look something like this:

local function fn(Sim)    local inst = CreateEntity()    -- more code here...    inst:AddComponent("inspectable")    inst.components.inspectable:SetDescription("This prefab's description")        -- more code here...    return instend

 

 

Although, as far as I know, this part of the code will run once when the entity is created. You could possibly set the description to a function, but I'm not sure how well that will work.

 

Using a function to find the hat:

local function fn(Sim)    local inst = CreateEntity()    -- more code here...    inst:AddComponent("inspectable")    local function custom_desc(inst)        local desc = "Basic desc."        local headgear = inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)        if headgear ~= nil then            desc = desc .. " " .. headgear.name        end        return desc    end    inst.components.inspectable:SetDescription(custom_desc)        -- more code here...    return instend

You should also make sure the prefab actually has an inventory component before using "inst.components.inventory.

 

I haven't tested any of these myself (yet).

 

 

Edit: Changes in code above. Just added "inst" parameter to the "custom_desc" function.

Link to comment
Share on other sites

I think this might work. Give it a try.

 

Inside your prefab file,

In the local function fn(Sim) function,

After inst:AddComponent("inspectable")

Use inst.components.inspectable:SetDescription(desc)

 

It should look something like this:

local function fn(Sim)    local inst = CreateEntity()    -- more code here...    inst:AddComponent("inspectable")    inst.components.inspectable:SetDescription("This prefab's description")        -- more code here...    return instend

 

 

Although, as far as I know, this part of the code will run once when the entity is created. You could possibly set the description to a function, but I'm not sure how well that will work.

 

Using a function to find the hat:

local function fn(Sim)    local inst = CreateEntity()    -- more code here...    inst:AddComponent("inspectable")    local function custom_desc(inst)        local desc = "Basic desc."        local headgear = inst.components.inventory.GetEquippedItem(EQUIPSLOTS.HEAD)        if headgear ~= nil then            desc = desc .. " " .. headgear.name        end        return desc    end    inst.components.inspectable:SetDescription(custom_desc)        -- more code here...    return instend

You should also make sure the prefab actually has an inventory component before using "inst.components.inventory.

 

I haven't tested any of these myself (yet).

 

 

Edit: Changes in code above. Just added "inst" parameter to the "custom_desc" function.

 

Thanks, I'll try using that! 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? 

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...