Jump to content

Updating Client Entity's name


Recommended Posts

I am trying to update client's entity's name (e.g Butterfly, Rabbit, Redbird, Merm, etc) on mouseover. What i am trying to achieve is something that look like this.

 

The code was originally written by Star for DS: here

He recently released a version for DST: here

 

I modified his DS version and came up with the first image. It works well on the host. Any damages occurs are updated in real time to the 'name' attributes. However, this does not work for the client. The examine part (the original mod) is working on both sides.

 

I made 3 different version of the code. 2 works fine, the other works like 80% of the time but only on the server. The mouseover never works in the client.

1. global class EntityScript and extends/modify the GetDisplayName

2. class HoverText and extends/modify the OnUpdate

3. class Health_replica and extends/modify the SetCurrent. (partially working as it does not include the initial health value, only start showing health after the entity is attacked).

 

As far is i understand (after reading the forum and what not), i can't use replica or its too complex to do so. My last option was to use another component class to transfer the 'name'  using net_ variable. But for some reason, i can't seems to addComponent to the prefabs. 

[string "scripts/entityscript.lua"]:52: attempt to index field '?' (a boolean value)

Any advice are extremely appreciated.

Link to comment
Share on other sites

My last option was to use another component class to transfer the 'name'  using net_ variable.
 You don't need a component. You should be able to attach the net_string directly to the prefab, in a prefabpostinit or whatever, I believe.
Link to comment
Share on other sites

You could just add the 'named' component to them. It already takes care of networking.

 

This honestly seems like a bad way to replicate that kind information. Abusing name for a value that is constantly changing will increase network traffic by a lot more then if you were to just replicate max health once, and health whenever it changes.

Link to comment
Share on other sites

This honestly seems like a bad way to replicate that kind information. Abusing name for a value that is constantly changing will increase network traffic by a lot more then if you were to just replicate max health once, and health whenever it changes.

Yes, it will transmit a string (the new name) for each affected entity (as opposed to prefab) whenever the name changes. It's a tradeoff between simplicity and efficiency. A custom implementation could certainly save on network resources.

Link to comment
Share on other sites

So basically i have 2 ways to deal with it.

#1 use AddPrefabPostInit to add "Named" component and then Update it whenever the Entity takes damage. This method is simple but less efficient as i am using component to transfer info to the client.

AddPrefabPostInit("butterfly", function(inst)    if inst.components then        inst:AddComponent("Named")        inst.components.Named.nickname = "Butterfly [1/1]"    endend)
AddGlobalClassPostConstruct("entityscript", "EntityScript", function(self)    self.GetDisplayName = function(self)	        local str = nil        if self.displaynamefn then            str = self.displaynamefn(self)        else            str = self.name            if self.components.health then                local name = str	        local h=self.components.health	        local mx=math.floor(h.maxhealth-h.minhealth)	        local cur=math.floor(h.currenthealth-h.minhealth)	        local i,j = string.find(name, " [", nil, true)	        if i ~= nil and i > 1 then name = string.sub(name, 1, (i-1)) end                name = (name=="" and "" or (name.." ["))..cur.." / "..mx .."]"--.. " ("..math.floor(cur*100/mx).."%%)"	        if self.components.named then                    self.components.named:SetName(name)                end            end        end        return str    endend)

#2 Use a net_string variable to store name directly on the prefab, then update it when an Entity takes damage. This method is more efficient, but slightly more complex to implement.

Uh-oh, i tried to code this, but this part eludes me. How do i correctly implement a new net_string directly in AddPrefabPostInit? I am pretty sure something is wrong with the code below.

AddPrefabPostInit("butterfly", function(self)    self.OnHealthNameDirty = function(inst)        inst.hunt_kills = inst.net_healthname:value()    end
 
    self.SetHealthName = function( name )
        self.healthname = name
        self.net_healthname:set(name)
    end

    self.healthname = "butterfly"     self.net_healthname = net_string(inst.GUID, "healthname", "healthnamedirty")
    if not TheWorld.ismastersim then self.inst:ListenForEvent("healthnamedirty", OnHealthNameDirty)     end end)

AddGlobalClassPostConstruct("entityscript", "EntityScript", function(self)    self.GetDisplayName = function(self)	        local str = nil        if self.displaynamefn then            str = self.displaynamefn(self)        else            str = self.name            if self.components.health then                local name = str	        local h=self.components.health	        local mx=math.floor(h.maxhealth-h.minhealth)	        local cur=math.floor(h.currenthealth-h.minhealth)	        local i,j = string.find(name, " [", nil, true)	        if i ~= nil and i > 1 then name = string.sub(name, 1, (i-1)) end                name = (name=="" and "" or (name.." ["))..cur.." / "..mx .."]"--.. " ("..math.floor(cur*100/mx).."%%)"	        if self.SetHealthName then                    self.SetHealthName(name)                end            end        end        return str    endend) 

p/s: I am in my office atm. I don't have DST to refer to the main script code. All i have with me is the mods files stored in my GDrive.

Link to comment
Share on other sites

Now my code looks something like this

AddGlobalClassPostConstruct("entityscript", "EntityScript", function(self)	self.GetDisplayName = function(self)		local str = nil	    if self.displaynamefn then	        str = self.displaynamefn(self)	    else	      	str = self.name	    end        if self.components.health and IsServer then		    -- if self.components and self.components.named == nil then		    -- 	self:AddComponent('named');		    -- end			local h=self.components.health			local mx=math.floor(h.maxhealth-h.minhealth)			local cur=math.floor(h.currenthealth-h.minhealth)			local i,j = string.find(str, " [", nil, true)			if i ~= nil and i > 1 then str = string.sub(str, 1, (i-1)) end			str = (str=="" and "" or (str.." ["))..cur.." / "..mx .."]"--.. " ("..math.floor(cur*100/mx).."%%)"			if self.components.named then				self.components.named:SetName(str)				-- for key,value in pairs(self.components.named) do print(key,value) end				print('name', self.components.named.name)			end		end		if not IsServer and self.components.named ~= nil then				str = self.components.named.name				-- for key,value in pairs(self.components.named) do print(key,value) end				print('=name', self.components.named.name)			end		end	    -- str = str.. " " .. " test"	    return str	endend)function InitNameHealth(inst)	if IsServer and inst.components and inst.components.named == nil then inst:AddComponent("named") endendAddPrefabPostInit("butterfly", InitNameHealth)AddPrefabPostInit("beefalo", InitNameHealth)AddPrefabPostInit("beehive", InitNameHealth)AddPrefabPostInit("pigman", InitNameHealth)

But a different problem happens now. 

	Unload BE done	Mod: tell-me-health (Tell me about health)	Registering prefabs	Mod: tell-me-health (Tell me about health)	  Registering default mod prefab		Load FE		Load FE: done	platform_motd	table: 0D5943A0	SimLuaProxy::QueryServer()update_date	table: 0D595138	SimLuaProxy::QueryServer()ModIndex: Load sequence finished successfully.	Reset() returningCould not unload undefined prefab 0x20e21d7a (puppet_wes)Could not unload undefined prefab 0x20e21d7a (puppet_wes)unloading prefabs for mod MOD_tell-me-health	Collecting garbage...lua_gc took 0.01 seconds~NetworkLuaProxy()~SimLuaProxy()Cancelling LuaQueryCallback handle [1]Cancelling LuaQueryCallback handle [2]lua_close took 0.02 secondsHttpClient::ClientThread::Main() completeHttpClient::ClientThread::Main() completeCould not unload undefined prefab 0x20e21d7a (puppet_wes)Could not unload undefined prefab 0x20e21d7a (puppet_wes)Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. HttpClient::ClientThread::Main() completeShutting down

What resource did it meant? 

Please help and thank 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...