Wannahavefun Posted January 25, 2022 Share Posted January 25, 2022 Hi, I'm making a pair of glasses for my friend who loved farming in game, which function like Premirer gardeneer hat. But the purple color fx on "nutrientsvision" is quite intense as my friend want to wear it all the time, and only turn that vision on when needed. So I'm trying to make it so when you press R it will add "nutrientsvision" tag to the glasses so the vision turn on, and when you press R again it will dissapear. As I searched the forum for solution: I understand that to make custom key you have to make a keyhandler in components folder so I made one ( attach in the description ) And also I'll send you the lua file of my glass. But the action will happen in the code in modmain.lua and I have no idea what to write there, I've tried something but to add tag is something I've never done before. Thank you in advance for any suggestion that I can try, it looked in the beginning so simple but when I actually sit down and do it, it's so much more complicated. Best regards to you all keyhandler.lua glasses.lua Link to comment https://forums.kleientertainment.com/forums/topic/137095-custom-key-to-turn-nutrients-vision-on-off/ Share on other sites More sharing options...
Monti18 Posted January 25, 2022 Share Posted January 25, 2022 (edited) It's much easier than making an entire component for it, TheInput already has keyhandlers in it, you can just use these. Here is some code that does exactly what you're asking for: Spoiler local enabled = false GLOBAL.TheInput:AddKeyDownHandler(118,function() -- key v, change the value to the key that you want to use (found in constants.lua) if TheWorld then local data = {enabled = enabled} enabled = not enabled TheWorld:PushEvent("nutrientsvision",data) end end) As the widget is listening for this event, you can just push it with the data that is required and have it enable or disable the nutrientvision Edited January 26, 2022 by Monti18 specified the key Link to comment https://forums.kleientertainment.com/forums/topic/137095-custom-key-to-turn-nutrients-vision-on-off/#findComment-1535070 Share on other sites More sharing options...
Wannahavefun Posted January 26, 2022 Author Share Posted January 26, 2022 Hi Monti, thank you for your reply, I totally understand the PushEvent part, thank you so much!! However the glasses are craftable from a character mod, not a seperate mod itself, I'm sorry I wasn't be very specific on my post. So with this GLOBAL command, I should put it in modmain right? Then it will make the character activate nutrientst vision despise having that glasses equip or not. I want this to be a feature of the glasses only, hope I explain it clearly enough now... Also one reason I want to get into this component thing because I have an idea to make this farming glasses more feature, like when I press R it will active nutrientsvision, if I press V it will make plants around you grow like Wickerbottom book as the cost of sanity. I did research some mods but the custom button are very different on different mods so it was hard for me to copy and learn from them. ( sorry ) . If you could explain a bit more details on this matter that would be amazing for me. Link to comment https://forums.kleientertainment.com/forums/topic/137095-custom-key-to-turn-nutrients-vision-on-off/#findComment-1535344 Share on other sites More sharing options...
Monti18 Posted January 26, 2022 Share Posted January 26, 2022 Right this should be added to the modmain This function pushes the event nutrientsvision, but it is only listened to if you have the gardening hat or your glasses equipped, as only then the wigdet is active that is listening to this event. So it won't activate the nutrientvision as long as this widget is not active. TheInput:AddKeyDownHandler takes 2 arguments, the first one is the key that it listens to and the second one is the function that is run if this key is pressed down, which is why it's called KeyDownHandler. In constants.lua you can see the different values for the keys. The function that is called in run on the client, which means if you want to change things on the server, you need to use a ModRPC. If you only want to change things on the client, you can use ThePlayer to get access to the player on the client or TheWorld to get access to TheWorld. So let's take your example of making plants grow like wickerbottom: Spoiler --First you define the function that makes these plants grow local function GrowPlants(inst) -- take a look at books.lua to see how they did it end --Then we define this function as a ModRPC: AddModRPCHandler("Your_Mod_Name", "GrowPlants", GrowPlants) --This saves the function as a ModRPC --Then we add the keyhandler tha calls this RPC when pressing the key: GLOBAL.TheInput:AddKeyDownHandler(118,function() -- key v, change the value to the key that you want to use (found in constants.lua) SendModRPCToServer(MOD_RPC["Your_Mod_Name"]["GrowPlants"]) --This sends a signal to the server to run the function that is saved as this RPC end) You can do it this way for every other key you want to add. If you take a look at input.lua, you will see that there are more keyhandlers defined there, depending on the use case (for example AddKeyUpHandler which only fires if you let go of a key) I hope that you understand it better now, if you have more questions don't hesitate to ask Link to comment https://forums.kleientertainment.com/forums/topic/137095-custom-key-to-turn-nutrients-vision-on-off/#findComment-1535354 Share on other sites More sharing options...
Wannahavefun Posted January 26, 2022 Author Share Posted January 26, 2022 (edited) Ok I made it worked but it's working exactly like I was thinking. So my mod is a character mod, and this glasses is an item that the character can craft. So the modmain file is the modmain of character, not the glasses. I think what I need is a line that can say if TheWorld ( or the player? ), and if the equip item on the head is "glasses" then when key trigger it will activate nutrientsvision local enabled = false GLOBAL.TheInput:AddKeyDownHandler(114,function() -- key v, change the value to the key that you want to use (found in constants.lua) if TheWorld then if hat and hat.prefab=="glass" then local data = {enabled = enabled} enabled = not enabled TheWorld:PushEvent("nutrientsvision",data) end end end) I tried this, obviously didn't work haha ( in tears ) ... I also tried local enabled = false GLOBAL.TheInput:AddKeyDownHandler(114,function() -- key v, change the value to the key that you want to use (found in constants.lua) if TheWorld and hat and hat:HasTag("duan_glass") then local data = {enabled = enabled} enabled = not enabled TheWorld:PushEvent("nutrientsvision",data) end end) Then the game run, no error, but then the code didn't work, and clicking the item is kinda weird... I also added you on discord. Edited January 26, 2022 by Wannahavefun Link to comment https://forums.kleientertainment.com/forums/topic/137095-custom-key-to-turn-nutrients-vision-on-off/#findComment-1535390 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