🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

weird problem calling functions in LUA.

Started by
5 comments, last by HexDump 19 years, 11 months ago
Hello, I´m having a weird problem I can´t solve. I have a function in LUA, this is: function DoLogic() Enemies[1].DoLogic(Enemies[1]); end That calls this other function: function ScannerLogic(Object) CnMoveObject(Object.ID,-1,0,0); end DoLogic Member is assigned ScannerLogic() at runtime. Well the problem is that when ScannerLogic is called, the computer hangs up with a violation error. If I change CnMoveObject(Object.ID,-1,0,0); by CnMoveObject(1,-1,0,0) I mean a hardcoded value everything works well. I think it has something to do with the function assigment, but don´t know. Thanks in advance, HexDump.
Advertisement
Check if Object in ScannerLogic is nil. This could happen if you forgot to use : instead of . somewhere. (e.g. calling Enemies[1]:DoLogic(); is the same as calling Enemies[1].DoLogic(Enemies[1]);)
it shouldn´t be nil, because the fuction is being called. And I have tried both operators '.' and ':'.

Thanks in advance,
HexDump.
Incredible, you were right Object is nil, but the function is being called using Object instance :(. Why could be this?.

Thanks in advance,
HexDump.
I´m trying to execute core code from C with loadstring instead of using dostring, but loadstring seems not to exist :?. What's up?.


Thnakns in advance,
HexDump.
The problem could be the way you instance your enemies. This would be the wrong way:

Enemy = {name="Default Name",position={0,0,0},skill=100}

enemyList={Enemy,Enemy,Enemy}

enemyList[1].name = "Bob"
enemyList[2].name = "Jimmy"

This is the correct way:

function NewEnemy(enemyName,enemyPosition,enemySkill)
return {name=enemyName,position=enemyPosition,skill=enemySkill}
end

enemyList={NewEnemy("Bob",{0,0,0},100),NewEnemy("Jimmy",{0,0,0},100)}
Everything, tables and funcions are pushed from C to Lua
using dostring.

I create a new enemy using:

->This function is pushed to Lua with DoString.
function MakeEnemy()
Instance={Speed=0;Enabled=0;Life=0;DoLogic=0;ID=0;}
Enemies[nNextEnemyID]=Instance;
nNextEnemyID=nNextEnemyID+1;
end;

->Now I load all IA functions and bound them to the objects
->The IA functions are load with DoString too
Enemies[nNextEnemyID-1].DoLogic=ScannerLogic;

->But when I do this, I have checked that Enemies[1] is nil
Enemies[1].DoLogic(Enemies[1]);


Really weird.

Edited: I have ran the code using LUAIDE and Enemies[1] seems to be ok, not nil :(. Does it has anything to do with DoString?.
I would like to use LoadString, but when I use it the compilers says that it is an undeclared identifier :(. I´m desesperated.


Thanks in advance,
HexDump.

This topic is closed to new replies.

Advertisement