Jump to content

Need help with a function


Recommended Posts

Hello everyone.
I need your help.
I have this function:

 

local remove_hauntable_from =
{
	'beebox',
	'treasurechest', 
	'cookpot',
	'researchlab', 
	'researchlab2', 
	'meatrack',
	'grass',
	'twig',
	'berrybush',
	'berrybush2',
	'sapling',
	'red_mushroom',
	'green_mushroom',
}

local Hauntables = GetModConfigData("Hauntables")
local function RemoveHauntable(inst)
	if inst and inst.components.hauntable and (Hauntables == 1) then
	inst:RemoveComponent('hauntable')
	end
end

for k,name in pairs(remove_hauntable_from) do
	AddPrefabPostInit(name, RemoveHauntable)
end

She allows to forbid ghosts to interact with specified things.
But I want the forbid to apply to everything except the prefabs I chose.
Ghosts could only interact with "multiplayer_portal" and "amulet".

Edited by Tezumoto
Link to comment
Share on other sites

local hauntable_list =
{
	multiplayer_portal = 1,
	amulet = 1,
}
local Hauntables = GetModConfigData("Hauntables")
local function RemoveHauntable(inst)
	if inst and not hauntable_list[inst.prefab] and inst.components.hauntable and (Hauntables == 1) then
		inst:RemoveComponent('hauntable')
	end
end

AddPrefabPostInitAny(RemoveHauntable)

 

Link to comment
Share on other sites

On 29.05.2018 at 1:14 AM, ptr said:

local hauntable_list =
{
	multiplayer_portal = 1,
	amulet = 1,
}
local Hauntables = GetModConfigData("Hauntables")
local function RemoveHauntable(inst)
	if inst and not hauntable_list[inst.prefab] and inst.components.hauntable and (Hauntables == 1) then
		inst:RemoveComponent('hauntable')
	end
end

AddPrefabPostInitAny(RemoveHauntable)

 

The server crashes from time to time.

 


[00:03:29]: [string "scripts/brains/stagehandbrain.lua"]:24: attempt to index field 'hauntable' (a nil value)
LUA ERROR stack traceback:
    scripts/brains/stagehandbrain.lua:24 in (field) fn (Lua) <24-24>
    scripts/behaviourtree.lua:200 in (method) Visit (Lua) <199-205>
    scripts/behaviourtree.lua:643 in (method) Visit (Lua) <633-664>
    scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>
    scripts/behaviourtree.lua:22 in (method) Update (Lua) <20-27>
    scripts/brain.lua:212 in (method) OnUpdate (Lua) <205-214>
    scripts/brain.lua:135 in (method) Update (Lua) <105-147>
    scripts/update.lua:222 in () ? (Lua) <149-228>

Edited by Tezumoto
Link to comment
Share on other sites

Well, those prefabs are assumed to have hauntable, but you remove them, so that is what you should fix to make it work. Or you can play with hauntable it self, like Hauntable:SetOnHauntFn(function() return false end)

Link to comment
Share on other sites

14 minutes ago, ptr said:

Well, those prefabs are assumed to have hauntable, but you remove them, so that is what you should fix to make it work. Or you can play with hauntable it self, like Hauntable:SetOnHauntFn(function() return false end)

You can tell in more detail how to use "Hauntable: SetOnHauntFn (function () return false end)"?
I'm not good at this.

Link to comment
Share on other sites

1 hour ago, ptr said:

replace

inst:RemoveComponent('hauntable')

with

inst.components.hauntable:SetOnHauntFn(function() return false end)

But I have not tested it

These changes do not cause crash of game, but ghosts can interact with everything.

Can make an exception for "stagehandbrain.lua"?
Or is it difficult?

Edited by Tezumoto
Link to comment
Share on other sites

Maybe don't remove component and just remove Tag("haunted")?

It seems this code fixed crash of game.
 

local hauntable_list =
{
	multiplayer_portal = 1,
	amulet = 1,
	stagehand = 1,
}
local Hauntables = GetModConfigData("Hauntables")
local function RemoveHauntable(inst)
	if inst and not hauntable_list[inst.prefab] and inst.components.hauntable and (Hauntables == 1) then
		inst:RemoveComponent('hauntable')
	end
end

AddPrefabPostInitAny(RemoveHauntable)

But this is not a full fix, but a crutches.

Edited by Tezumoto
Link to comment
Share on other sites

Try this

local hauntable_list =
{
	multiplayer_portal = 1,
	amulet = 1,
}
local function RemoveHauntable(inst)
	if inst and not hauntable_list[inst.prefab] and inst.components.hauntable then
		inst:AddTag("haunted")
	end
end

AddPrefabPostInitAny(RemoveHauntable)

 

Link to comment
Share on other sites

1 hour ago, ptr said:

Try this


local hauntable_list =
{
	multiplayer_portal = 1,
	amulet = 1,
}
local function RemoveHauntable(inst)
	if inst and not hauntable_list[inst.prefab] and inst.components.hauntable then
		inst:AddTag("haunted")
	end
end

AddPrefabPostInitAny(RemoveHauntable)

 

Everything works without errors, big thanks for your help.

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