Slastraf Posted November 4, 2015 Share Posted November 4, 2015 scripts/objects/ai/monsterai.lua" : 143 : attempt to call method 'GetPosition' (a nil value) I get this error whenever i spawn a crawler into the map by code (not drag & drop). It is the default script and it works when i drag the prefab into the scene. The following happens: Crawlers begin to run towards player. Error comes up. (all in all <1 second). I have no Idea why this isnt working. In case heres a download link: http://www.mediafire.com/download/13drm2j8i14ydin/SlastrafProject.rar its the start map which is not loaded by default i think ( in my project) . The self.Target is assigned (target = player) trought the script and everything else is not missed out. Quote Link to comment Share on other sites More sharing options...
Rick Posted November 4, 2015 Share Posted November 4, 2015 OK, to make it not crash you need to set the zombie target to the players script variable. So in ZombieSpawner.lua line 48 instead of: self.zombie.script.target = self.Target do self.zombie.script.target = self.Target.script The zombie script is expecting the target variable to be the entities script variable instead of the entity itself. I saw that because inside MonsterAI.lua there is a ChooseTarget() function which sets the target variable and you can see it sets the entities script as this target value. Doing that fixed the crash but the zombie didn't run to the player. The thing is the MonsterAI has code in it where it searchers for opposite teamid so you don't even have to set the target manually because the monster will find the player. The other thing you are doing incorrectly is in your spawner you are using a script level variable: if self.zombie==nil then self.zombie= Prefab:Load("Prefabs/Characters/crawler1.pfb")end --self.zombie.script:Start() self.zombie:SetPosition(self.entity:GetPosition(true), true) self.zombie.script:Enable() For the first time self.zombie will be nil so it's load it. Then it'll set it's position and enable it. The 2nd time in there self.zombie is NOT nil so it won't load it again, but it'll keep setting it's position to the spawners position over and over again. When you load things dynamically you don't really need to hold onto them from the loader script itself. Just let it be free! In other words use a local zombie variable when loading and setting things up for that zombie. After the function exists that variable will be free'd but the zombie will now exist in your world and do it's thing. In other words change it to this: local zombie= Prefab:Load("Prefabs/Characters/crawler1.pfb") --self.zombie.script:Start() zombie:SetPosition(self.entity:GetPosition(true), true) zombie.script:Enable() You don't need to set the zombie's target and also not the true params in GetPosition() and SetPosition(). It's needed because your spawner is a child of the spawn controller and you want to use the global position of the spawner not the child position. 1 Quote Link to comment Share on other sites More sharing options...
Gonan Posted November 4, 2015 Share Posted November 4, 2015 Not sure if this bughas been fixed yet from last year, but it was a similar issue and has a simple fix http://www.leadwerks.com/werkspace/topic/11065-monsterailua-function-scriptendattack-bug-fix/ 1 Quote Link to comment Share on other sites More sharing options...
macklebee Posted November 5, 2015 Share Posted November 5, 2015 I haven't looked at the inherent monsterAI script in a long time so I do not know if there have been a lot of changes this past year for it - but the Target property in the monsterAI used to be just for making the monster go to a certain waypoint. If you set the target as the player and then moved the player prior to the monster reaching you then it would cause issues. See this old post for more detail: http://www.leadwerks.com/werkspace/topic/11527-monsterai-prefab/#entry83180 1 Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Slastraf Posted November 5, 2015 Author Share Posted November 5, 2015 OK, to make it not crash you need to set the zombie target to the players script variable. So in ZombieSpawner.lua line 48 instead of: self.zombie.script.target = self.Target do self.zombie.script.target = self.Target.script The zombie script is expecting the target variable to be the entities script variable instead of the entity itself. I saw that because inside MonsterAI.lua there is a ChooseTarget() function which sets the target variable and you can see it sets the entities script as this target value. Doing that fixed the crash but the zombie didn't run to the player. The thing is the MonsterAI has code in it where it searchers for opposite teamid so you don't even have to set the target manually because the monster will find the player. The other thing you are doing incorrectly is in your spawner you are using a script level variable: if self.zombie==nil then self.zombie= Prefab:Load("Prefabs/Characters/crawler1.pfb")end --self.zombie.script:Start() self.zombie:SetPosition(self.entity:GetPosition(true), true) self.zombie.script:Enable() For the first time self.zombie will be nil so it's load it. Then it'll set it's position and enable it. The 2nd time in there self.zombie is NOT nil so it won't load it again, but it'll keep setting it's position to the spawners position over and over again. When you load things dynamically you don't really need to hold onto them from the loader script itself. Just let it be free! In other words use a local zombie variable when loading and setting things up for that zombie. After the function exists that variable will be free'd but the zombie will now exist in your world and do it's thing. In other words change it to this: local zombie= Prefab:Load("Prefabs/Characters/crawler1.pfb") --self.zombie.script:Start() zombie:SetPosition(self.entity:GetPosition(true), true) zombie.script:Enable() You don't need to set the zombie's target and also not the true params in GetPosition() and SetPosition(). It's needed because your spawner is a child of the spawn controller and you want to use the global position of the spawner not the child position. That fixed it. and thank you for the informations Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.