Jump to content

Recommended Posts

I have some code in the character that doesn't allow them to wear armour but allows them to wear backpacks and carry suspicious marbles.

However it doesn't allow them to wear amulets, how would I change it so they can wear amulets? 

Here is the code:

local function drop(inst)
 
local body = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
    if body then
if not body:HasTag("backpack") then
if not body:HasTag("heavy") then
if not body:HasTag("tag")
inst:DoTaskInTime(0.1, function()
local item = inst.components.inventory:Unequip(EQUIPSLOTS.BODY)
	if inst and inst.components.talker then
		inst.components.talker:Say("It does not fit me.")
	end
inst.components.inventory:GiveItem(item)
    inst.AnimState:OverrideSymbol("swap_body", "nil", "nil")
        end)
        end
    end
end
end

 

Not every check must be done through tags- I personally opt more towards prefabs in specific if I only want certain things to take effect.

 

In your case all of the base game code's prefabs for the amulets all contain 'amulet' in it.

So adding in the check:

a.prefab and (a.prefab:find("amulet") == nil)

Would make it exclude amulets.

 

The 'a.prefab' check is to ensure that some other mod didn't destroy the prefab name by making it nil.

 

You can also set the DoTaskInTime with time of 0 to make it be called on the next logic tick.

This is how I'd have your code setup and formatted:

local function drop(inst)
    local body = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
    if not ( body and (
        body:HasTag("backpack") or
        body:HasTag("heavy") or
        body:HasTag("tag") or
        (body.prefab and (body.prefab:find("amulet") ~= nil))
    ))
    then
        inst:DoTaskInTime(
            0,
            function()
                if not inst:IsValid()
                then
                    return
                end
                if inst.components.talker
                then
                    inst.components.talker:Say("It does not fit me.") -- Fixme: Use localization string tables here!
                end
                local item = inst.components.inventory:Unequip(EQUIPSLOTS.BODY)
                if item
                then
                    inst.components.inventory:GiveItem(item)
                    inst.AnimState:OverrideSymbol("swap_body", "nil", "nil")
                end
            end
        )
    end
end

 

Though I'm not sure if you need to do the override symbol call manually, I'd think that the unequip callback would handle such things.  This may not be the case, I didn't look.

Edited by CarlZalph
Negation

It works! I did change

(body.prefab and (body.prefab:find("amulet") == nil))

to this

(body.prefab and (body.prefab:find("amulet") ))

and it works. The only problem is when he puts on a hat the dialog is triggered when it shouldn't be. I can just remove the dialogue as it's an extra feature. 

Now he can equip amulets and can't equip armour which is what i was going for. Thank you! 

Edited by SirDoggo
3 hours ago, SirDoggo said:

It works! I did change


(body.prefab and (body.prefab:find("amulet") == nil))

to this


(body.prefab and (body.prefab:find("amulet") ))

and it works. The only problem is when he puts on a hat the dialog is triggered when it shouldn't be. I can just remove the dialogue as it's an extra feature. 

Now he can equip amulets and can't equip armour which is what i was going for. Thank you! 

Ah yup, was supposed to be "~=" for the not equals.  Your change or the explicit check both will work fine.

 

As for the hat triggering, are you listening to the 'equip' event?

If so, then you can check for if it was the body via the event's data:

if data.eslot == EQUIPSLOTS.BODY
then
    drop(inst)
end

Likewise the data.item would be used, so you could edit the function 'drop' to change it from getting the item to using the item passed directly.

Edited by CarlZalph
On 5/6/2019 at 8:52 PM, CarlZalph said:

if data.eslot == EQUIPSLOTS.BODY then drop(inst) end

I'm not sure where this is meant to go. I've put it in different places and it causes the game to crash. Lua file is in the post.

I've got this error:

[string "scripts/mainfunctions.lua"]:150: Error loading file prefabs/frank
[string "../mods/Frank/scripts/prefabs/frank.lua"]:53: 'then' expected near 'if'

frank.lua

You have not closed your function or if-statement. Look at the red line on the left-hand side, which shows where the "thing" starting at the cursor ends. Your function is never closed, meaning you're missing an "end" somewhere.

5cd1f075456fe_2019-05-0722_51_40-_C__Users_Jonas_Downloads_frank.lua-Notepad.thumb.png.c1046eb5dcdb73367d1bcd65bc2e57c7.png

Get yourself Notepad++ or something similar with this kind of highlighting, so you can see these problems.

I can highly recommend keeping your indentation tidy, as well. It really helps with the overview of what's happening, and it would even have helped you see this error, even without highlighting.

I think this works.
frank.lua

I changed how your checks are done a bit, for clarity. Also made use of the "data" parameter that CarlZalph was talking about.

Edited by Ultroman

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