Sign in to follow this  
disanti

Lua Behaving Differently on Seperate Machines

Recommended Posts

Hello, I've been programming a game that uses Lua as its scripting engine, and it has been working fantastically on my computer. However, I sent the game to a friend to try, and Lua is behaving differently on his machine. I don't think it could be a DLL hell issue as Lua is statically linked to my application. Basically I have a table defined in the script as follows:
mytable = {
   indexname = value,
   ...
   pos = {0.0,0.0},
   ...
}

function foo()
   mytable.pos = GetPosition()
   ...
   mytable.pos[1] = mytable.pos[1] + somevalue
   mytable.pos[2] = mytable.pos[2] + anothervalue
   SetPosition(mytable.pos[1],mytable.pos[2])
end

Now this works great on my computer, but on my friends, Lua claims that it cannot index 'pos' because it is a nil value. (Note that function foo is called 60 times per second by the main game loop, this is when it fails.) So in order to assess this, I had him put a print(mytable.pos) after it is set to GetPosition(), and it is printing 'nil'. On my computer, it is printing Table: somenumber. I cannot figure out why my C bindings would behave differently on different computers, but I will post my GetPosition code below:
int CSpriteBehaviorScript::GetPosition(lua_State* L)
{
	CRect<int> rect = mSprite->GetRect(); // get the position

	lua_newtable(L); // create the return value table and put it on a stack

	// push index[1] to the table
	lua_pushnumber(L, 1);
	lua_pushnumber(L, rect.x);
	lua_settable(L, -3);

	// push index[2] to the table
	lua_pushnumber(L, 2);
	lua_pushnumber(L, rect.y);
	lua_settable(L, -3);

	return 1; // pass one return value to lua
}

Any ideas as to how I can fix this are appreciated. Thanks!

Share this post


Link to post
Share on other sites
I'm having a similar problem, Linux verses Windows. My code works fine on Linux, but on Windows things start to screw up.

I've patched in my Vector and Matrix classes using ToLua. In my demo hovercraft script I create an array of vectors for the thruster positions. I call the Lua function OnApplyForceAndTorque, during which I do the necessary calculations with the thruster positions and forces/torques. On Linux, all is fine and floaty, but on Windows I get the Lua error 'Table contains a nil' every time I reference an array item.

Here's a relevant extract:


-- Craft size
Size = SYM_VECTOR3(20, 10, 40)

-- Thruster placement
Thrusters = { SYM_VECTOR3( Size.x * 0.6, Size.y * 0.1, Size.z * 0.6),
SYM_VECTOR3(-Size.x * 0.6, Size.y * 0.1, Size.z * 0.6),
SYM_VECTOR3( Size.x * 0.6, Size.y * 0.1, 0.0),
SYM_VECTOR3(-Size.x * 0.6, Size.y * 0.1, 0.0),
SYM_VECTOR3( Size.x * 0.6, Size.y * 0.1, -Size.z * 0.6),
SYM_VECTOR3(-Size.x * 0.6, Size.y * 0.1, -Size.z * 0.6)}

...

function oOnApplyForceAndTorque(GeomName)

if GeomName == "HCraft"
then

-- Get matrix
Matrix = getmatrix("HCraft")

-- Get hovercraft omega
Omega = getomega("HCraft")

-- Get force direction in global space
ThrusterDir = Matrix:UnrotateVector(SYM_VECTOR3(0, 1, 0))

TotalForce = GravityForce
TotalTorque = SYM_VECTOR3(0, 0, 0)

-- Loop through thrusters
for i = 1,6
do
-- **ERROR OCCURS HERE**
io.write(Thrusters[i].x.." "..Thrusters[i].y.." "..Thrusters[i].z.."\n")
-- Get thruster position in global space
ThrusterPos = Matrix:TransformVector(Thrusters[i])

...



I've had all sorts of fine suggestions from the #Lua crew on Freenode, like accidentally linking Lua 5.0 with Tolua 5.1, but nothing that helps this problem.

If you find any clues or solutions Disanti, please post.


Share this post


Link to post
Share on other sites
OK, I just compiled my game on my Xubuntu laptop, and it is executing the script with no problems at all.

I also forgot to mention in the original post that my friend was able to successfully run the script ONCE, and it never worked again after that. My friend's PC has Vista, but I wouldn't think that that would have anything to do with the problem. My development PC is running XP if that makes any difference.

Deadstar, did the version of Windows that your script failed on happen to be Vista? If it is I think we might be on to something.

Share this post


Link to post
Share on other sites
Strange problem...
Are mytable={...} and function foo()...end in the same script?
Are you sure the table get initialized always before foo is called?(mytable!=nil?)
Do you open the script files with a 'read-only' flag?
Maybe there is some threading issue. Does the games works on vista in Windows XP or Windows 95 compatibility mode?

Share this post


Link to post
Share on other sites
The table and the function are both in the same script, and I printed the table inside the function, and it is not nil on both machines. However, on the Vista machine, the table.pos is nil, while it is a value on the XP and Linux machines.

I will have my friend turn on XP compatibility and see if that fixes anything.

Share this post


Link to post
Share on other sites
My Windows install is XP, service pack 3.

It seems I can reference array elements everywhere else except in that OnApplyForceAndTorque function. It's called during Newton's ForceAndTorque callback, so nothing special.

Does Newton do any strange stuff before triggering this callback?

Disanti, are there places inside your script where you CAN reference array elements? Or is it failing wherever you try it?

Share this post


Link to post
Share on other sites
Quote:
Original post by deadstar
It seems I can reference array elements everywhere else except in that OnApplyForceAndTorque function. It's called during Newton's ForceAndTorque callback, so nothing special.

Is the callback called from the main thread? I don't know how Lua behaves when the same lua_State is used simultaneously from different threads.
Whatever Newton does before calling the callback shouldn't affect Lua

Share this post


Link to post
Share on other sites
I don't believe Newton is multithreaded, but I could be wrong. I just noticed that I am indexing the table successfully at other parts of the code. Its just at the end of the function that it has a problem. Of course, I don't think the other indexing code is called as its condition is not met.

Share this post


Link to post
Share on other sites
Quote:
Original post by disanti
I don't believe Newton is multithreaded, but I could be wrong. I just noticed that I am indexing the table successfully at other parts of the code. Its just at the end of the function that it has a problem. Of course, I don't think the other indexing code is called as its condition is not met.


See if you can figure out a pattern to it. I've tried accessing the array in as many places as possible, and it seems to only be the physics callback where it fails.

And no, Newton isn't multithreaded, as far as I know.

Share this post


Link to post
Share on other sites
OK, my friend noticed that he was starting to have other problems on his computer, so he reinstalled Windows, and now my game works for him (although it did get the same error once, every other time seems to be working for him.)
I have no idea what could cause this, but now I don't have a problematic machine to test it with.

Share this post


Link to post
Share on other sites
deadstar have u tried debugging in an interactive debugger like Decoda?

I find when i have some runtime issues, it's best to just break into the code and look at all the variables and step through it line by line. If clearly the array is fubar then something must be up with Lua itself, and then u can place checks all over your code (Lua) to check for when and how it gets corrupt.

You can download Decoda from:

http://www.unknownworlds.com/decoda

A helpful tip, if your building Lua as a staic lib ( which i do ) you will want to change the Lua API macro LUA_API extern to LUA_API __declspec(dllexport) that will allow you to hook into release builds with Decoda.

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
OK, I just started getting the error on my system, so I could actually debug it. It turns out that it was all caused by an uninitialized boolean. So basically, there was a 50% chance that the game would work at all, and I'm very surprised that it has been working on my computer for so long.

Deadstar, I hope you can figure out what the trouble is on yours. Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by disanti
OK, I just started getting the error on my system, so I could actually debug it. It turns out that it was all caused by an uninitialized boolean. So basically, there was a 50% chance that the game would work at all, and I'm very surprised that it has been working on my computer for so long.


Just an aside, in case you're interested...

Actually, you'll find that boolean types are rarely if ever implemented as a bit. A byte is much more likely, so the odds are 255:1 that an uninitialized bool will be true.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this