Jump to content

ListenForEvent with Client-Only mod


Recommended Posts

Hey ho,

is it correct, that it is not possible to register for events using ListenForEvent in a client only mod?:

AddComponentPostInit("health", function(Health, inst)
    inst:ListenForEvent("healthdelta", function(inst, data)
      if inst.components.health then
          -----
      end
  end)
end)

Would be happy for any confirmation in any direction.


Thanks a lot!

Link to comment
Share on other sites

@s1m13

It's perfectly fine to listen for events on the client side.

However, you're trying to check for a component that doesn't exist entirely on the client.  The health component check you're doing there will always return false as a client when it is also not the server (no cave self host).

See: player_common.lua and how `inst:AddComponent("health")` happens after the "if not TheWorld.ismastersim then" check.

It exists only in a replica state on the client side.

 

You can touch the replica health via: inst.replica.health

This does not exist on the server however, so mind your checks.

 

 

Further, you do not have access to the health component replica of anything that isn't you the player by default.

So doing a global component postinit is effectively just hooking your player, which at that point you may as well do a player post init and check against the GLOBAL.ThePlayer variable or something.  Or a "world" prefab post init and then listen for "playeractivated" while still checking against GLOBAL.ThePlayer.

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