Jump to content

attempt to call method 'GetPosition' (a nil value)


Slastraf
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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

  • Upvote 1

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

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! smile.png 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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...