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.

bobmittens

Issues with creating a character specific item (need help)

Recommended Posts

bobmittens    12

I have been making a mod character and have made the item I want to be character specific (a bedroll.) I tried looking around in the vanilla game's scripts for other character specific items, but they don't seem to have much. So I began reading through a thread, which I linked below. I decided to start where the original poster in that thread started by using the script he originally posted and tailoring it to my character with the idea that I could trouble shoot any problems the same way the users in the thread did. Although the script doesn't crash my game and does cause characters to be unable to hold the item, it makes all characters including my own drop it. I tried a couple things mostly going to the (if player.prefab ~= "wisia" then) code and changing what is set in the quotation marks. What i found is that when the prefab name is set to that of my character, they won't spawn with the item or say the text for being unable to hold the item, but if the value is set to something else such as "wilson" I will start with the item dropping off my character and them saying the appropriate text. 

The short of it is, does any one know how to fix this issue, or a better set of code that would garner the same results without the hassle? Thank you for your time.

 

I also added the item's prefab if you want to poke around in there. My friend wanted me to hurry up making the mod like 6 months back and got someone who knew what they were doing to write the bed roll code since i was having trouble so i can only kind of guess and assume what each part of the code does but not why or how it affects other parts. meaperroll.luameaperroll.lua

    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "meaperroll"
	inst.components.inventoryitem.atlasname = "images/inventoryimages/meaperroll.xml"

	inst.components.inventoryitem.cangoincontainer = false  
    inst.components.inventoryitem.onputininventoryfn = function(inst, player) 
    if player.prefab ~= "wisia" then 
        inst:DoTaskInTime(0.1, function()
        
        player.components.inventory:DropItem(inst)
        player.components.talker:Say("It seems to slip right out of my hands!")
    end)
    end
    end

 

Share this post


Link to post
Share on other sites
K1NGT1GER609    120

I use to implement that code into my mods but I later found out that it would crash the game if you put that item in a chest/chester and/or someone else tried to get it (or something like that). I could help you out with another way of achieving character specific items (and without the crash) but its a little complicated. If not i'd recommend changing the code to

inst.components.inventoryitem.onputininventoryfn = function(inst, player)
    if not player:HasTag("wisia")  then
    inst:DoTaskInTime(0.1, function()
    player.components.inventory:DropItem(inst)
    player.components.talker:Say("It slips right out of my hands.")
    end)
    end
    end

while having this in your character lua file:

local common_postinit = function(inst) --PLEASE READ THIS NOTE, its reference on where it goes
    inst:AddTag("wisia")--add this line of code, its usually implemented on new character templates

but you'll have that crash you'll encounter later on. Otherwise your call if you want to try the alternative route.

Share this post


Link to post
Share on other sites
bobmittens    12
59 minutes ago, K1NGT1GER609 said:

I use to implement that code into my mods but I later found out that it would crash the game if you put that item in a chest/chester and/or someone else tried to get it (or something like that). I could help you out with another way of achieving character specific items (and without the crash) but its a little complicated. If not i'd recommend changing the code to

inst.components.inventoryitem.onputininventoryfn = function(inst, player)
    if not player:HasTag("wisia")  then
    inst:DoTaskInTime(0.1, function()
    player.components.inventory:DropItem(inst)
    player.components.talker:Say("It slips right out of my hands.")
    end)
    end
    end

while having this in your character lua file:

local common_postinit = function(inst) --PLEASE READ THIS NOTE, its reference on where it goes
    inst:AddTag("wisia")--add this line of code, its usually implemented on new character templates

but you'll have that crash you'll encounter later on. Otherwise your call if you want to try the alternative route.

So i've done this and its fixed my issues. But you are correct, attempting to place the item back in a chest or Chester instant crashes the game. SO if you'd be willing, id like to know more about this alternate option. Something else i should add is that implementing this code also made the inventory icon for the item become invisible. 

Share this post


Link to post
Share on other sites
Ultroman    340

This post at the bottom of the thread you posted has a better solution, using GrandOwner. It still has some problems, though, but you might have an easier time fixing those (personally, I'd just make it not be able to be in containers, and/or make the item automatically drop out of the backpack it's in if the backpack is dropped or given to another player.

Share this post


Link to post
Share on other sites
bobmittens    12
8 minutes ago, Ultroman said:

This post at the bottom of the thread you posted has a better solution, using GrandOwner. It still has some problems, though, but you might have an easier time fixing those (personally, I'd just make it not be able to be in containers, and/or make the item automatically drop out of the backpack it's in if the backpack is dropped or given to another player.

So attempting to use that set of code hits me with an error. Maybe i set it up wrong in the lua file could you take a look?meaperroll.lua

Capture.thumb.PNG.e12d19b635e2d32a748e9e817c45f533.PNG

Share this post


Link to post
Share on other sites
K1NGT1GER609    120

think its like this:

inst.components.inventoryitem.onputininventoryfn = function(inst, player)
    local owner = inst.components.inventoryitem:GetGrandOwner()
    if owner.components.inventory and owner.prefab ~= "star" then
        inst:DoTaskInTime(0.1, function()
        owner.components.inventory:DropItem(inst)
        owner.components.talker:Say("It seems to slip right out of my hands!")
        end)
    end
    end

meaperroll.lua

Share this post


Link to post
Share on other sites
bobmittens    12
11 minutes ago, K1NGT1GER609 said:

think its like this:

inst.components.inventoryitem.onputininventoryfn = function(inst, player)
    local owner = inst.components.inventoryitem:GetGrandOwner()
    if owner.components.inventory and owner.prefab ~= "star" then
        inst:DoTaskInTime(0.1, function()
        owner.components.inventory:DropItem(inst)
        owner.components.talker:Say("It seems to slip right out of my hands!")
        end)
    end
    end

So this worked except now the item's inventory icon is invisible so almost there. Here is the current lua file. meaperroll.lua

Share this post


Link to post
Share on other sites
Ultroman    340

Yeah, that should work. But with the caveats mentioned.

Here's another solution, which only cares about making anyone but a particular player unable to pick up the item.

You'll still have the problem with it being transferable in a backpack, though (I think), but as soon as the player tries to pick up the item from the backpack, I don't think it will let them...not sure, though.

Share this post


Link to post
Share on other sites
bobmittens    12
16 minutes ago, K1NGT1GER609 said:

Think your missing this line:

inst.components.inventoryitem.atlasname = "images/inventoryimages/meaperroll.xml"

meaperroll.lua

That fixed it, and i checked and there doesn't seem to be any chest or backpack related crashes. I think that just making so that other players can't pickup items is enough to get the idea across. any player who goes through a complicated process of moving it into their own backpack and giving it to another probably wouldn't want to play a simple character like mine anyway. So thank you K1NGT1GER609 and Ultroman. Im going to set this thread as solved but ill make sure to ask you if i find any other issues. Thank you again.

 Capture.PNG.c02655357f9a62dea1272b4e17b436a3.PNG 

Share this post


Link to post
Share on other sites