Jump to content

[SOLVED] Inconsistencies between an object's class and its method


Izeick

Recommended Posts

While coding my mod I faced with another problem. I'm trying to make a character say a phrase when dressing a certain item. I tried to implement this with Events.OnClothingUpdated.Add(). When this event was called, it was checked whether this item (CI141.KRNDHazmat) was dressed. I did this through a method getBodyLocation() ~= "". But every time it gave an error. I suspect that the problem is that item CI141.KRNDHazmat belongs to a different class, but getBodyLocation() method belongs to the class Clothes. But no matter how hard I tried, I couldn't determine the class of the object CI141.KRNDHazmat.

I need help from someone who understands. I paste code below.

 

Script [media/scripts/CI141_items.txt]
item KRNDHazmat 
    {
        DisplayCategory     = Clothing,
        Type                 = Clothing,
        DisplayName         = KRND Hazmat Suit,
        ClothingItem         = CI141_KRNDHazmat,
        BodyLocation         = FullSuitHead,
        Icon                 = KRNDHazmat,
        BloodLocation         = Trousers;Jumper,
        Icon                 = Hazmatsuit,
        RunSpeedModifier     = 0.75,
        ScratchDefense         = 25,
        BiteDefense         = 25,
        Insulation             = 0.65,
        WindResistance         = 0.9,
        WorldStaticModel     = Hazmat_Ground,
    }

 

the .xml file works fine, so I don't see the point in attaching it here

 

LUA [media/lua/client/CI141_maincore.lua]

function HazmatvoiceFc(player)
    local hazmat = "CI141.KRNDHazmat"
    if player:getInventory():contains(hazmat) then
        if hazmat:getBodyLocation() ~= nil then         <- Error comes from this line
            player:Say("TESTPHRASE")
        end
    end
end
 
Events.OnClothingUpdated.Add(HazmatvoiceFc)
Edited by Izeick
Link to comment
Share on other sites

37 minutes ago, Izeick said:

While coding my mod I faced with another problem. I'm trying to make a character say a phrase when dressing a certain item. I tried to implement this with Events.OnClothingUpdated.Add(). When this event was called, it was checked whether this item (CI141.KRNDHazmat) was dressed. I did this through a method getBodyLocation() ~= "". But every time it gave an error. I suspect that the problem is that item CI141.KRNDHazmat belongs to a different class, but getBodyLocation() method belongs to the class Clothes. But no matter how hard I tried, I couldn't determine the class of the object CI141.KRNDHazmat.

I need help from someone who understands. I paste code below.

 

Script [media/scripts/CI141_items.txt]
item KRNDHazmat 
    {
        DisplayCategory     = Clothing,
        Type                 = Clothing,
        DisplayName         = KRND Hazmat Suit,
        ClothingItem         = CI141_KRNDHazmat,
        BodyLocation         = FullSuitHead,
        Icon                 = KRNDHazmat,
        BloodLocation         = Trousers;Jumper,
        Icon                 = Hazmatsuit,
        RunSpeedModifier     = 0.75,
        ScratchDefense         = 25,
        BiteDefense         = 25,
        Insulation             = 0.65,
        WindResistance         = 0.9,
        WorldStaticModel     = Hazmat_Ground,
    }

 

the .xml file works fine, so I don't see the point in attaching it here

 

LUA [media/lua/client/CI141_maincore.lua]

function HazmatvoiceFc(player)
    local hazmat = "CI141.KRNDHazmat"
    if player:getInventory():contains(hazmat) then
        if hazmat:getBodyLocation() ~= nil then         <- Error comes from this line
            player:Say("TESTPHRASE")
        end
    end
end
 
Events.OnClothingUpdated.Add(HazmatvoiceFc)

 

hazmat is just the text "CI141.KRNDHazmat", so you can't call getBodyLocation on it.

 

You might want to try:
local hazmatItem = player:getInventory():FindAndReturn(hazmat)

if hazmatItem then

--do something

end

 

Link to comment
Share on other sites

1 minute ago, Hugo Qwerty said:

 

hazmat is just the text "CI141.KRNDHazmat", so you can't call getBodyLocation on it.

 

You might want to try:
local hazmatItem = player:getInventory():FindAndReturn(hazmat)

if hazmatItem then

--do something

end

 

Okay, in this case how can I access the elements of the Item class that I create in .txt if all I give them is a name like "CI141.KRNDHazmat". Do these items have their own IDs to call? If so, how to get them? F.e. what is the lua-id of my item CI141.KRNDHazmat?

Link to comment
Share on other sites

Just now, Izeick said:

Okay, in this case how can I access the elements of the Item class that I create in .txt if all I give them is a name like "CI141.KRNDHazmat". Do these items have their own IDs to call? If so, how to get them? F.e. what is the lua-id of my item CI141.KRNDHazmat?

 

If you want to access a script item you can use: getScriptManager():getItem("Base.Bowl"), if you want to create an instance then you can use InventoryItemFactory.CreateItem("Base.Bowl").

 

The former will give you a:  https://zomboid-javadoc.com/41.78/zombie/scripting/objects/Item.html  (or nil)

The latter will give you a:  https://zomboid-javadoc.com/41.78/zombie/inventory/InventoryItem.html  (or nil)

 

If you want to check if the player is wearing an item, loop through their inventory and look for a item with type hazmat (or whatever) and check :isEquipped()

 

Not sure how you would check if the player has only just put that item on.

Link to comment
Share on other sites

24 minutes ago, Hugo Qwerty said:

 

If you want to access a script item you can use: getScriptManager():getItem("Base.Bowl"), if you want to create an instance then you can use InventoryItemFactory.CreateItem("Base.Bowl").

 

The former will give you a:  https://zomboid-javadoc.com/41.78/zombie/scripting/objects/Item.html  (or nil)

The latter will give you a:  https://zomboid-javadoc.com/41.78/zombie/inventory/InventoryItem.html  (or nil)

 

If you want to check if the player is wearing an item, loop through their inventory and look for a item with type hazmat (or whatever) and check :isEquipped()

 

Not sure how you would check if the player has only just put that item on.

Okay, but what about ":isEquipped()" syntax? player:item:isequiped? Or hazmatItem:isequiped?

Link to comment
Share on other sites

29 minutes ago, Hugo Qwerty said:

 

If you want to access a script item you can use: getScriptManager():getItem("Base.Bowl"), if you want to create an instance then you can use InventoryItemFactory.CreateItem("Base.Bowl").

 

The former will give you a:  https://zomboid-javadoc.com/41.78/zombie/scripting/objects/Item.html  (or nil)

The latter will give you a:  https://zomboid-javadoc.com/41.78/zombie/inventory/InventoryItem.html  (or nil)

 

If you want to check if the player is wearing an item, loop through their inventory and look for a item with type hazmat (or whatever) and check :isEquipped()

 

Not sure how you would check if the player has only just put that item on.

And that's it, I figured it out. Tnx u so much! Again.

Link to comment
Share on other sites

  • Izeick changed the title to [SOLVED] Inconsistencies between an object's class and its method

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
×
×
  • Create New...