Jump to content

[HELP] Replacing a broken item with another item in the same mod


Recommended Posts

Hi, y'all!

I wanted to try making some sort of weapon system for my character mod, but I've been having problems with a part of it.

(I'm not very experienced in the more complex parts of coding:wilson_hurt:)

As of right now, weapons can be made and upgraded using the game's builder, but I want to add a script to the weapon scripts that "reverts" the weapon back to its previous form if it "breaks." (Lvl 1 just breaks, Lvl 2/3 reverts to the previous level and lvl 4 is unbreakable, but eats a lot of rare materials.)

So for example in scripting terms (I think), if you run out of uses for a Level 3 Sword, the game will remove the "broken" item and give you a Level 2 sword to replace it. I've tried fiddling with the finiteuses component, but I don't seem to be getting anywhere. All of the weapons in this system are in the same mod.

Does anyone know of a way to get this working? I'm at wit's end here.

Thanks in advance!

P.S. I've attached the script files for the weapons I have so far as well as the modmain just in case they're needed. The "shin" swords are (currently) the level 2s.

prefabs.zip

Link to comment
Share on other sites

SetConsumption takes an action and a number. It should be:

inst.components.finiteuses:SetConsumption(SOME ACTION or nil, TUNING.SPEAR_USES * 1.5)

I would also HIGHLY recommend that you don't use TUNING.SPEAR_USES, since it might be changed by other mods. Make your own variable. Then you can also make it adjustable in the modsettings, if you so desire.

In the case of your SetOnFinished() call:

inst.components.finiteuses:SetOnFinished(inst.Remove)

SetOnFinished takes a function. I'm not versed in destroying things, but I'm guessing inst.Remove simply destroys the item. What you really want, is probably something like:

local function Downgrade(inst)
    inst.Remove()
    -- Do whatever to spawn the downgraded weapon, and somehow equip it.
end

...
-- then in fn()
inst.components.finiteuses:SetOnFinished(Downgrade)

 

Edited by Ultroman
Link to comment
Share on other sites

Here an example that should still works (will drop the prefab of your choice, in this case. If you want it to go in inventory you'll probably have to adjust the code. If you want to autoequip it i'm not sure it's possible).

Note : The order of the line matters in some case. I know that in some items, you should have the "inst.Remove()" line after the others, otherwise the item will be removed and the other part of the code will not work. I'm not sure it's the case for all items, but i know it's definitively important for some of them.

Link to comment
Share on other sites

 

9 hours ago, Lumina said:

Here an example that should still works (will drop the prefab of your choice, in this case. If you want it to go in inventory you'll probably have to adjust the code. If you want to autoequip it i'm not sure it's possible).

Note : The order of the line matters in some case. I know that in some items, you should have the "inst.Remove()" line after the others, otherwise the item will be removed and the other part of the code will not work. I'm not sure it's the case for all items, but i know it's definitively important for some of them.

Thanks for the help! I actually DO want to add it into the inventory, but the code from here just drops the downgraded sword. How would I change the code up so it goes into the inventory instead? (I already tried a few things. Either didn't work or caused a crash.)

Link to comment
Share on other sites

17 hours ago, Lumina said:

Maybe by doing something similar to the c_give command ? I don't know, never did this.

Alright, thank you very much for the help though! Here's hoping I can figure something out~

(Or someone else who knows how will drop in)

Link to comment
Share on other sites

you pretty much just need to spawn the item and use inst.inventory:GiveItem(item) or inst.inventory:Equip(item)

for example:

local owner = inst.components.inventoryitem.owner
local hat = SpawnPrefab("strawhat")
owner.components.inventory:Equip(hat)

local item = SpawnPrefab("cutgrass")
owner.components.inventory:GiveItem(item)

--replace inst with appropriate path to the prefab you are giving it to.
--as inst is the weapon or equipment not your character.
--you'll need to inst.components.inventoryitem.owner.components.inventory instead

You would call this from your onfinishedfn, so you would add w.e. it is you are using it for appropriately into the function. I can elaborate if you have questions.

Edited by IronHunter
Link to comment
Share on other sites

2 hours ago, IronHunter said:

you pretty much just need to spawn the item and use inst.inventory:GiveItem(item) or inst.inventory:Equip(item)

for example:


local owner = inst.components.inventoryitem.owner
local hat = SpawnPrefab("strawhat")
owner.components.inventory:Equip(hat)

local item = SpawnPrefab("cutgrass")
owner.components.inventory:GiveItem(item)

--replace inst with appropriate path to the prefab you are giving it to.
--as inst is the weapon or equipment not your character.
--you'll need to inst.components.inventoryitem.owner.components.inventory instead

You would call this from your onfinishedfn, so you would add w.e. it is you are using it for appropriately into the function. I can elaborate if you have questions.

Thank you so much for the help, it works perfectly!

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