Jump to content

Armor dropping item when depleted


Recommended Posts

I'm trying to make a helmet type item that when it gets destroyed it drops an item. (either to inventory or on ground)

I've been inspecting DST's script files but if I recall correctly, there isn't an item in-game that does this. Is something like this even doable?

If it's not, Maybe rather than hit durability I could use usage fuel durability. But again, can this be used to drop an item?

Sorry if I'm not making much sense, I'm unsure how to word this out properly.

Link to comment
Share on other sites

If your hat has the armor-component, you can listen for the event "percentusedchange" on it, and if the percentage it reports is <= 0, you spawn an item. You can find the current owner of the item and spawn the item in the player's inventory, and if you can't find an owner, you spawn the item where the hat currently is.

Link to comment
Share on other sites

I'm having trouble getting it to run the actual giveitem command (Maybe I'm doing it wrong)

91 : function checkGlassDura(inst, owner)
92 : 	local benglassperc = inst.components.armor:GetPercent()
93 : 	if benglassperc <= 0 then
94 :         inst.components.inventory:GiveItem("beardhair")
95 :     end
96 : end

However I get the error :

Quote

[string "../mods/benjamin/scripts/prefabs/benglass.l..."]:94: attempt to index field 'inventory' (a nil value)

Is there something I'm missing or doing wrong?

Sorry if I'm making rookie mistakes, coding isn't exactly my forte'

benglass.lua

(Additionally, Thankyou @Ultroman what you said did lean me in the right direction, took me a while to get to it because I was tryna figure something else out.)

Edited by woogigooie
Link to comment
Share on other sites

You're trying to give an item to inst, which is your item. You need to give it to the owner. If owner is nil, you need to spawn an item on the ground where the item is:

local x,y,z = inst:GetPosition()

if I'm not mistaken, will get you the position. Spawning an item should be easy to find the code for. Can't remember it right now.

And you're welcome :)

Edited by Ultroman
Link to comment
Share on other sites

Pigs have an inventory, so there'll be an owner if the pig wears it.

I'm not sure if e.g. explosives or things like Bearger's AoE damage items on the ground. You should always check for game-variables being nil before using it, anyway.

Edited by Ultroman
Link to comment
Share on other sites

Is it possible to use GiveItem on another mod item? I've been trying this multiple ways however it always returns an error.

Quote

[string "scripts/components/inventory.lua"]:640: attempt to index local 'inst' (a nil value)

It works on anything but items added through the mod.

Link to comment
Share on other sites

Yes it is. The error you're getting says that whatever inst you're giving it in GiveItem is nil, so you must be doing something wrong when utilizing it. Use some print-statements to make sure everything you're doing works as intended.

Edited by Ultroman
Link to comment
Share on other sites

  • Developer

@Ultromanis right, you should add some prints or check the log - it shows the callstack with the variables it's using in the calls, it should give you some understanding of what leads to it.

Maybe attach the entire log from the crash.

As an aside, I don't think that GiveItem("beardhair") would work, it expects an instance, not a string. It would be more along the lines of GiveItem(SpawnPrefab("beardhair")) I think.

Link to comment
Share on other sites

17 minutes ago, bizziboi said:

As an aside, I don't think that GiveItem("beardhair") would work, it expects an instance, not a string. It would be more along the lines of GiveItem(SpawnPrefab("beardhair")) I think.

Oops, didn't even catch that :)

Link to comment
Share on other sites

Oof sorry bout that waste of time. After spending some time fidgeting with other bits I came back to re-create the issue to send the logs just to have it suddenly work just fine... I even had this big thing of info typed out and stuff...

I believe the issue was somewhere in my prefab name defining spots. As after messing with all of that it works fine. Thank you both for the help in these matters and I'm sorry for wasting time. :?

I did have it written as "GiveItem(SpawnPrefab("beardhair"))" shortly after i'd posted my file. Apologies, I should have updated that accordingly.

 

I do however have one more question: When looking at an item in the crafting menu, it shows the materials required to make it. However one of the items I've added doesn't have an image here. It just shows a blank box.

wqWfSH2.png

Perhaps this is another oversight on my part. The inventory icon appears fine and it's texture does appear on the ground when dropped.

I've attached the modmain file and broken glasses file as well. modmain.lua benglassbutbroken.lua

(sorry again about that previous time waster :? you've been super helpful.)

Link to comment
Share on other sites

  • Developer
Ingredient = Class(function(self, ingredienttype, amount, atlas, deconstruct)
	...
    self.atlas = resolvefilepath(atlas or "images/inventoryimages.xml")
	..
end)

In other words, if your ingredient is not in "images/inventoryimages.xml" you have to specify the atlas.

Ingredient("benglassbutbroken",1, "images/inventoryimages/benglassbutbroken.xml")

...I did not test this.

 

Link to comment
Share on other sites

That makes alot of sense actually, Sorry for not inspecting the Ingredient definition myself!

That did the trick very nicely! Thank you so much!

Thank you both, It's working how I wanted it to and it's thanks to you. @Ultroman @bizziboi

I appreciate you guys taking the time to assist me on this. :D

kvtMqGI.png

This has been quite the learning experience!

Link to comment
Share on other sites

  • Developer

Good times. Yeah, most of the times what you need is there, you just gotta get good at finding it. 

One step at a time :)

Credit to @Ultroman here, he gives people just enough info to help them find the answer, instead of just telling you the solution, stimulating you to find the actual solution yourself and learn the process of solving similar problems so that next time you're prepared to dive a bit deeper.

Link to comment
Share on other sites

You're very welcome :) But it's mostly thanks to you doing hard work, and doing your best to educate yourself, ask good questions, and your ability to fill in the blanks of our non-complete solution proposals. I see A LOT of people here asking for help, and then getting annoyed when we don't just give them the whole solution, and thankfully you aren't one of them. I have a life too, currently doing a master thesis, so I don't have very much time, but I do what I can to point people in the right direction.

EDIT:

6 minutes ago, bizziboi said:

Credit to @Ultroman here, he gives people just enough info to help them find the answer, instead of just telling you the solution, simulating you to find the actual solution yourself and learn the process of solving similar problems so that next time you're prepared to dive a bit deeper.

Wow, thanks a lot for that credit. Didn't know I was being noticed :D Funny that we wrote about the same thing there simultaneously. And that is exactly what I try to do. I find it much more helpful in the long run. You don't learn anything from copy/pasting.

Edited by Ultroman
Link to comment
Share on other sites

  • Developer
10 minutes ago, Ultroman said:

You don't learn anything from copy/pasting

Indeed,it's the whole 'give a man a fish and he'll eat for a day, teach a man to fish and you feed him for a lifetime' thing. 

Yes, I notice :)

(and yep, it was striking to see the reply you had typed right after I pressed send :p )

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