Jump to content

[Solved] need help with fixing line of code


Recommended Posts

Hello, I need some help fixing a tiny bit of code :)!

So, this is the code

if inst.HUD:IsChatInputScreenOpen() or inst.HUD:IsConsoleScreenOpen() then return end

I put this code in some my functions in modmain.lua then they don't effect the character if they're typing in chat, but I keep getting this crash if the world has caves on?

" attempt to index field 'HUD' (a nil value) "

It'd be great if someone can tell me what I'm doing wrong :wilson_dorky:!

Edited by SuperDavid
Link to comment
Share on other sites

Usually when something work when in server with no cave, but not in server with caves, it's because of a prob of server(host)/client thing. Like something working if you are the host (server side) but not if you are client (client side).

I can't help more, sorry.

Link to comment
Share on other sites

42 minutes ago, SuperDavid said:

I put this code in some my functions in modmain.lua then they don't effect the character if they're typing in chat, but I keep getting this crash if the world has caves on?

Your character on your side (client) has the HUD attached to itself. The dedicated server does not.

if inst.HUD and (inst.HUD:IsChatInputScreenOpen() or inst.HUD:IsConsoleScreenOpen()) then return end

 

Link to comment
Share on other sites

4 hours ago, DarkXero said:

Your character on your side (client) has the HUD attached to itself. The dedicated server does not.


if inst.HUD and (inst.HUD:IsChatInputScreenOpen() or inst.HUD:IsConsoleScreenOpen()) then return end

Thank you, now it doesn't  crash in world with caves but the code does not doing in world with caves even when I'm in console or in chat.

Is there a way to fix that or no?

Link to comment
Share on other sites

12 minutes ago, SuperDavid said:

Is there a way to fix that or no?

You have to present some context. I can only guess in what kind of spaghetti is this "tiny bit of code" used.

My best guess is that you have a key that triggers a transformation and you don't want the console interfering.

In that case you want to put that code in the function that sends the transformation RPC (ran by client), not the action itself (ran by server).

Link to comment
Share on other sites

18 hours ago, DarkXero said:

You have to present some context. I can only guess in what kind of spaghetti is this "tiny bit of code" used.

Sorry :wilson_dorky:!

Here's the entire code

 

modmain.lua

Spoiler

local DODGE = GLOBAL.Action()
DODGE.str = "Dodge"
DODGE.id = "DODGE"
DODGE.fn = function(act)
	if act.target.HUD and (act.target.HUD:IsChatInputScreenOpen() or act.target.HUD:IsConsoleScreenOpen()) then return end
	
	act.target.sg:GoToState("dodge")
end
AddAction(DODGE)

 

mycharacter.lua

Spoiler

local function OnKeyPressed(inst, data)
  if data.inst == ThePlayer then
  
    if data.key == KEY_R then
      if TheWorld.ismastersim then
      BufferedAction(inst, inst, ACTIONS.DODGE):Do()
      else
      SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.DODGE.code, inst, ACTIONS.DODGE.mod_name)
      end
	  
    end
  end
end

local function common_postinit(inst)
	inst:AddComponent("keyhandler")
	inst:ListenForEvent("keypressed", OnKeyPressed)
end

 

keyhandler.lua component

Spoiler

local KeyHandler = Class(function(self, inst)
    self.inst = inst
    self.handler = TheInput:AddKeyHandler(function(key, down) self:OnRawKey(key, down) end )
 
end)
 
function KeyHandler:OnRawKey(key, down)
    local player = ThePlayer
      if (key and not down) and not IsPaused() then
          player:PushEvent("keypressed", {inst = self.inst, player = player, key = key})
    elseif key and down and not IsPaused() then
          player:PushEvent("keydown", {inst = self.inst, player = player, key = key})
      end
end
 
return KeyHandler 

 

 

Link to comment
Share on other sites

23 minutes ago, SuperDavid said:

code

I was right.

local function OnKeyPressed(inst, data)
    if data.inst == ThePlayer then

        if data.key == KEY_R then
            
            if data.inst.HUD and (data.inst.HUD:IsChatInputScreenOpen() or data.inst.HUD:IsConsoleScreenOpen()) then return end

            if TheWorld.ismastersim then
                BufferedAction(inst, inst, ACTIONS.DODGE):Do()
            else
                SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.DODGE.code, inst, ACTIONS.DODGE.mod_name)
            end

        end
    end
end

 

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