Jump to content

Recommended Posts

Hi!

[modmain.lua]

AddPrefabPostInit("compfire", function(inst)
   inst:AddTag("custom_tag123")
end)

[widgets/mywidget.lua]

local x, y, z = self.owner.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 50, {"custom_tag123"})

TheSim:FindEntities can not  find custom tag "custom_tag123".

Please tell me how to fix it.
Edited by Steve_Zhang

Hi!

[modmain.lua]

AddPrefabPostInit("compfire", function(inst)
    inst:AddTag("custom_tag123")
end)

[widgets/mywidget.lua]

local x, y, z = self.owner.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 50, {"custom_tag123"})

TheSim:FindEntities can not  find custom tag "custom_tag123".

Please tell me how to fix it.
 
Edited by Steve_Zhang
6 hours ago, IronHunter said:

Did you make a typo for campfire and called it compfire? Because I assume your trying to add it to the campfire prefab.

Apologies, I made a spelling mistake with the word 'campfire'. 
This is merely an example. The problem is still as per the title.
Is it because the widget can't access the custom tags added on the mastersim?

 

Edited by Steve_Zhang

Hi, your code works for me. I think your problem is one of these:

  1. You didn't put FindEntities() in a DoPeriodicTask. The function only searches for entities one time when you call it.
  2. Maybe you aren't close enough to the campfire. Remember that the object must exist and be nearby.
  3. Maybe a confussion. The campfire object in the game is this: image.png.9a4219cce6347f1f5ef834f469d325b6.png and you might think it's this image.jpeg.dd16cde5403dbf1a48c310ec8ba2dfe3.jpeg (it happened to me)

I also call

local x, y, z = self.owner.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 50, {"custom_tag123"})

from a widget, so I don't think that's the problem. If you could pass your complete code, maybe I could be more helpful.

15 minutes ago, FerniFrenito said:

Hi, your code works for me. I think your problem is one of these:

  1. You didn't put FindEntities() in a DoPeriodicTask. The function only searches for entities one time when you call it.
  2. Maybe you aren't close enough to the campfire. Remember that the object must exist and be nearby.
  3. Maybe a confussion. The campfire object in the game is this: image.png.9a4219cce6347f1f5ef834f469d325b6.png and you might think it's this image.jpeg.dd16cde5403dbf1a48c310ec8ba2dfe3.jpeg (it happened to me)

I also call

local x, y, z = self.owner.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 50, {"custom_tag123"})

from a widget, so I don't think that's the problem. If you could pass your complete code, maybe I could be more helpful.

 

Here is my widget code, please take a look at it.

local MyUI = Class(Widget, function(self, owner)
	Widget._ctor(self, "MyUI")
	self.owner = owner

	-- basic settings
	self:SetScaleMode(SCALEMODE_PROPORTIONAL)
	self:SetHAnchor(ANCHOR_MIDDLE)
	self:SetVAnchor(ANCHOR_MIDDLE)
	self:SetClickable(false)

	self:StartUpdating()
end)

function MyUI:OnUpdate(dt)
	local x, y, z = self.owner.Transform:GetWorldPosition()
	local ents = TheSim:FindEntities(x, y, z, 50, nil, nil, {"custom_tag123"})
	for _, ent in ipairs(ents) do
		if ent:IsValid() then
      			print("prefab:", ent.prefab)
		end
	end
end

return MyUI

 

Edited by Steve_Zhang

I hope no one saw what I posted before; it was a big lie. A widget's OnUpdate method does run, but to start it, you must use

yourwidgetInstance:StartUpdating()

You can use it at the end of your code to start updating when the object is finished creating by calling self:StartUpdating().

local myWidget = Class(Widget function()
  	...
    self:StartUpdating()
end)

 

Edited by FerniFrenito
Final response
10 minutes ago, FerniFrenito said:

Hi, It seems that the OnUpdate of the widgets are not executed alone, in fact, looking at the source code of the game they do not even have an OnUpdate method of any kind, but there is a code in the game to execute the OnUpdate of the screens, therefore the solution I propose is that you put your widget in a Screen and in the OnUpdate function of the screen execute the OnUpdate method of your widget

 

The widget "MyUI" has been added to ThePlayer.HUD . And I'm sure that the OnUpdate of the widget can be executed. 

But "TheSim:FindEntities" just can not find the custom tag. 

Edited by Steve_Zhang

I'm very sorry, I made a mistake by answering too quickly without looking into the code enough, but I've already edited my response, and that solution will work for your problem.

5 minutes ago, Steve_Zhang said:

 

The widget "MyUI" has been added to ThePlayer.HUD . And I'm sure that the OnUpdate of the widget can be executed. 

But "TheSim:FindEntities" just can not find the custom tag. 

If you are sure that OnUpdate is being executed, then I don't know what the problem could be. I used the exact same code without changing anything, and in my case, it works even with the same tag.

6 hours ago, FerniFrenito said:

I'm very sorry, I made a mistake by answering too quickly without looking into the code enough, but I've already edited my response, and that solution will work for your problem.

If you are sure that OnUpdate is being executed, then I don't know what the problem could be. I used the exact same code without changing anything, and in my case, it works even with the same tag.

 

This mod is for Don't Starve Together, not Don't Starve .

-- [[modmain.lua]]
local MyUI = require("widgets/myui")

AddPrefabPostInit("campfire", function(inst)
	inst:AddTag("custom_tag123")
end)

AddPlayerPostInit(function(inst)
	inst:ListenForEvent("playeractivated", function()
		if not TheWorld.ismastersim then
			if not inst.HUD then
				return
			end
			if not inst.steve_myui then
				inst.steve_myui = inst.HUD:AddChild(MyUI(inst))
				inst.steve_myui:MoveToBack()
			end
		end
	end)
end)

-- [[widgets/myui.lua]]
local Widget = require("widgets/widget")

local MyUI = Class(Widget, function(self, owner)
	Widget._ctor(self, "MyUI")
	self.owner = owner

	-- basic settings
	self:SetScaleMode(SCALEMODE_PROPORTIONAL)
	self:SetHAnchor(ANCHOR_MIDDLE)
	self:SetVAnchor(ANCHOR_MIDDLE)
	self:SetClickable(false)

	self:StartUpdating()
end)

function MyUI:OnUpdate(dt)
	print("MyUI:OnUpdate")
	local x, y, z = self.owner.Transform:GetWorldPosition()
	local ents = TheSim:FindEntities(x, y, z, 50, nil, nil, { "custom_tag123", "lighter" })
	for _, ent in ipairs(ents) do
		if ent:IsValid() then
			print("prefab:", ent.prefab)
		end
	end
end

return MyUI

In Don't Starve Together's console, using c_spawn("torch") will print "MyUI:OnUpdate" and "prefab: torch", but c_spawn("campfire") only prints "MyUI:OnUpdate". Therefore, I conclude that TheSim:FindEntities cannot find entities with custom tags. I just don't know why......

PS: The 'lighter' tag is a non-custom tag used to test whether TheSim:FindEntities works.

Edited by Steve_Zhang

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
×
  • Create New...