# Lua Behaving Differently on Seperate Machines

This topic is 3668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 sizeSize = SYM_VECTOR3(20, 10, 40)-- Thruster placementThrusters = { 	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.x.."  "..Thrusters.y.."  "..Thrusters.z.."\n")			-- Get thruster position in global space			ThrusterPos = Matrix:TransformVector(Thrusters)...

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 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 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 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 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 on other sites
Quote:
 Original post by deadstarIt 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 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 on other sites
Quote:
 Original post by disantiI 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 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.

1. 1
2. 2
3. 3
4. 4
frob
11
5. 5

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632175
• Total Posts
3004585

×