hi ross
thanks for help ,i would love to share some parts of my code to you ,maybe you can help me to find out the problem.
part 1
I used the whole code you shared on github, and i add a custom function to get a random target .
function M.getAnRandomEnt(group)
local num = M.getCount(group)
local randomNum = math.abs(math.random(1,num))
local count = 1
for k,v in pairs(ent[group]) do
if count == randomNum then
--print(num,randomNum,count)
return k
else
count = count + 1
end
end
end
part 2
how i use the function at a missile init()
i usually got some error when a missile created ,and try to get the target position, it will report a error that the instance not found.
function init(self)
self.refreshTime = 1
self.t = 0
self.speed = 500
M.subscribe(msg.url("#"),M.enemies)
if M.getCount(M.enemies) > 0 then
print("INSTANCE EXIST!!!!!!")
self.target = M.getAnRandomEnt(M.enemies)
if self.target == null then
go.delete()
else
self.targetPos = go.get_position(self.target)
end
else
print("NO INSTANCE")
end
end
and the error output is below.
code line 25 is self.targetPos = go.get_position(self.target)
DEBUG:SCRIPT: INSTANCE EXIST!!!!!!
DEBUG:SCRIPT: INSTANCE EXIST!!!!!!
ERROR:SCRIPT: /scripts/missile.script:25: Instance l not found
stack traceback:
[C]: in function 'get_position'
/scripts/missile.script:25: in function </scripts/missile.script:10>
[C]: in function 'create'
/scripts/go_role.script:102: in function </scripts/go_role.script:79>
ERROR:GAMEOBJECT: Could not initialize when spawning /main/missile.goc.
ERROR:GAMEOBJECT: Could not spawn an instance of prototype /main/missile.goc.
[UPDATE]
I think i may found the problem from.
Because I create my own function for use, but I did not realize that the key-value “l” is also a value.
So when the Entity Manager is empty ,the M.getCount will not 0.
That `s why the debug log shows instance l not found ,I ignored the l letter .
Now I need to do more test to confirm it.but if you can help me to check my code to find out is there any other problem, it still help me.
thanks!