Jump to content

Recommended Posts

Hello, I need help to put code the proper way. So, here's my code

-- Don't attack mosslingwhisperer, ever.
    inst:ListenForEvent("attacked", function(inst, data, target)
    if data.target:HasTag("mosslingwhisperer") then
    inst.components.combat:DropTarget()
    end
    end)

I put this code into my mossling.lua, i'm trying to make it that mosslings won't ever attack anyone with the tag "mosslingwhisperer" even if they get hit by the mosslingwhisperer but the code just crashes the game, i'm probably doing it the wrong way but I don't know how to do it the proper way :(... Can someone please tell me the proper way for a code like this? Thanks for reading :)!

PS. If you help me can you also please add that they don't attack anyone with the tag "mossling" either, then they don't kill eachother, thanks :D!

Link to comment
https://forums.kleientertainment.com/forums/topic/67771-help-with-fixing-code/
Share on other sites

2 hours ago, SuperDavid said:

code just crashes the game

Always always always include the stack trace of the crash if you have one, it saves anyone looking into it a lot of time.

Also, having your own version of a game file is always a last resort in modding, because it means your mod will be incompatible with any other mod that does the same thing. It's much better to use one of the PostInit functions (or if you know what you're doing, requiring in the class and modifying its functions, but that only works for non-prefabs).

But I believe the best way to do what you want is something like this:

AddPrefabPostInit("mossling", function(inst)
	local OldCanTarget = inst.replica.combat.CanTarget
	function inst.replica.combat:CanTarget(target, ...)
		return OldCanTarget(self, target, ...) and not (target:HasTag("mosslingwhisperer") or target:HasTag("mossling"))
	end
end)

 

Edited by rezecib
40 minutes ago, rezecib said:

But I believe the best way to do what you want is something like this:


AddPrefabPostInit("mossling", function(inst)
	local OldCanTarget = inst.replica.combat.CanTarget
	function inst.replica.combat:CanTarget(target, ...)
		return OldCanTarget(self, target, ...) and not (target:HasTag("mosslingwhisperer") or target:HasTag("mossling"))
	end
end)

I put that code you gave me in my mossling prefab and it crashed the game and said, "[string "../mods/Adam Character Mod/scripts/prefabs/..."]:277: variable 'AddPrefabPostInit' is not declared" :(... Thanks for helping anyways :)!

Thank you very much Rezecib it worked perfectly, really thanks so much :D! And i'll be sure to always include my stack trace crash thing for the future crashes (I hope though I never get crash again), thanks a lot :)!

Edited by SuperDavid

I'm sorry to bother you again Rezecib but that code you gave me to put in my modmain all of a sudden now gives a crash when I enter a world, here's the crash (it also crashes some worlds without giving a LUA ERROR stack traceback)

"[string "../mods/Adam Character Mod/modmain.lua"]:128: attempt to index field 'combat' (a nil value)

LUA ERROR stack traceback: ../mods/Adam Character Mod/modmain.lua(128,1)

=(tail call) ?

=[C] in function 'xpcall'

scripts/mods.lua(123,1) in function 'mod'

scripts/mainfunctions.lua(155,1)"

AddPrefabPostInit("mossling", function(inst)
local OldCanTarget = inst.replica.combat.CanTarget -- This is causing crash for some reason
function inst.replica.combat:CanTarget(target, ...)
return OldCanTarget(self, target, ...) and not (target:HasTag("mosslingwhisperer") or target:HasTag("mossling"))
end
end)

 

Edited by SuperDavid

@SuperDavid Try 

AddPrefabPostInit("mossling", function(inst)
	if not GLOBAL.TheWorld.ismastersim then return end
	local OldCanTarget = inst.replica.combat.CanTarget -- This is causing crash for some reason
	function inst.replica.combat:CanTarget(target, ...)
		return OldCanTarget(self, target, ...) and not (target:HasTag("mosslingwhisperer") or target:HasTag("mossling"))
	end
end)

If you're removing the combat component or something that would also screw it up.

Edited by rezecib

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