Jump to content

How to run a client side thing only?


Recommended Posts

Minutes ago, I tried the Hide the Hud mod, and pressing H to hide the hud worked only for the host.

 

I tried:

GLOBAL.TheInput:AddKeyDownHandler(GLOBAL.KEY_K, function(key) print("Hello world") end)

But it only worked for the host.

And print(TheInput:GetWorldEntityUnderMouse()) works for the client.

 

I have tried adding functions, commands, and components to things from the client side and they don't work if the server doesn't have them, or only the server can do them.

 

For example, my attachment. It works for the host but it doesn't for the client. Why? The OnUpdate only runs on the host and it doesn't on the client? Where can I attach a periodic task for the client?

 

 

Test.zip

Link to comment
Share on other sites

@Kzisor, yes, I don't know why it didn't work for me.

 

edit: I thought it was client side only. Regardless, my point still stands, I take the postinits only work if you are the host then? How can I make the client run custom commands based on what happens to him or the world?

 

For example, I want the client to press the key K and run ThePlayer:EnableMovementPrediction(false).

Or press K and run print("Hello").

Edited by DarkXero
Link to comment
Share on other sites

@DarkXero, I have not looked into making that happen. I think I know of a few ways of doing it, however, I was working on getting nightvision working fully on non-dedicated servers, which is impossible with how the current game mechanics work. I will look into making the Toggle HUD client side only and create a guide on how it's done. 

Link to comment
Share on other sites

@DarkXero, you will need the following code to make it work correctly on the client*.

 

local prediciton = truelocal function OnKeyPressed(key)    if key == GLOBAL.KEY_K then        if prediction then            GLOBAL.ThePlayer:EnableMovementPrediction(false)            prediction = false        else            GLOBAL.ThePlayer:EnableMovementPrediction(true)            prediction = true        end    endendlocal function OnRawKey(key, down)  	if (key and not down) and not GLOBAL.IsPaused() then      	OnKeyPressed(key)  	endendlocal function ControlsPostConstruct(inst)	local handler_prediction = GLOBAL.TheInput:AddKeyHandler(function(key, down) OnRawKey(key, down) end )endlocal function ConsolePostConstruct(inst)function inst:OnBecomeActive()GLOBAL.SetPause(true)inst._base.OnBecomeActive(inst)TheFrontEnd:ShowConsoleLog()inst.console_edit:SetFocus()inst.console_edit:SetEditing(true)TheFrontEnd:LockFocus(true)endfunction inst:OnBecomeInactive()    GLOBAL.SetPause(false)    inst._base.OnBecomeInactive(self)    if inst.runtask ~= nil then        inst.runtask:Cancel()        inst.runtask = nil    endendreturn instendlocal function ChatPostConstruct(inst)function inst:OnBecomeActive()GLOBAL.SetPause(true)inst._base.OnBecomeActive(inst)inst.chat_edit:SetFocus()inst.chat_edit:SetEditing(true)TheFrontEnd:LockFocus(true)endfunction inst:OnBecomeInactive()    GLOBAL.SetPause(false)    inst._base.OnBecomeInactive(self)    if inst.runtask ~= nil then        inst.runtask:Cancel()        inst.runtask = nil    endendreturn instendAddClassPostConstruct("screens/consolescreen", ConsolePostConstruct)AddClassPostConstruct("screens/chatinputscreen", ChatPostConstruct)AddClassPostConstruct("widgets/controls", ControlsPostConstruct) 

 

 

 

* has been edited.

Edited by Kzisor
Link to comment
Share on other sites

@Kzisor, awesome. This works because screens and widgets are loaded by host and client seperately, right?

 

Maybe there is a way to attach something to replicas? The world and entites of the client are replicas, right? The not replicas are the things you c_spawn as the client but you can do nothing with them because they don't have components as per the ismastersim if.

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