Locke_C Posted July 11, 2024 Share Posted July 11, 2024 Hello, first post on this forum, apologies if this is weird to read! I am trying to make a character mod where my character cannot equip body and chest items with the tag "armor", as that is his downside. (I understand this is an odd downside gameplay-wise, but I would like to figure this out for my own curiosity and to better understand modding this game.) I have tried looking through game code and other mod code to see if there was something that would give me insight on how to do this. I can't find anything in the base game that is useful for my needs. I looked at Lucy's code but that doesn't quite work as you can't pick her up in your inventory whatsoever. Armor is often already in your inventory you should just be unable to equip it. I tried to look at a mod that automatically un equips items when they are low durability, to see if I could make it instantly un equip "armor" upon being equipped. I've found blocks of code that may be what I need but I'm not sure if I fully understand the code. I've used this code in my character but it does not work in a weird error. I ask for help either understanding why the code does not work, any other mods that do something similar, or any other forum posts that I've missed that cover this same topic. My code is located in my prefab file titled after the character's name. local function Unequip (inst) if inst.replica.equippable:IsEquipped() then GLOBAL.ThePlayer.replica.inventory:ControllerUseItemOnSelfFromInvTile(inst) end end local function OnEquip(inst, data) local item = inst.entity:GetParent() if data ~= nil and data.item ~= nil then if data.item:HasTag("armor") then Unequip(item) end end end The above code is located near the top with other local functions, and the lower code is located in master_post_init inst:ListenForEvent("equip", OnEquip) My understanding is that it checks if you equip something, if you do then it runs OnEquip. OnEquip checks for if your item and data exists, then checks if it has the tag "armor". If it does then it runs Unequip, while sending over: local item = inst.entity:GetParent() I do not understand what this is needed for but the mod I used code for specifically did this for sending the item's contents to their Unequip function. When the item is sent over, it checks if it is equipped, and if it is then it does "ControllerUseItemOnSelfFromInvTile(inst)", which uses the item therefore unequipping it. The mod I used the unequip code from is (Auto-Unequip on 1%), and that mod seems to work. But in my mod whenever I try to load a server it fails to start, the crash logs say " variable 'GLOBAL' is not declared ", but this was no issue for the (Auto-Unequip on 1%) mod. I have been trying to figure this system out for practically the entire day by reviewing other mod's code, looking at old forum posts, trying to understand code and concepts such as "replica", and of course looking over and over at the game's files and code. I do not understand what the issue is, and surely there is a better way to go about this that I would be appreciative to figure out. Please, if anyone knows what the issue is, or an alternative I could use the help. Link to comment https://forums.kleientertainment.com/forums/topic/158174-make-equippables-of-a-certain-tag-unable-to-be-equipped/ Share on other sites More sharing options...
alainmcd Posted July 11, 2024 Share Posted July 11, 2024 Hi, welcome to the forums! A few explanations: The mod you're referring to is a client-only mod, which is why it uses the replica and calls a controller function, so it simulates user interaction. Since you're making a character mod, the server needs the mod too, so we can delegate this logic to the simulation. My understanding is that replicas hold and manage some data on the client, e.g. giving info to HUD elements or handling player input and such, but that's about as much as I know. Actions normally occur server-side, you wouldn't want to allow the client to just manipulate what they feel like. The GLOBAL not being found is an out of scope error. I'm not sure, but I assume the mod sandbox doesn't allow to call GLOBAL from anywhere, but it's definitely available in the modmain.lua file, probably where you found the code in the linked mod. -- Here's a suggestion: local function OnEquip(inst, data) if data ~= nil and data.item ~= nil then inst.components.inventory:Unequip(data.item.components.equippable.equipslot) end end I don't see an easy or clean way to prevent the character from trying to equip armour in the first place, which would probably be nicer. To make clear what's happening, you might prefer to drop the armour with the DropItem function from the inventory component (note that it takes the item as an argument) or have the character announce what's happening (e.g. "I don't need that!"). Happy modding! Link to comment https://forums.kleientertainment.com/forums/topic/158174-make-equippables-of-a-certain-tag-unable-to-be-equipped/#findComment-1734532 Share on other sites More sharing options...
Locke_C Posted July 11, 2024 Author Share Posted July 11, 2024 Thank you for the help! Yeah, replicas don't seem necessary, I was just too hyperfocused on the other mod's code when this is located somewhere else. I looked through (This Basic Guide) to understand more about the concept, and other parts of Don't Starve Together's coding. This code is of course located in the prefab file, and not modmain, and is a server-side mod, not a client-side. Replica doesn't seem to need to be used at all, and I guess you can't call GLOBAL from the character prefab file. Ultimately, I guess it doesn't matter if the item drops on the ground when trying to equip it, as they cannot use it, can still hold it in their inventory if the slot is already taken, and it provides more feedback that you cannot equip the item. local function OnEquip(inst, data) if data ~= nil and data.item ~= nil then if data.item.components.armor ~= nil then inst.components.inventory:DropItem(data.item) inst.components.talker:Say(({"That will not be necessary.", "I don't have any need for armor.", "My barrier is far better."})[math.random(3)]) -- inst.components.inventory:Unequip(data.item.components.equippable.equipslot) end end end This is the code I have at the moment thanks to your suggestion, and it seems to work! I originally had the 2nd if statement be [[ if data.item:HasTag("armor") then ]] but that didn't seem to work for some reason, despite that being almost the exact same code used in wolfgang's prefab for lifting heavy objects. However, checking if the component armor exists does the exact same thing. I also made the talker:Say cycle through 3 random lines, I'm not sure if there is a better way to do it but it works perfectly. Thank you for your help, I'm just putting the exact code I used here in case anyone else needs to do something similar in the future, or if I find an issue later down the line. 1 Link to comment https://forums.kleientertainment.com/forums/topic/158174-make-equippables-of-a-certain-tag-unable-to-be-equipped/#findComment-1734634 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now