Moonkis Posted November 3, 2016 Share Posted November 3, 2016 I have been wrecking my brain on this for a while. I can't seem to access a spawned prefabs (GLOBAL.SpawnPrefab) brain. I guess this has something to do with replication but I'm THE host, so is should have access to it, right? Whenever an item I made breaks it's suppose to spawn 10 rabbits (which it does) and then make them follow you. The problem is that the brain is nil so I can't add a follow-behavior in their behavior-tree. Not sure what I am doing wrong. Current code: inst.components.finiteuses:SetOnFinished(function() -- if not GLOBAL.TheWorld.ismastersim then return end for i = 1, 10 do local bunny = GLOBAL.SpawnPrefab('rabbit') bunny.Transform:SetPosition(GLOBAL.ThePlayer:GetPosition():Get()) --bunny:AddComponent('follower') --bunny.components.follower:SetLeader(GLOBAL.ThePlayer) --GLOBAL.ThePlayer.components.leader:AddFollower(bunny) --bunny.components.follower.maxfollowtime = TUNING.PIG_LOYALTY_MAXTIME * 2 print(bunny.brain) --bunny.inst.brain:Stop() --print(bunny.inst) print(bunny.entity.brain) -- We apparently need to insert some brain stuff -- local behaviours = bunny.entity.brain.bt.root.children -- table.insert(behaviours, math.min(3, #behaviours), GLOBAL.Follow(bunny, function() return GLOBAL.ThePlayer end, 1, 2, 10, true)) end -- Original Call inst:Remove() end) --inst.components.finiteuses:SetOnFinished(inst.Remove) end) The code currently (as I commented out most of my experiments), it prints nil for both bunny.brain and bunny.entity.brain. What do I do? Link to comment https://forums.kleientertainment.com/forums/topic/71368-making-a-spawned-prefab-following-you/ Share on other sites More sharing options...
Serpens Posted November 3, 2016 Share Posted November 3, 2016 (edited) 18 minutes ago, Moonkis said: I have been wrecking my brain on this for a while. I can't seem to access a spawned prefabs (GLOBAL.SpawnPrefab) brain. I guess this has something to do with replication but I'm THE host, so is should have access to it, right? Whenever an item I made breaks it's suppose to spawn 10 rabbits (which it does) and then make them follow you. The problem is that the brain is nil so I can't add a follow-behavior in their behavior-tree. Not sure what I am doing wrong. Current code: The code currently (as I commented out most of my experiments), it prints nil for both bunny.brain and bunny.entity.brain. What do I do? hmm.. have no clue but did you try it with other prefabs like pigs? (also try spawning pighouses and test if the pigs from these houses have brains) Maybe it is a rabbit problem only? And can you access brains from rabbits from worldgeneration? Or can you accces brain from your spawned rabbits ingame using the console? print(TheSim:FindFirstEntityWithTag("rabbit").brain) Edited November 3, 2016 by Serpens Link to comment https://forums.kleientertainment.com/forums/topic/71368-making-a-spawned-prefab-following-you/#findComment-832650 Share on other sites More sharing options...
Moonkis Posted November 3, 2016 Author Share Posted November 3, 2016 44 minutes ago, Serpens said: hmm.. have no clue but did you try it with other prefabs like pigs? (also try spawning pighouses and test if the pigs from these houses have brains) Maybe it is a rabbit problem only? And can you access brains from rabbits from worldgeneration? Or can you accces brain from your spawned rabbits ingame using the console? print(TheSim:FindFirstEntityWithTag("rabbit").brain) Tested it and no, everything spawned with "SpawnPrefab" returns nil when trying to access the brain. TheSim function works, but it doesn't fix the underlying problem. Link to comment https://forums.kleientertainment.com/forums/topic/71368-making-a-spawned-prefab-following-you/#findComment-832697 Share on other sites More sharing options...
CarlZalph Posted November 3, 2016 Share Posted November 3, 2016 (edited) The brain doesn't fully initialize at this time and you must put a frame delay in between brain surgery: local ent = SpawnPrefab("catcoon") ent:DoTaskInTime( 0, function(self) if(self.brain) then -- Edit self.brain here end end ) Also keep in mind that if the entity despawns for any reason then the brain itself reverts to default behaviour as the initialization happens again, so if you remove components or alter its stategraph it can lead to a crash if you didn't fix up during its onremove event callback (by deleting the thing and making a new one near the owner). Edit: This is to say since you're using the mod API and not the client->server console that it is better making your own prefab with its own brain to do what you want to do than to shoehorn in something from an already made prefab. Edited November 3, 2016 by CarlZalph Link to comment https://forums.kleientertainment.com/forums/topic/71368-making-a-spawned-prefab-following-you/#findComment-832705 Share on other sites More sharing options...
Moonkis Posted November 3, 2016 Author Share Posted November 3, 2016 (edited) On 11/3/2016 at 7:44 PM, CarlZalph said: The brain doesn't fully initialize at this time and you must put a frame delay in between brain surgery: local ent = SpawnPrefab("catcoon") ent:DoTaskInTime( 0, function(self) if(self.brain) then -- Edit self.brain here end end ) Also keep in mind that if the entity despawns for any reason then the brain itself reverts to default behaviour as the initialization happens again, so if you remove components or alter its stategraph it can lead to a crash if you didn't fix up during its onremove event callback (by deleting the thing and making a new one near the owner). Edit: This is to say since you're using the mod API and not the client->server console that it is better making your own prefab with its own brain to do what you want to do than to shoehorn in something from an already made prefab. Ah, okay, thank you, good suggestion! Just curious, I was trying to add a follower-component but nothing happened, is that mainly because State Graphs override that component? EDIT: Never mind, a bit of more search I figured it out. Edited November 5, 2016 by Moonkis Link to comment https://forums.kleientertainment.com/forums/topic/71368-making-a-spawned-prefab-following-you/#findComment-832721 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