Page 1 of 2

Lua problems... Don't bother to read.

Posted: Wed Nov 19, 2008 3:29 pm
by PixelP
In my game I've got a function that closes the current map from lua.. It looks like this:

Code: Select all

    int KillMap(lua_State* L) {
       Map.close();
       return 1;
    }
When I'm using it in the lua main loop it works fine.

Code: Select all

    --main.lua
    function Main()
       KillMap()
    end
But when I try to use it in a if-statement it doesn't work...

Code: Select all

    --main.lua
    function Main()
        tilex, tiley = GetPlayerXY()
        if tilex == 5 and tiley == 2 then
           KillMap()
        end
    end
It just keeps going as if nothing has happend.
Any ideas what I could have made wrong?
Thanks.

Re: Lua problems...

Posted: Wed Nov 19, 2008 3:32 pm
by Falco Girgis
Hrm, that's intersting. Have you tried doing anything else in the condition? I'm wondering if it's your GetPlayerXY() rather than your actual KillMap() function. Maybe try printing or doing something that you know to work in that condition? At least then you would know for sure that it's an issue with KillMap().

Re: Lua problems...

Posted: Wed Nov 19, 2008 3:53 pm
by trufun202
Yeah...try breaking it on purpose. That'll usually expose what is or isn't happening.

Re: Lua problems...

Posted: Wed Nov 19, 2008 3:57 pm
by Falco Girgis
I hypothesize that his GetPlayerXY() is not returning what he thinks. Perhaps its returning by pixel, while you are checking by tile. Perhaps its returning by pixel/tile onscreen?

Re: Lua problems...

Posted: Wed Nov 19, 2008 4:25 pm
by PixelP
I tried using other conditions and spotted something interesting:
The player starts at the tiles 1, 1.
I tried this:

Code: Select all

--main.lua
function Main()
	dofile("scripts/globals.lua")
	dofile("scripts/menu.lua")
	dofile("scripts/movement.lua")
	dofile("scripts/events.lua")
	dofile("scripts/functions.lua")
	dofile("scripts/text.lua")
	dofile("scripts/input.lua")

	if ytile == 1 then
		KillMap()
	end

	Input()
	Events()
	Text()
end
And it worked!

Then I tried this:

Code: Select all

--main.lua
function Main()
	dofile("scripts/globals.lua")
	dofile("scripts/menu.lua")
	dofile("scripts/movement.lua")
	dofile("scripts/events.lua")
	dofile("scripts/functions.lua")
	dofile("scripts/text.lua")
	dofile("scripts/input.lua")

	if ytile > 1 then
		KillMap()
	end

	Input()
	Events()
	Text()
end
This time it didn't work.
It's like it just checks the if-statement only once...

Re: Lua problems...

Posted: Wed Nov 19, 2008 4:31 pm
by Falco Girgis
Can I see where you are calling that function in the engine? Are you calling it every frame?

Also, can you paste your GetPlayerXY() function?

Re: Lua problems...

Posted: Wed Nov 19, 2008 4:47 pm
by PixelP

Code: Select all

   ...
   ...
   //load lua scripts
	luaL_dofile(Lua, "scripts/init.lua");
	luaL_dofile(Lua, "scripts/main.lua");

	//game loop
	while(!quit) {
		timer.Start();
		while(SDL_PollEvent(&event)) {
			if(event.type == SDL_QUIT) {
				quit = true;
			}
			//player input
			player.Input();
		}
		//call lua main functions
		lua_getglobal(Lua, "Main");
		lua_call(Lua, 0, 0);
...
...

Code: Select all

//get the players tile coordinates
int GetPlayerXY(lua_State* L) {
	lua_pushinteger(L, xtile);
	lua_pushinteger(L, ytile);
	return 2;
}

Re: Lua problems...

Posted: Thu Nov 20, 2008 10:12 am
by Falco Girgis
Wow, I really don't see anything. You are updating/calling GetPlayerXY() every frame, right? I don't see it in your main, but I'm assuming that it's happening in one of the dofile()s?

trufun, do you see anything?

Re: Lua problems...

Posted: Thu Nov 20, 2008 10:53 am
by PixelP
GyroVorbis wrote:You are updating/calling GetPlayerXY() every frame, right? I don't see it in your main, but I'm assuming that it's happening in one of the dofile()s?
Yeah, I update GetPlayerXY() every frame, I got another lua script for that which I include in the lua main function.

Re: Lua problems...

Posted: Thu Nov 20, 2008 11:13 am
by trufun202
GyroVorbis wrote:Wow, I really don't see anything. You are updating/calling GetPlayerXY() every frame, right? I don't see it in your main, but I'm assuming that it's happening in one of the dofile()s?

trufun, do you see anything?
I'm wondering if it's bombing out when its executing all of the other scripts, and it never gets to the ytile > 1 condition.

My suggestion would be to write some debug messages to a file, so you can see how far its getting. You'll have a huge file if its being called every frame, but it will let you know what's going on.

Try something like this:

Code: Select all

--main.lua
function Main()

   WriteToLog("BEFORE other lua calls"); 

   dofile("scripts/globals.lua")
   dofile("scripts/menu.lua")
   dofile("scripts/movement.lua")
   dofile("scripts/events.lua")
   dofile("scripts/functions.lua")
   dofile("scripts/text.lua")
   dofile("scripts/input.lua")

   WriteToLog("AFTER other lua calls"); 

   if ytile > 1 then
      WriteToLog("BEFORE KillMap() ytile = " .. ytile); 
      KillMap()
      WriteToLog("AFTER KillMap() ytile = " .. ytile); 
   else
      WriteToLog("ytile = " .. ytile); 
   end

   Input()
   Events()
   Text()
end

Re: Lua problems...

Posted: Thu Nov 20, 2008 11:16 am
by PixelP
Good idea, I'll try it.

EDIT:
So, I did what you sad.

Code: Select all

--main.lua
file = io.open("debug.txt", "w")
function Debug(string)
	file:write(string .. "\n-----\n")
end
function Main()
	Debug("before includes")
	dofile("scripts/globals.lua")
	dofile("scripts/menu.lua")
	dofile("scripts/movement.lua")
	dofile("scripts/events.lua")
	dofile("scripts/functions.lua")
	dofile("scripts/text.lua")
	dofile("scripts/input.lua")
	Debug("after includes")

	if y > 2 then
		Debug("before KillMap() y: " .. y) -- i changed ytile to y and xtile to x
		KillMap()
		Debug("after KillMap() x: " .. x)
	end

	Input()
	Events()
	Text()
end
I got this result from debug.txt:

Code: Select all

before includes
-----
after includes
-----
before includes
-----
after includes
-----
before includes
-----
after includes
-----
before includes
-----
after includes
-----
before includes
-----
after includes
-----
before includes
-----
after includes
-----
before includes
-----
after includes
-----
before KillMap() y: 3
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 3
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 3
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 3
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 3
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 4
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 4
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 4
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 4
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 5
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 5
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 5
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 5
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 6
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 6
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 7
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 7
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 7
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 7
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 7
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 8
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 8
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 8
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 8
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
before includes
-----
after includes
-----
before KillMap() y: 9
-----
after KillMap() x: 1
-----
?

Re: Lua problems...

Posted: Thu Nov 20, 2008 12:58 pm
by Falco Girgis
...

Okay... add a printf/cout/WriteToLog() to your C/++ KillMap() function just to make sure it's being called correctly. Also, post your KillMap() function (C/++ source (Map::close()) and wrapper).

edit: You can also try doing it in your C++ heartbeat like:

Code: Select all

if(y > 1) Map.close();
That would tell you if it's even a Lua issue or not.

Re: Lua problems...

Posted: Thu Nov 20, 2008 2:12 pm
by PixelP
Man.. It's not even working in the engine...

Code: Select all

//game loop
	while(!quit) {
		timer.Start();
		while(SDL_PollEvent(&event)) {
			if(event.type == SDL_QUIT) {
				quit = true;
			}
			//ash input
			ash.Input();
		}
		//lua functions
		lua_getglobal(Lua, "Main");
		lua_call(Lua, 0, 0);
		
		if(y > 1) {
            Map.close();   
        }
...
...

Re: Lua problems...

Posted: Thu Nov 20, 2008 2:13 pm
by Falco Girgis
Show me Map.Close().

Re: Lua problems...

Posted: Thu Nov 20, 2008 2:17 pm
by PixelP
Map is a filestream.

Code: Select all

std::fstream Map;
Map.open("maps/town.map", std::fstream::in);
Map.close();