Jump to content

Recommended Posts

I want to write a mod that need to get current pigman follower, but ThePlayer seems don't have leader component, how do I do? 

 

Also if I found pigman by 

    local player = GLOBAL.ThePlayer
    local x, y, z = player.Transform:GetWorldPosition()
    local ents = GLOBAL.TheSim:FindEntities(x, y, z, 30, {"pig"}) -- 30 is search radius

Should this ents have follower component?

Edited by weire
On 2/11/2025 at 6:35 PM, weire said:

I want to write a mod that need to get current pigman follower, but ThePlayer seems don't have leader component, how do I do? 

 

Also if I found pigman by 

    local player = GLOBAL.ThePlayer
    local x, y, z = player.Transform:GetWorldPosition()
    local ents = GLOBAL.TheSim:FindEntities(x, y, z, 30, {"pig"}) -- 30 is search radius

Should this ents have follower component?

I saw a similar post

 

2 hours ago, weire said:

thanks i saw that too, but it didn't say how can I got server instance of player, cuz ThePlay is client version in mod.

AddPlayerPostInit(function(inst)
	if not TheWorld.ismastersim then
		return inst
	end
	--[[
	Trigger by somethings...
		local followers = inst.components.leader:CountFollowers("pig")
		print(followers)
	--]]

end)

You can use that code, trigger by event or something, the code below works when pigman has new leader

local function CountFollowers(inst)
	local leader = inst.components.follower:GetLeader()
	if leader ~= nil and leader:HasTag("player") then 
		print(leader.components.leader:CountFollowers("pig"))
	end
end
AddPrefabPostInit("pigman", function(inst)
	if not TheWorld.ismastersim then
		return inst
	end
	inst:ListenForEvent("startfollowing", CountFollowers)

end)

 

  • Like 2
13 hours ago, weire said:

Thanks! I'm not sure I understand Event right. I saw somewhere said that the event can be only get by the same instance, only the event from TheWorld is global, and the listener need to set the source of listen to ThenWorld.

Sorry, this is beyond my understanding. I usually use the built-in events in the game and hardly ever pay attention to how they work.

I tried

AddPlayerPostInit(function(inst)
	if not TheWorld.ismastersim then
		return inst
	end

        local followers = inst.components.leader:CountFollowers("pig")
        print(followers)

end)

which try to get the followers that player have last time at the begining, but got nothing. Is it because that the follower has not be loaded here? anyway else I can do that?

I looked in the leader component and it looks like it uses a LoadPostPass, not sure what it means right now, but it might be why you aren't getting the right amount of followers. The only way pigs can be counted this way is if theyre able to continue to be loyal after disconnecting.

Spoiler
local function PigPlayerFollower(inst)
  if not inst.components.leader then return end
  local function followerfn(followers) -- what you want to do with num followers post-init
      print(followers)
  end
  local lpp = inst.components.leader.LoadPostPass
  inst.components.leader.LoadPostPass = function(self, ...)
      local result = lpp(self, ...)
      local followers = self:CountFollowers"pig"
      followerfn(followers)
      return result
  end
end
AddPlayerPostInit(PigPlayerFollower)

does something like that give the result you'd expect?

this may be a better implementation if it works

Spoiler
local function PlayerFollower(player, count) -- rename fn to what u want
	print(player, count)
end

AddComponentPostInit("leader", function(self)
	if not self.inst:HasTag"player" then return end
	local LoadPostPass_old = self.LoadPostPass
	self.LoadPostPass = function(self, ...)
		local result = LoadPostPass_old(self, ...)
		PlayerFollower(self.inst, self:CountFollowers"pig")
		return result
	end
end)
Edited by oregu

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