Rincevvind Posted May 10, 2014 Share Posted May 10, 2014 (edited) Atm i just get part of code from betterconsole mod :local function AddComponent() local character = GLOBAL.SaveGameIndex:GetSlotCharacter() GLOBAL.TheSim:LoadPrefabs {character} local oldfn = GLOBAL.Prefabs[character].fn GLOBAL.Prefabs[character].fn = function(...) local inst = oldfn(...) inst:AddComponent("customcomponent") return inst endendAddPrefabPostInit("world", AddComponent)its works fine, till i tried to enter caves. which makes game crash and erasing saves. second method :local function AddComponent(p) p:AddComponent("customcomponent")end AddSimPostInit(AddComponent)works fine, even in caves, but have 1 major issue - component doesn't call OnLoad Edited May 10, 2014 by Rincevvind Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/ Share on other sites More sharing options...
simplex Posted May 10, 2014 Share Posted May 10, 2014 The first method does not cause a crash when entering caves. So unless you post the mod code or at least the log there's nothing we can do to help. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477420 Share on other sites More sharing options...
_Q_ Posted May 10, 2014 Share Posted May 10, 2014 Atm i just get part of code from betterconsole mod :local function AddComponent() local character = GLOBAL.SaveGameIndex:GetSlotCharacter() GLOBAL.TheSim:LoadPrefabs {character} local oldfn = GLOBAL.Prefabs[character].fn GLOBAL.Prefabs[character].fn = function(...) local inst = oldfn(...) inst:AddComponent("customcomponent") return inst endendAddPrefabPostInit("world", AddComponent)its works fine, till i tried to enter caves. which makes game crash and erasing saves. second method :local function AddComponent(p) p:AddComponent("customcomponent")end AddSimPostInit(AddComponent)works fine, even in caves, but have 1 major issue - component doesn't call OnLoad I recently found out magic, to load stuff, look on hunter component, definition of it: self.inst:DoTaskInTime(0, function(inst) inst.components.hunter:StartCooldown() end)That makes sure all data is loaded, I also use that in OnLoad function, it works good for prefabs like pigs and merms, so it should work with character also. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477424 Share on other sites More sharing options...
Rincevvind Posted May 10, 2014 Author Share Posted May 10, 2014 log is clear when crash happened, game just shows start menu. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477428 Share on other sites More sharing options...
_Q_ Posted May 10, 2014 Share Posted May 10, 2014 log is clear when crash happened, game just shows start menu.If its svae file deleting, it can be something with save index, check the way you save talbes, cause that can be the problem. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477433 Share on other sites More sharing options...
Rincevvind Posted May 10, 2014 Author Share Posted May 10, 2014 is it safe way ? function CustomComponent:OnSave() return {d = deepcopy(self.data)}end where self.data is array Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477450 Share on other sites More sharing options...
_Q_ Posted May 10, 2014 Share Posted May 10, 2014 is it safe way ?function CustomComponent:OnSave() return {d = deepcopy(self.data)}endwhere self.data is arrayDon't think so, maybe try to declare the inner table first.Like in invenotry component for example. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477454 Share on other sites More sharing options...
Rincevvind Posted May 10, 2014 Author Share Posted May 10, 2014 (edited) looks like not, lol. just commented this save part and return {}. all works fine.thx _Q_ for pointing on real problem... looks like my laziness is issue :/Don't think so, maybe try to declare the inner table first.Like in invenotry component for example.well code works fine till caves, i checked it many times with loading/unloading. whole array is fine, its actually array of arrays and it was loaded/saved properly in this way Edited May 10, 2014 by Rincevvind Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477463 Share on other sites More sharing options...
_Q_ Posted May 10, 2014 Share Posted May 10, 2014 looks like not, lol. just commented this save part and return {}. all works fine.thx _Q_ for pointing on real problem... looks like my laziness is issue :/well code works fine till caves, i checked it many times with loading/unloading. whole array is fine, its actually array of arrays and it was loaded/saved properly in this way When I was making telestone mod saves were erased cause saveindex could not load when entering/exiting caves, cause of the way I was saving tables, can't say more, so you could just post whole code or mod here for others to take a look at it and see what is the cause of the problem. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477470 Share on other sites More sharing options...
Rincevvind Posted May 10, 2014 Author Share Posted May 10, 2014 well its few mods, and looks like saving data in this way is issuehere example in attach Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477490 Share on other sites More sharing options...
Rincevvind Posted May 10, 2014 Author Share Posted May 10, 2014 only thing i dont understand - how containers works then, they saves array of arrays too, well in different way i try to make array manually instead of deepcopy Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477494 Share on other sites More sharing options...
squeek Posted May 10, 2014 Share Posted May 10, 2014 (edited) Why even use deepcopy at all in OnSave? OnSave won't alter the table, so just doing return {d = self.data}or even return self.datashould be fine. Same with loading, no reason to deepcopy the table in OnLoad. Edited May 10, 2014 by squeek Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477507 Share on other sites More sharing options...
Rincevvind Posted May 10, 2014 Author Share Posted May 10, 2014 i guess i know my mistake, i saved pointer to function in this array... Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477527 Share on other sites More sharing options...
Rincevvind Posted May 11, 2014 Author Share Posted May 11, 2014 oh damn yes, so much fail lol, i just made function which coping array without variables which have "function" type and all works finelocal function RemoveFN(t) local t2 = {} for k,v in pairs(t) do if type(v) == "table" then t2[k] = RemoveFN(v) else if type(v)~="function" then t2[k] = v end end end return t2endits wasnt issue outside caves since functions was always redefined in array while init Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477535 Share on other sites More sharing options...
Rincevvind Posted May 11, 2014 Author Share Posted May 11, 2014 thx guys for help Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477537 Share on other sites More sharing options...
Rincevvind Posted May 11, 2014 Author Share Posted May 11, 2014 But still a question, why so hack-like method is required for such simple action like adding component to player Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477542 Share on other sites More sharing options...
squeek Posted May 11, 2014 Share Posted May 11, 2014 oh damn yes, so much fail lol, i just made function which coping array without variables which have "function" type and all works fineFunctions can be saved (I did it in the trade sequence code I wrote for the Link mod). But still a question, why so hack-like method is required for such simple action like adding component to player Thankfully, it won't be for too much longer. Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477553 Share on other sites More sharing options...
simplex Posted May 11, 2014 Share Posted May 11, 2014 (edited) Functions can be saved (I did it in the trade sequence code I wrote for the Link mod). Yes, but this can cause issues if you're not careful, since every external local variable used by the function is also saved. A simple case where this is disastrous is if you store the global environment in a local variable and use it in the function saved. Also, the saved function gets loaded in the global environment, so if it's a function defined in the mod environment (as seems to be the case here) it won't work after loading due to the environment change. Edited May 11, 2014 by simplex Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477556 Share on other sites More sharing options...
Rincevvind Posted May 11, 2014 Author Share Posted May 11, 2014 well its not required for me to save them in saveslot at all.as i understand every time i leave or enter cave all mods init functions is called again Link to comment https://forums.kleientertainment.com/forums/topic/36302-custom-component-for-player-prefab/#findComment-477559 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