MoisiAlt Posted January 18, 2014 Share Posted January 18, 2014 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 thenplayer.components.inventory:GiveItem(loot, nil, Vector3(TheSim:GetScreenPos(inst.Transform:GetWorldPosition())))endinst.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 thenprefab.components.dryer:SetOnHarvestFn(function(inst) GiveItem(inst) end)endendAddPrefabPostInit( "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 Link to comment https://forums.kleientertainment.com/forums/topic/30994-help-with-function-call-in-modmain/ Share on other sites More sharing options...
no_signal Posted January 18, 2014 Share Posted January 18, 2014 (edited) 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 January 18, 2014 by no_signal Link to comment https://forums.kleientertainment.com/forums/topic/30994-help-with-function-call-in-modmain/#findComment-405540 Share on other sites More sharing options...
MoisiAlt Posted January 18, 2014 Author Share Posted January 18, 2014 Ooh, that's brilliant!It now works exquisitely well!Thanks a lot sir! Link to comment https://forums.kleientertainment.com/forums/topic/30994-help-with-function-call-in-modmain/#findComment-405562 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now