Jump to content

Can I call a function that exists in prefab file from modmain.lua?


Recommended Posts

Accidentally posted this in the DS forum, meant to post it here in the DST forum...

 

I want to add a haunting effect to "smallmeat" so in my modmain.lua I have:

AddPrefabPostInit("smallmeat", 
  function(inst)
    --here I want to call AddMonsterMeatChange(inst, "someotherprefab") that exists in \data\scripts\prefabs\meats.lua
  end)

Is this possible, or will I have to just duplicate this method inside of my modmain.lua?

Also, related, what if I need to call a method on a new custom prefab and also on an existing prefab? Am I required to define that function twice as well (once in the new prefab's .lua file where I am creating the prefab, and once in the modmain.lua where I call AddPrefabPostInit)?

Link to comment
Share on other sites

AddMonsterMeatChange is a local variable to the meats.lua file.

You can't call it from modmain, or from anywhere else. You need to copy paste the method.

 

If you put in your custom prefab file the

function MyGlobalFunctionInPrefab(inst, prefab)
end

without the "local", then the function will be global.

And you can call globals from modmain like

AddPrefabPostInit("smallmeat", function(inst)
	GLOBAL.MyGlobalFunctionInPrefab(inst)
end)

 

You can also make global functions from modmain, like

GLOBAL.ThisWillBeGlobal = function()
end

 

You can even create a new lua file (myfunctions.lua) that looks like

local func1 = function()
end

local func2 = function()
end

return {
	dothis = func1,
	dothat = func2,
}

and then you can do

local myfuncs = require("myfunctions") -- returns the table with the functions

myfuncs.dothis() -- calls func1
myfuncs.dothat() -- calls func2

wherever you want.

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