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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

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

Post 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.
Last edited by PixelP on Fri Dec 05, 2008 1:03 pm, edited 1 time in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua problems...

Post 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().
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Lua problems...

Post by trufun202 »

Yeah...try breaking it on purpose. That'll usually expose what is or isn't happening.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua problems...

Post 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?
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Lua problems...

Post 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...
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua problems...

Post 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?
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Lua problems...

Post 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;
}
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua problems...

Post 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?
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Lua problems...

Post 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.
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Lua problems...

Post 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
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Lua problems...

Post 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
-----
?
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua problems...

Post 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.
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Lua problems...

Post 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();   
        }
...
...
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua problems...

Post by Falco Girgis »

Show me Map.Close().
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Lua problems...

Post by PixelP »

Map is a filestream.

Code: Select all

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