Jump to content

How to add a new key function


Recommended Posts

@Purswader, I've done considerable amount of testing and have come to the conclusion that further testing is needed. Your gir.lua file should be working and both the client and server should be able to see the print test, however, they should see no changes to the character itself.

 

Edit:

Tomorrow I will do further testing with RPC's.

 

Edit 2:

RPC functionality while will work, to get it to work properly at this time is extremely difficult and the time would probably be better spent elsewhere until the developers add support.

Edited by Kzisor
Link to comment
Share on other sites

Tryied again and it wont work with or without "if data.key == KEY_T" on client side, on host side both versions are fully functional.

What du you mean by RPC?

 

Make sure both client and server are running the most up to date mod version. Make sure keyhandler is in the scripts/components folder. If it's still not working after that I will upload a fully working version of what I am using for you to test and see if it's working.

 

RPC = Remote Procedure Call

 

It's how clients communicate with servers, but Klei hasn't gotten the support fully implemented just yet to allow modders to create our own custom RPC's.

 

EDIT:

I've uploaded the mod I am using which works on both client and server as far as Key Handling goes. I have not gotten the RPC's to work and I probably won't try until Klei offers better support for them.

Edited by Kzisor
Link to comment
Share on other sites

Make sure both client and server are running the most up to date mod version. Make sure keyhandler is in the scripts/components folder. If it's still not working after that I will upload a fully working version of what I am using for you to test and see if it's working.

 

RPC = Remote Procedure Call

 

It's how clients communicate with servers, but Klei hasn't gotten the support fully implemented just yet to allow modders to create our own custom RPC's.

 

EDIT:

I've uploaded the mod I am using which works on both client and server as far as Key Handling goes. I have not gotten the RPC's to work and I probably won't try until Klei offers better support for them.

 

Yeah, i hope they offer support soon. By looking at your code, i can see, that i have no chance to code that the right way :)

 

Link to comment
Share on other sites

@Purswader, sorry, it seems I forgot to save the modmain.lua file; the RPC code wasn't actually working it was me fiddling with it to see if I could make it work.

 

Here is the real modmain.lua to make the rest of the code work just overwrite the code in it and save it and test it out.

 

--[[ Created by: Ysovuka/Kzisor ]]   local function OnKeyPressed(inst, data)--if data.player == GLOBAL.ThePlayer thenif data.key == GLOBAL.KEY_T thenprint("KEY_T has been pressed.")end--endend Wilson = function(self)self:AddComponent("keyhandler") self:ListenForEvent("keypressed", OnKeyPressed) return selfend AddPrefabPostInit("wilson", Wilson)
Link to comment
Share on other sites

@Purswader, I actually figured out an easy way of 'hacking' this together. I looked at Up and Away's code and I have to say @simplex did a really great job on putting that together.

 

Here is my new modmain.lua

local function OnKeyPressed(inst, data)	--if data.player == GLOBAL.ThePlayer then		if data.key == GLOBAL.KEY_T then			print("KEY_T has been pressed.")			if GLOBAL.TheWorld.ismastersim then				-- Since we are the server, do the action on the server.			else				GLOBAL.SendRPCToServer(GLOBAL.RPC.DoWidgetButtonAction, GLOBAL.ACTIONS.SETHEALTH.code, inst, GLOBAL.ACTIONS.SETHEALTH.mod_name)			end		end	--endendWilson = function(self)	self:AddComponent("keyhandler")	self:ListenForEvent("keypressed", OnKeyPressed)		return selfendAddPrefabPostInit("wilson", Wilson)local SETHEALTH = GLOBAL.Action()SETHEALTH.str = "SetHealth"SETHEALTH.id = "SETHEALTH"SETHEALTH.fn = function(act)        	act.target.components.health:SetCurrentHealth(1)			act.target.components.health:DoDelta(0)        	return trueendAddAction(SETHEALTH) 

 

Basically what I did was assign a new action which will be performed on the server by sending the proper RPC code to the server.

 

In this code we send the RPC.DoWidgetButtonAction, and send our new action information. The information we must send is the code of the action, the instance of our character, and the mod_name of the action. As seen below.

 

GLOBAL.SendRPCToServer(GLOBAL.RPC.DoWidgetButtonAction, GLOBAL.ACTIONS.SETHEALTH.code, inst, GLOBAL.ACTIONS.SETHEALTH.mod_name)

 

Whenever the action is performed on the server it will do exactly what you are wanting it to do on the clients machine.

Edited by Kzisor
Link to comment
Share on other sites

@Purswader, you are using the code in the wrong manner and that is why it's not working correctly.

 

			local silent = true			if (inst.strength=="normal" or inst.strength=="mighty") then 				local damage_mult = 0.9				local health_max = 150				local hunger_rate = 1.7				local scale = 0.9				local speed = 1.1				inst.AnimState:SetBuild("gir_dog")				inst.strength="dog"				inst.Transform:SetScale(scale,scale,scale)				inst.components.hunger:SetRate(hunger_rate*TUNING.WILSON_HUNGER_RATE)				inst.components.combat.damagemultiplier = damage_mult				inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * speed)				inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * speed)				inst.components.sanity.dapperness = 0				local health_percent = inst.components.health:GetPercent()				inst.components.health:SetMaxHealth(health_max)				inst.components.health:SetPercent(health_percent, true)			elseif inst.components.sanity.current < (85) then                becomenormal(inst, silent)			elseif inst.components.sanity.current > (85) then				becomemighty(inst, silent)			end 

 

 

This code should be in it's own action. You will call that action within the following function:

local function OnKeyPressed(inst, data)    --if data.player == GLOBAL.ThePlayer then        if data.key == KEY_T then            print("KEY_T has been pressed.")            if TheWorld.ismastersim then                -- Since we are the server, do the action on the server.                -- CALL THE ACTION HERE!!!            else                -- SEND THE ACTION CALL TO THE SERVER HERE!!!                GLOBAL.SendRPCToServer(GLOBAL.RPC.DoWidgetButtonAction, GLOBAL.ACTIONS.SETHEALTH.code, inst, GLOBAL.ACTIONS.SETHEALTH.mod_name)            end        end    --endend  

 

Also in the code the following will never work because you haven't properly added a new custom SETHEALTH action.

                GLOBAL.SendRPCToServer(GLOBAL.RPC.DoWidgetButtonAction, GLOBAL.ACTIONS.SETHEALTH.code, inst, GLOBAL.ACTIONS.SETHEALTH.mod_name) 

 

 

You must add a new action for each additional action you want called on the server. Until you provide the custom action SETHEALTH the client/server communication will never work properly; this is why it is not working for the client.

 

As far as it not working correctly on the host machine, that is where the following line from the keypress function comes into play.

    --if data.player == GLOBAL.ThePlayer then 

We want to only apply this to our character because we are the host. Simply remove the comment from that line and the -- end line associated with it so that it only functions on your current character and not all instances of the character in the game.

 

Link to comment
Share on other sites

@Purswader, Yes you can put all the things you want to change in a single action.

 

It means that you must implement the functionality which will call your action on the server.

 

Example from container.lua:

BufferedAction(inst.components.container.opener, inst, ACTIONS.COOK):Do()
Link to comment
Share on other sites

@Purswader, it was not meant as disrespect. If your friend is using a different language operating system it could have an adverse affect on the keystrokes. Example: In French and German languages the keystrokes for certain normal keys are different than on an English keyboard. Very recently a different game had a similar issue when implementing keystrokes across multiple nationalities. If your friend is using the same operating system language as you are, I suggest obtaining the log file from your friend to see if there are any errors in it.

 

As far as the transforming back after your friend joins; that is not something that is dealing with the code that I provided. I think that is a separate issue and one way to test that theory is to set up a global function which you call from the console to do the transformation and have your friend join the server.

Link to comment
Share on other sites

@Kzisor, i don't feel disrespected. infact we both use a german keyboard. And i think u dont understand the other problem. If he joins and i press t i can see the code working and the changes are made on the character, but just for a second then it changes back to normal. that just happens when playing with together. So so code is somehow working.

Link to comment
Share on other sites

@Purswader, yeah I understood the second issue. That's why I proposed setting up a second function which you call from the console to test to see if it's not your actual transformation code not working correctly.

 

As far as the code not working on the client, there is no reasoning as to why it shouldn't be working on your friends computer. The only thing I can say is double check to make sure they have the updated mod files installed on their computer. If you are using the workshop make sure you have in your modinfo.lua file the "all_clients_require_mod = true" setting implemented. If that still doesn't work then, there is something else playing a factor into the cause and I'm not sure what it could be causing the issue.

Link to comment
Share on other sites

@Kzisor how about i upload my files anywhere, so you can try on your own. i tried with two of my friends and switched positions as client and host. Nothing has worked so far and i think i can't be of any help, just fiddle around unsure what i am doing exactly.

 

Edit:

Here you go
http://www.share-online.biz/dl/R2V7Z4INCYP

Edited by Purswader
Link to comment
Share on other sites

@Purswader, sorry today is my only day off so I had other things I had to do before I got a chance to look over your mod. 

 

Results:

I was able to reproduce the issue where it would not work correctly on the client as it works on the server. The only thing I can think of is it is dealing with how you set up your masterpostinit function. Components should only be ran or changed on the server, not on the client. The exception to this rule is the keyhandler component which I created; this particular component must be active on both the client and the server.

 

I do not have enough time to fully delve into the code to give a proper solution to the problem today, but I will have time tomorrow. If you will wait until then, I will have a fully working functional solution for you.

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