Jump to content

How to toggle moggles vision with a hotkey?


Recommended Posts

Hello,

i've been searching for hours now and can't find the solution to what i guess might be a simple task for somebody who really knows how to mod with lua-code.
For myself i am just trying to mess around with it to see what i can get to work and what not and learning along the way, but this one i simply can't figure out.

I just want to add to an abandoned character (that i fixed before with the help of the forums and reuploaded the working version) the ability to switch on the moggles smell-e-vision with a hotkey like "X".

I thought it would be rather simple to do, but even after hours i am not able to find the code snippets i need and where to put them :(

Only thing left for me to do is ask here for a kind soul who knows how to do this. Would be much appreciated. 
I hope if i see how it's done i can understand how to do it myself in the future and for similar tasks.

(mod in question in attachments)

workshop-1662813017.zip

Edited by Fidu
Link to comment
Share on other sites

local SpawnHounds = function()
	-- Spawn the hounds and whatever. Probably make some kind of cooldown timer.
end

GLOBAL.TheInput:AddKeyDownHandler(GLOBAL.KEY_H, SpawnHounds)

KEY_H should correspond to the H key on the keyboard.

You can make the key customizable by adding an option in the mod configuration.

local SPAWN_HOUNDS_KEY = GetModConfigData("HoundSpawnKey")

local SpawnHounds = function()
	-- Spawn the hounds and whatever. Probably make some kind of cooldown timer.
end

GLOBAL.TheInput:AddKeyDownHandler(SPAWN_HOUNDS_KEY, SpawnHounds)

Then you'd have to find the list of possible keys in constants.lua. I'd try to find a mod with a customizable key, and steal their configuration :)

Keep in mind, though, that AddKeyDownHandler fires every frame where you hold down the key, while AddKeyUpHandler only fires when you let go of the key.

Link to comment
Share on other sites

Try this. I commented out self.forcenightvision, because I think you wanted to do that on the playervision component, but you're already doing that by calling the ForceNightVision function.

local _G = GLOBAL

local function GetKeyFromConfig(config)
    local key = GetModConfigData(config)
    return key and (type(key) == "number" and key or _G[key])
end

local key = GetKeyFromConfig("nv")

nvk = false

_G.TheInput:AddKeyUpHandler(key, function ()
    nvk = not nvk
	
	if nvk == true then
		_G.ThePlayer.components.playervision:ForceNightVision(true)
		_G.ThePlayer.components.playervision:SetCustomCCTable(BEAVERVISION_COLOURCUBES)
		--self.forcenightvision = true
		_G.ThePlayer.components.talker:Say("Night Vision enabled")
	else
		_G.ThePlayer.components.playervision:ForceNightVision(false)
		_G.ThePlayer.components.playervision:SetCustomCCTable(nil)
		--self.forcenightvision = false
		_G.ThePlayer.components.talker:Say("Night Vision disabled")
	end
end)
Link to comment
Share on other sites

First of all thank you very much for responding and offering some advice. Much appreciated.

I added the mod that i try to edit as an attachment to my first post, if you wanna have a look at it.

In my modinfo.lua there is such an optional keyhandler (i hope i use the term here correctly) defined.

How i understand it now is to add a second:
{
   name = "key",
   label = "toggle Moggle",
   hover = "The key to toggle smell-e-vision",
   options = 
    {

After that, edit a function like the one you showed me to the modmain.lua and just replace the houndspawn with something like "moggle". (still don't know how to let the game know that the function should activate moggle vision without equipping the item).

What i try to accomplish here is not the "standart nightvision" (i know how to do that, except for how it works with a keybind... but we are on that ;) ) but the smell-e-vision provided by the moggles (ingame item).

So yes, you are right on that. :)

And again, thank you so much for helping out. It's so much fun to learn more and more pieces trough editing and i just feel great when it really works in the end.

Edited by Fidu
Link to comment
Share on other sites

I am honestly not sure yet. Thanks for asking. I have to try and see what happens...

It was a question ^^, like a student asking for approval. :) 

might take a while for me because i need to read and double-check everything a couple of times to not screw everything up *sweatingsmily*

I am not confident yet in what i am doing even with your assistance.

I had a look at the playervision component and i didn't realize that the "Beavervision colourcubes" was the thing i was looking for, because i couldn't find the smell-e-vision part or moggles anywhere.

Link to comment
Share on other sites

ok here is what i did and it crashed on me while loading.

what did i do wrong or what am i missing here?

modinfo.lua:
 

Quote

configuration_options =
{
    {
        name = "key7",
        label = "Change Weapon Key",
        hover = "This is the key used to change tessaiga.",
        options =
        {
            {description="TAB", data = 9},
            {description="KP_PERIOD", data = 266},
            {description="KP_DIVIDE", data = 267},
            {description="KP_MULTIPLY", data = 268},
            {description="KP_MINUS", data = 269},
            {description="KP_PLUS", data = 270},
            {description="KP_ENTER", data = 271},
            {description="KP_EQUALS", data = 272},
            {description="MINUS", data = 45},
            {description="EQUALS", data = 61},
            {description="SPACE", data = 32},
            {description="ENTER", data = 13},
            {description="ESCAPE", data = 27},
            {description="HOME", data = 278},
            {description="INSERT", data = 277},
            {description="DELETE", data = 127},
            {description="END", data   = 279},
            {description="PAUSE", data = 19},
            {description="PRINT", data = 316},
            {description="CAPSLOCK", data = 301},
            {description="SCROLLOCK", data = 302},
            {description="RSHIFT", data = 303}, -- use SHIFT instead
            {description="LSHIFT", data = 304}, -- use SHIFT instead
            {description="RCTRL", data = 305}, -- use CTRL instead
            {description="LCTRL", data = 306}, -- use CTRL instead
            {description="RALT", data = 307}, -- use ALT instead
            {description="LALT", data = 308}, -- use ALT instead
            {description="ALT", data = 400},
            {description="CTRL", data = 401},
            {description="SHIFT", data = 402},
            {description="BACKSPACE", data = 8},
            {description="PERIOD", data = 46},
            {description="SLASH", data = 47},
            {description="LEFTBRACKET", data     = 91},
            {description="BACKSLASH", data     = 92},
            {description="RIGHTBRACKET", data = 93},
            {description="TILDE", data = 96},
            {description="A", data = 97},
            {description="B", data = 98},
            {description="C", data = 99},
            {description="D", data = 100},
            {description="E", data = 101},
            {description="F", data = 102},
            {description="G", data = 103},
            {description="H", data = 104},
            {description="I", data = 105},
            {description="J", data = 106},
            {description="K", data = 107},
            {description="L", data = 108},
            {description="M", data = 109},
            {description="N", data = 110},
            {description="O", data = 111},
            {description="P", data = 112},
            {description="Q", data = 113},
            {description="R", data = 114},
            {description="S", data = 115},
            {description="T", data = 116},
            {description="U", data = 117},
            {description="V", data = 118},
            {description="W", data = 119},
            {description="X", data = 120},
            {description="Y", data = 121},
            {description="Z", data = 122},
            {description="F1", data = 282},
            {description="F2", data = 283},
            {description="F3", data = 284},
            {description="F4", data = 285},
            {description="F5", data = 286},
            {description="F6", data = 287},
            {description="F7", data = 288},
            {description="F8", data = 289},
            {description="F9", data = 290},
            {description="F10", data = 291},
            {description="F11", data = 292},
            {description="F12", data = 293},
 
            {description="UP", data = 273},
            {description="DOWN", data = 274},
            {description="RIGHT", data = 275},
            {description="LEFT", data = 276},
            {description="PAGEUP", data = 280},
            {description="PAGEDOWN", data = 281},
 
            {description="0", data = 48},
            {description="1", data = 49},
            {description="2", data = 50},
            {description="3", data = 51},
            {description="4", data = 52},
            {description="5", data = 53},
            {description="6", data = 54},
            {description="7", data = 55},
            {description="8", data = 56},
            {description="9", data = 57},
        },
        default = 114,
    },
 
    {
        name = "key",
        label = "Change smell-e-vision Key",
        hover = "This is the key used to toggle smell-e-vision.",
        options =
        {
            {description="TAB", data = 9},
            {description="KP_PERIOD", data = 266},
            {description="KP_DIVIDE", data = 267},
            {description="KP_MULTIPLY", data = 268},
            {description="KP_MINUS", data = 269},
            {description="KP_PLUS", data = 270},
            {description="KP_ENTER", data = 271},
            {description="KP_EQUALS", data = 272},
            {description="MINUS", data = 45},
            {description="EQUALS", data = 61},
            {description="SPACE", data = 32},
            {description="ENTER", data = 13},
            {description="ESCAPE", data = 27},
            {description="HOME", data = 278},
            {description="INSERT", data = 277},
            {description="DELETE", data = 127},
            {description="END", data   = 279},
            {description="PAUSE", data = 19},
            {description="PRINT", data = 316},
            {description="CAPSLOCK", data = 301},
            {description="SCROLLOCK", data = 302},
            {description="RSHIFT", data = 303}, -- use SHIFT instead
            {description="LSHIFT", data = 304}, -- use SHIFT instead
            {description="RCTRL", data = 305}, -- use CTRL instead
            {description="LCTRL", data = 306}, -- use CTRL instead
            {description="RALT", data = 307}, -- use ALT instead
            {description="LALT", data = 308}, -- use ALT instead
            {description="ALT", data = 400},
            {description="CTRL", data = 401},
            {description="SHIFT", data = 402},
            {description="BACKSPACE", data = 8},
            {description="PERIOD", data = 46},
            {description="SLASH", data = 47},
            {description="LEFTBRACKET", data     = 91},
            {description="BACKSLASH", data     = 92},
            {description="RIGHTBRACKET", data = 93},
            {description="TILDE", data = 96},
            {description="A", data = 97},
            {description="B", data = 98},
            {description="C", data = 99},
            {description="D", data = 100},
            {description="E", data = 101},
            {description="F", data = 102},
            {description="G", data = 103},
            {description="H", data = 104},
            {description="I", data = 105},
            {description="J", data = 106},
            {description="K", data = 107},
            {description="L", data = 108},
            {description="M", data = 109},
            {description="N", data = 110},
            {description="O", data = 111},
            {description="P", data = 112},
            {description="Q", data = 113},
            {description="R", data = 114},
            {description="S", data = 115},
            {description="T", data = 116},
            {description="U", data = 117},
            {description="V", data = 118},
            {description="W", data = 119},
            {description="X", data = 120},
            {description="Y", data = 121},
            {description="Z", data = 122},
            {description="F1", data = 282},
            {description="F2", data = 283},
            {description="F3", data = 284},
            {description="F4", data = 285},
            {description="F5", data = 286},
            {description="F6", data = 287},
            {description="F7", data = 288},
            {description="F8", data = 289},
            {description="F9", data = 290},
            {description="F10", data = 291},
            {description="F11", data = 292},
            {description="F12", data = 293},
 
            {description="UP", data = 273},
            {description="DOWN", data = 274},
            {description="RIGHT", data = 275},
            {description="LEFT", data = 276},
            {description="PAGEUP", data = 280},
            {description="PAGEDOWN", data = 281},
 
            {description="0", data = 48},
            {description="1", data = 49},
            {description="2", data = 50},
            {description="3", data = 51},
            {description="4", data = 52},
            {description="5", data = 53},
            {description="6", data = 54},
            {description="7", data = 55},
            {description="8", data = 56},
            {description="9", data = 57},
        },
        default = 120,
    },
 

modmain.lua (i edited your code in at line 373)
 

Quote

local _G = GLOBAL

local function GetKeyFromConfig(key)
    local key = GetModConfigData(key)
    return key and (type(key) == "120" and key or _G[key])
end

local key = GetKeyFromConfig("key")

nvk = false

_G.TheInput:AddKeyUpHandler(key, function ()
    nvk = not nvk
    
    if nvk == true then
        _G.ThePlayer.components.playervision:ForceNightVision(true)
        _G.ThePlayer.components.playervision:SetCustomCCTable(BEAVERVISION_COLOURCUBES)
        --self.forcenightvision = true
        _G.ThePlayer.components.talker:Say("Night Vision enabled")
    else
        _G.ThePlayer.components.playervision:ForceNightVision(false)
        _G.ThePlayer.components.playervision:SetCustomCCTable(nil)
        --self.forcenightvision = false
        _G.ThePlayer.components.talker:Say("Night Vision disabled")
    end
end)

-- The character select screen lines

Quote

[00:02:05]: Loading mod: workshop-1662813017 (Inuyasha) Version:5.1    
[00:02:05]: Mod: workshop-1662813017 (Inuyasha)    Loading modworldgenmain.lua    
[00:02:05]: Mod: workshop-1662813017 (Inuyasha)      Mod had no modworldgenmain.lua. Skipping.    
[00:02:05]: Mod: workshop-1662813017 (Inuyasha)    Loading modmain.lua    
[00:02:05]: modimport: ../mods/workshop-1662813017/engine.lua    
[00:02:05]: MOD ERROR: workshop-1662813017 (Inuyasha): Mod: workshop-1662813017 (Inuyasha)    
[00:02:05]: Event data unavailable: lavaarena_event_server/lavaarena_achievement_quest_defs
[00:02:05]: [string "scripts/events.lua"]:21: table index is nil
LUA ERROR stack traceback:
        scripts/events.lua(21,1)
        =(tail call) ?
        ../mods/workshop-1662813017/modmain.lua(384,1) in main chunk
        =[C] in function 'xpcall'
        scripts/util.lua(711,1) in function 'RunInEnvironment'
        scripts/mods.lua(513,1) in function 'InitializeModMain'
        scripts/mods.lua(487,1) in function 'LoadMods'
        scripts/main.lua(302,1) in function 'ModSafeStartup'
        scripts/main.lua(375,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(26,1) in function 'SavePersistentString'
        scripts/modindex.lua(80,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(67,1) in function 'BeginStartupSequence'
        scripts/main.lua(374,1) in function 'callback'
        scripts/modindex.lua(545,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(519,1) in function 'Load'
        scripts/main.lua(373,1) in main chunk
[00:02:05]: [string "scripts/mainfunctions.lua"]:1089: variable 'global_error_widget' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        scripts/mainfunctions.lua(1089,1)
        =[C] in function 'GetPersistentString'
        scripts/quagmire_recipebook.lua(54,1) in function 'Load'
        scripts/main.lua(320,1) in function 'ModSafeStartup'
        scripts/main.lua(375,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(26,1) in function 'SavePersistentString'
        scripts/modindex.lua(80,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(67,1) in function 'BeginStartupSequence'
        scripts/main.lua(374,1) in function 'callback'
        scripts/modindex.lua(545,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(519,1) in function 'Load'
        scripts/main.lua(373,1) in main chunk
[00:02:05]: DoLuaFile Error: (null)
[00:02:05]: LuaError but no error string
[00:02:05]: Error loading main.lua
[00:02:05]: Failed mSimulation->Reset()
[00:02:05]: Error during game restart!
[00:02:06]: AddPortMapping(10999, 10999, 192.168.178.44) failed with code 403 (UnknownError)
[00:02:06]: AddPortMapping(10999, 10999, 192.168.178.44) failed with code 403 (UnknownError)
[00:02:09]: ModWorkshop::CancelDownloads clearing all unfinished downloads
[00:02:09]: Collecting garbage...
[00:02:09]: lua_gc took 0.02 seconds
[00:02:09]: ~ShardLuaProxy()
[00:02:09]: ~cEventLeaderboardProxy()
[00:02:09]: ~ItemServerLuaProxy()
[00:02:09]: ~InventoryLuaProxy()
[00:02:09]: ~NetworkLuaProxy()
[00:02:09]: ~SimLuaProxy()
[00:02:09]: ModWorkshop::CancelDownloads clearing all unfinished downloads
[00:02:09]: lua_close took 0.01 seconds
[00:02:09]: ModWorkshop::CancelDownloads clearing all unfinished downloads
[00:02:09]: saved ping_cache
[00:02:09]: [Steam] Auth ticket cancelled
[00:02:09]: CurlRequestManager::ClientThread::Main() complete
[00:02:09]: HttpClient2 discarded 0 callbacks.
[00:02:10]: steamnetworkingsockets_lowlevel.cpp (88) : Assertion Failed: SteamDatagramTransportLock held for 250.5ms!
[00:02:10]: Shutting down

 

Edited by Fidu
Link to comment
Share on other sites

It says this:

 ../mods/workshop-1662813017/modmain.lua(384,1) in main chunk

...meaning it's on line 384 in your modmain.lua.

I'm guessing it's this

_G.ThePlayer.components.playervision:SetCustomCCTable(BEAVERVISION_COLOURCUBES)

or this

_G.ThePlayer.components.playervision:SetCustomCCTable(nil)

I don't know if BEAVERVISION_COLOURCUBES is one of your variables, but if it isn't, remember that you are in modmain.lua which is outside the GLOBAL scope, so in order to use any of Klei's global variables, you need to prefix it with "GLOBAL.", so GLOBAL.BEAVERVISION_COLOURCUBES

Link to comment
Share on other sites

Colour Cubes does not exist in the server. So you need to make a net event to sync the client and the server.

Look at this post if you want an explanation of netvars.

Put these codes in modmain.lua
local function KeyCheckCommon(inst)
	return inst == GLOBAL.ThePlayer and GLOBAL.TheFrontEnd:GetActiveScreen() ~= nil and GLOBAL.TheFrontEnd:GetActiveScreen().name == "HUD"
end

local function RegisterKeyEvents(classified)
	local parent = classified._parent
	if parent.HUD == nil then return end

	local modname = GLOBAL.KnownModIndex:GetModActualName("modprettyname")
	local INFOKEY = GetModConfigData("mogglevisionkey", modname) or "KEY_X"
	GLOBAL.TheInput:AddKeyDownHandler(GLOBAL[INFOKEY], function() 
		if KeyCheckCommon(parent) then
			SendModRPCToServer(MOD_RPC["globaltest"]["togglemoggle"], parent) 
		end
	end) 
end

local function ToggleMoggleRPC(inst)
	inst.player_classified.mogglevision:set(not inst.components.playervision.forcenightvision)
	print(inst.components.playervision.forcenightvision)
end
AddModRPCHandler("globaltest", "togglemoggle", ToggleMoggleRPC)

local NIGHTVISION_COLOURCUBES = {
	day = "images/colour_cubes/mole_vision_off_cc.tex",
    dusk = "images/colour_cubes/mole_vision_on_cc.tex",
    night = "images/colour_cubes/mole_vision_on_cc.tex",
    full_moon = "images/colour_cubes/mole_vision_off_cc.tex",
}

local function ToggleMoggoleScreen(inst)
	local var = inst.mogglevision:value()
	inst._parent.components.playervision:ForceNightVision(var)
	inst._parent.components.playervision:SetCustomCCTable(var and NIGHTVISION_COLOURCUBES or nil)
end

-------------------------------------------------------------------------------------------------------------------------------------------

local function RegisterModNetListeners(inst)
	if GLOBAL.TheWorld and GLOBAL.TheWorld.ismastersim then
		inst._parent = inst.entity:GetParent()
	else
	end
	RegisterKeyEvents(inst)
	inst:ListenForEvent("setmogglevisiondirty", ToggleMoggoleScreen)
end

AddPrefabPostInit("player_classified", function(inst)
	inst.mogglevision = GLOBAL.net_bool(inst.GUID, "setmogglevision", "setmogglevisiondirty")
	inst.mogglevision:set(false)

	inst:DoTaskInTime(2 * GLOBAL.FRAMES, RegisterModNetListeners) 
	-- delay two more FRAMES to ensure the original NetListeners to run first.
end)

You need to rename "modprettyname" to your mod name you wrote in modinfo.lua
Also, rename "globaltest" as well. For this one, you can name it as you want. But make it uniquely.

And you can improve the modinfo like this :

local Keys = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "PERIOD", "SLASH", "SEMICOLON", "TILDE", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "INSERT", "DELETE", "HOME", "END", "PAGEUP", "PAGEDOWN", "MINUS", "EQUALS", "BACKSPACE", "CAPSLOCK", "SCROLLOCK", "BACKSLASH"}

local KeyOptions = {}
for i = 1, #Keys do KeyOptions[i] = { description = ""..Keys[i].."", data = "KEY_"..Keys[i] } end

configuration_options = {
	{
		name = "mogglevisionkey",
        label = "Change smell-e-vision Key",
        hover = "This is the key used to toggle smell-e-vision.",
		options = KeyOptions,
		default = "X",
	},

	...

You can make a custom field that is work as the constructor of options table. And be cautious that the config name is matched with the code I wrote above.

Edited by YakumoYukari
Link to comment
Share on other sites

Wow that is a lot to take in and i am not sure i can comprehend it all.

But ofc i am gonna try and see if it'll work out and what i can understand while doing so :)

Thank you so very much for your efforts and taking the time. It just feels good to see there are such nice people around who give a helping hand.
Made my day just seeing your responses.

now let's see...

Edited by Fidu
Link to comment
Share on other sites

Holy sh... Yukari, THAT's IT! it really works :o

I am baffled and so happy i wanna do somersaults at the same time.
And thank you ultroman for the assistance and insights you provided. I learned a lot even though i still can't understand everything about the code that was actually needed in the end.

I seriously don't wanna sound greedy here... and i am very glad and happy as it is, but what if we would like to take it even one step further...

There is a quanyecha.lua in which are coded 3 different forms the character can be in. So can we make it so that this mogglevision-function only works in one specific form?

I know that the character state i want this to work in is the quanyecha. Not in quanyecharumo and definitely not in quanyecharenlei state.

The function in question starts here (i guess) and is the third state mentioned. From what i understand everything after the inst.AnimState:SetBuild("quanyecha") specifies what is different in this state.

Quote


local function change(inst)
   
    if inst.components.sanity:GetPercent() < 0.3 
    --and not TheWorld.state.isfullmoon 
    then

        inst.AnimState:SetBuild("quanyecharumo")
        
        pugong = true
        
        inst.components.combat.damagemultiplier = 10.5
        inst.components.combat.min_attack_period = 0.3
        inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE * 0.5)
        inst.components.sanity.night_drain_mult = 0
        inst.components.health:StartRegen(1, 600)
        inst.components.sanity.dapperness = 0
        inst.components.health.absorb = 0.5

        if TheWorld.state.isnight or TheWorld:HasTag("cave") then
            inst.Light:Enable(true)
        else
            inst.Light:Enable(false)
        end
        
    

        
    elseif TheWorld.state.isfullmoon then    
        inst.AnimState:SetBuild("quanyecharenlei")
        pugong = false
        
        inst.components.combat.damagemultiplier = 0.3
        inst.components.combat.min_attack_period = 0.9
        inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE * 2)
        inst.components.sanity.night_drain_mult = 2
        inst.components.health:StartRegen(1, 600)
        inst.components.sanity.dapperness = 0
        --inst.components.sanity.dapperness = TUNING.DAPPERNESS_LARGE
        inst.components.health.absorb = -0.2
        
            inst.Light:Enable(false)
            
        local hand = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
        if hand ~= nil then
        if inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS).prefab  == "pot" 
        or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS).prefab  == "jingang"
        or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS).prefab  == "xueyu"
        or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS).prefab  == "mindao"
        or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS).prefab  == "longlin" then
        
        inst.components.inventory:Equip(SpawnPrefab("potie"))
            end
        end
    

    else
    inst.AnimState:SetBuild("quanyecha")
    

    
    pugong = false
    
    inst.components.combat.damagemultiplier = 5
    inst.components.combat.min_attack_period = 0.5
    inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE)
    inst.components.sanity.night_drain_mult = 1
    inst.components.health:StartRegen(5, 30)
    inst.components.sanity.dapperness = 0
    inst.components.health.absorb = 0
    
    --[[
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then 
        inst.Light:Enable(true)
    else
        inst.Light:Enable(false)
    end
    ]]
    inst.Light:Enable(false)
    end
        
    end


local common_postinit = function(inst) 
    
    inst.MiniMapEntity:SetIcon( "quanyecha.tex" )
--inst:AddTag("mihoowner")
inst:AddTag("quanyechaa")
    inst:AddTag("quanyecha_builder")
--inst:AddTag("quanyechabuilder")
inst:AddComponent("keyhandler")
 inst.components.keyhandler:AddActionListener("quanyecha", TUNING.WEAPON.KEYSEVEN, "ChanGe")
end

-- This initializes for the host only
local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "webber"
    inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    
inst.level = 0
    inst.components.eater:SetOnEatFn(oneat)
 
    applyupgrades(inst)

    
    
    
    inst.components.temperature.maxtemp = 92
    inst.components.temperature.mintemp = -5
    inst.components.temperature.overheattemp = 80
    inst.components.health:SetMaxHealth(80)
    inst.components.hunger:SetMax(100)
    inst.components.sanity:SetMax(60)
    

    --inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.4)
    --inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.45)
    
    --inst.components.sanity.ghost_drain_mult = 0.5
    

    inst.components.combat:SetDefaultDamage(15)
    
    
    inst:ListenForEvent("onhitother", OnAttack)

    
 


    
    --inst:AddComponent("reader")
    
inst.entity:AddLight()
    inst.Light:Enable(false)
        inst.Light:SetRadius(10)
        inst.Light:SetFalloff(0.75)
        inst.Light:SetIntensity(.2)
        inst.Light:SetColour(12/255,255/255,12/255)
    --local rt = 4
    --inst:DoPeriodicTask(rt, function() change(inst, rt) end)    
    inst:ListenForEvent("sanitydelta", change)

    inst.OnSave = onsave
    inst.OnPreLoad = onpreload
    inst.OnLoad = onload
    inst.OnNewSpawn = onload

end

return MakePlayerCharacter("quanyecha", prefabs, assets, common_postinit, master_postinit, start_inv)
 

 

If this is to much to ask for, please don't bother. You already helped out a lot and made me very happy.

But if you still wanna help i would love to see how this can be done and i would love to reupload the mod in that state with the new feature. 

But when it works in all of the character stages it wouldn't make any sense and i can only use it for myself.

ps: attached the now working mod with the ne function and the nicely cleaned up modinfo.lua (thanks again).

pps: had to undo the cleaned up modinfo because the weapon change key didn't work anymore. Trying to figure out why

 

workshop-1662813017.zip

Edited by Fidu
Link to comment
Share on other sites

ok i'll better stop before i mess it up completely, but i can't figure out how to make both keys work.

When i use the old structure i can use the weapon key just fine but only "X" activates the moggle-vision, can't change it, no more options ingame.

When i switch to the nicely new code for the modinfo.lua the mogglevisionkey is configurable and works fine, but the weapon change no longer works even though i still got the options menu to change it ingame.

:(

I found this in the modmain.lua

Quote


GLOBAL.TUNING.WEAPON = {}
GLOBAL.TUNING.WEAPON.KEYSEVEN = GetModConfigData("key7") or 121

--Transformation

local function ChangeWeaponFn(inst)

if inst:HasTag("playerghost") then return end

GetWeapon(inst)

--if inst.transformed then
--inst.Transform:SetScale(0.8, 0.8, 0.8)
if case == 1 then

if wuqipd.jingangon then...

 

I just don't understand why there is a "key7" Variable in the modinfo.lua BUT here it is a written Keyseven and the following getmodconfigdata("key7") is not working anymore.

-----------------------------

I got both of them to work now, but as a result my modinfo.lua now is a mess of both the new and old :/

modinfo.lua

Edited by Fidu
Link to comment
Share on other sites

Actually, if you write RegisterKeyEvents in modmain.lua, modname argument doesn't need. It needs when you get the config data outside of modmain scope since the only file that has its mod's info is modmain.

local function RegisterKeyEvents(classified)
	local parent = classified._parent
	if parent.HUD == nil then return end

	local INFOKEY = GetModConfigData("mogglevisionkey") or "KEY_X"
	GLOBAL.TheInput:AddKeyDownHandler(GLOBAL[INFOKEY], function() 
		if KeyCheckCommon(parent) then
			SendModRPCToServer(MOD_RPC["globalfun"]["togglemoggle"], parent) 
		end
	end) 
end


And I think have made a mistake about getting the key number into AddKeyDownHander. So the code should be 

local function RegisterKeyEvents(classified)
	local parent = classified._parent
	if parent.HUD == nil then return end

	local MOGGLE_KEY = GetModConfigData("mogglevisionkey") or "X"
	GLOBAL.TheInput:AddKeyDownHandler(GLOBAL["KEY_"..MOGGLE_KEY], function() 
		if KeyCheckCommon(parent) then
			SendModRPCToServer(MOD_RPC["inuyasha"]["togglemoggle"], parent) 
		end
	end) 
end

like this. It's because I have copied most of the codes from my mod.

Note that _G["KEY_X"] is 120 and _G["KEY_Y"] is 121.

Renamed the unnatural variable name and MOD_RPC's namespace(It doesn't really matter how you write in there but the first key is typically where to put your mod name.)

If you want more keys to raise an event, you need to make another KeyDownHandler in RegisterKeyEvents.

local MOGGLE_KEY = GetModConfigData("mogglevisionkey") or "X"
GLOBAL.TheInput:AddKeyDownHandler(GLOBAL["KEY_"..MOGGLE_KEY], function() 
  if KeyCheckCommon(parent) then
    SendModRPCToServer(MOD_RPC["inuyasha"]["togglemoggle"], parent) 
  end
end) 

Write lines like this and let it call RPC.
 

Edited by YakumoYukari
Link to comment
Share on other sites

Your first one works just fine, the same as before. I get what you are talking about.

But your second correction about the keydownhandler gives me an error in this line:

GLOBAL.TheInput:AddKeyDownHandler(GLOBAL["KEY_"..MOGGLE_KEY], function()

it's not really an issue because it works just fine with your first correction.

I think i understand what i might have to do to make another key event like this, and i thank you very very much for explaining to me which parts i would need.

But i still don't get the original code that was used for the weapon change key.

Any insights on my question about the "charactername".lua (quanyecha.lua)? is it possible to only make this mogglevision available when the character is in state quenyecha? I would love to do this and when the char is in one of the other states give out the phrase that he can't use it right now.

Edited by Fidu
Link to comment
Share on other sites

It is not that difficult to do. I suggest that to create one variable to store your character state and makes it change when you change the appearance. Like, 

inst.TrasformState = 1

if you set this as a normal form, just change this value when you press the key. Then check the state var when you press the key in ToggleMoggleRPC.

local function ToggleMoggleRPC(inst)
	if inst.TransformState == 1 then return end
	inst.player_classified.mogglevision:set(not inst.components.playervision.forcenightvision)
end

 

Link to comment
Share on other sites

It surely doesn't sound that difficult when you are explaining it. :)
And again i can't thank you enough for doing this.

I am so happy that i understand how you handled it to call the variable (transformstate) in the togglemogglerpc function.

Sadly what i still do not know is how i can store said state in such a variable like inst.TransformState = 1.

Do i just insert this under the inst.AnimState:SetBuild("quanyecha") in quanyecha.lua to make it work?

And then put inst.TransformState = 0 under the other two inst.AnimState:Setbuild("quanyecharenlei") and inst.AnimState:SetBuild("quanyecharumo")?

I tried it like this but it didn't work. I also tried what i said previously. Both didn't work. The mogglevision is still usable in all forms.

 

5ce51cdc36619_Annotation2019-05-22115516.thumb.png.3fdad1e3e7d77b54d01da23508086f41.png

Edited by Fidu
Link to comment
Share on other sites

For the case of your code, yes. You can just put inst.TransformState = 1 under SetBuild() should work.

inst.AnimState:SetBuild("quanyecharumo")
inst.TransformState = 1

Like this. This is exactly how to store the variable in lua. If a variable does not exist in the table, it will create it, otherwise, modify it.
And make, actually, define TransformState to 2 when "quanyecharenlei", 3 when "quanyecharumo" so you can later detect which state is it currently.

When inst.TransformState is 1, you know that it is the normal state. if 2 then "quanyecharenlei". And so on.
You can just define your logic and to use it somewhere else like this.

Edited by YakumoYukari
Link to comment
Share on other sites

I was so happy i jumped around because i felt like i had accomplished something, even though it was all your work and i simply try to understand what you are doing :)

Sadly it doesn't work. The moggle-vision is still available in all forms.

5ce52001ef2b7_Annotation2019-05-22120942.thumb.png.95f9252e2bd7b315d07e58679cc19506.png

I tried to insert a if inst.TransformState == 2 then nil but that just resulted in error. I thought maybe when i tell the togglemogglerpc function to not be usable when transformstate 2 or 3 are up it would work. But i don't know how the code must be like for that and just took a guess.

i hope you are not getting tired because of my inability to manage so little by myself .

Link to comment
Share on other sites

Spoiler

local function ToggleMoggleRPC(inst)
    if inst.TransformState == 1 then
    inst.components.talker:Say("I can smell it!", 2.5,true) return
    else
    inst.components.talker:Say("I can't smell anything!", 2.5,true)
    end
    inst.player_classified.mogglevision:set(not inst.components.playervision.forcenightvision)
    print(inst.components.playervision.forcenightvision)
end

This is what i did and you won't believe it ... IT WORKS! but in reverse XD

He is saying what he is supposed to say but now the mogglevision works NOT in TransformState 1 , but in Transform state 3 O_o

I am so close in getting it to work. I just don't know where to put the return and what it does here. :(

I tried your link. thank you for that. But i am not sure how long it will take for me to get to the point where i understand what i need to know.

Edited by Fidu
Link to comment
Share on other sites

This is what i got now, and no matter how hard i think about it i just can't figure out why it's working in reverse as it is supposed to.

I even tried with "if not..." but that didn't help. I tried a lot of other combinations too but i still can't make it work the right way.

So, please... would you be so kind to tell me what this last step is that i am doing wrong? please.

Quote

local function ToggleMoggleRPC(inst)
    if inst.TransformState == 1 then return
    inst.components.talker:Say("I can smell it!", 2.5,true)
    else inst.components.talker:Say("I can't smell anything!", 2.5,true)
    end
    inst.player_classified.mogglevision:set(not inst.components.playervision.forcenightvision)
    print(inst.components.playervision.forcenightvision)
end

He says "i can smell it" in the right state

he says "i can't smell anything" in the right sate

But why is the function in reverse? Why does the moggle vision work now in state 3 and does not work in state 1?

Edited by Fidu
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...