Jump to content

Recommended Posts

Hello everyone!

 

I couldn't find this issue in the forum, so I'll just ask.

My idea is to alter the code of the meatrack to make certain products spawn multiple items in inventory when harvested.

So I created a function in meatrack.lua, and replaced onharvested() with it like this.[codesyntax]

local function GiveItem( inst )
local loot = SpawnPrefab("myitem")
local player = GetPlayer()
if player.components.inventory then
player.components.inventory:GiveItem(loot, nil, Vector3(TheSim:GetScreenPos(inst.Transform:GetWorldPosition())))
end
inst.AnimState:PlayAnimation("idle_empty")
end
[/codesyntax]

 

and then

 

[codesyntax]inst.components.dryer:SetOnHarvestFn(GiveItem)[/codesyntax]

 

This seems to work quite nicely.

However, I'd like to avoid modifying existing files if I can help it. (It feels wrong)

So I thought, hey, that's easy, all I have to do is put the function GiveItem in modmain.lua, and then go 

[codesyntax]function MakeDryerGiver( prefab, settings )
if prefab.components.dryer then
prefab.components.dryer:SetOnHarvestFn(function(inst) GiveItem(inst) end)
end
end
AddPrefabPostInit( "meatrack", function( inst ) MakeDryerGiver( inst, settings ) end )[/codesyntax]

 

Alas, cruel world, it seems that when GiveItem is in modmain.lua, the game no longer finds SpawnPrefab.

 

Is there some way around this issue? If not, is any special code required to tell my mod to use the modified version of meatrack.lua?

 

Thanks for any help

In modmain you need to use GLOBAL.SpawnPrefab("myitem")

The modmain.lua is placed in a sandbox to pervent you from messing up the global varables.

All global varables and global functions are stored in the GLOBAL table.

 

 

GetPlayer() and Vector3() also must be called with GLOBAL

Edited by no_signal

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