Welcome! Log In Create A New Profile

Advanced

Lua to build my in game levels

Posted by owen 
Lua to build my in game levels
February 12, 2011 08:18PM
I am looking into using it for initializing and animating the levels in my game. Presently I have the levels hardcoded in C. Lua seems offer some flexibility as oppose to having fixed levels. Hopefully by the time my game engine is finished I will be able to get some people add levels using the Lua interface.

I saw the astronomy project. Anybody else currently using Lua for any wii homebrew project? and are there any problems that I may encounter? Lag, etc



Edited 1 time(s). Last edit at 02/12/2011 08:21PM by owen.
Re: Lua to build my in game levels
February 13, 2011 01:00AM
I'm using lua for animated stages in GuitarsOnFire. And quite a few commercial games are using it for things like AI an UI. Don't worry about the speed, it's quite fast. Just make sure you understand the C api, and start small first.
Re: Lua to build my in game levels
February 13, 2011 02:16AM
Getting it installed in devkitpro was the initial headache but it seems to be working now. I've managed to get one function to be called straight from the lua script. That should be enough for now. All I will do is call a Lua function once before the stage starts (to set up the scene) and another Lua function while the stage is running. I plan to expose as much as is needed to Lua and then convert my current stages into Lua Scripts.

Hopefully its not too much of a headache to convert the current stages from C to Lua.
Re: Lua to build my in game levels
February 14, 2011 05:14AM
Got a marco conflict between Grrlib and Lua. Apparently they both have a macro called G(). I renamed the one in Grrlib since it had the least code.

My lua script seems to be running, I ignored your advice and wrote a long ass Lua script which was a pain to debug but I got it working. I have one stage running now and all I have to do is beef up the API and figure out how to dynamically load the files.
Re: Lua to build my in game levels
February 15, 2011 04:32AM
Getting weird stretched shapes when I include Lua. @diad do you use it with GRRLIB?
[grrlib.santo.fr]
Re: Lua to build my in game levels
February 15, 2011 10:54AM
I don't use GRRLIB. But the conflict is a problem, a quick look show both libraries seem to use the G() macro in other macros, at which point the "undef" suggested in the other forum won't do.

The only workable solution might be to include Lua or GRRLIB from each source file, but never both.
Re: Lua to build my in game levels
February 15, 2011 05:18PM
Once I declare a function with Lua_state
static int test2(lua_State *L) { 
    return 0;
}

then the stretched shape error occurs.

@Diad I tried that this morning. I copied all the LUA references into a lua_api.c and included a lua_api.c into main.c but the artifact is still occuring. Now I'm confused.
lua_api.c looks like this; [pastie.org]

Even if I include it in separate source files I think they eventually get linked together if I try to use the lua stuff from main.c
Re: Lua to build my in game levels
February 15, 2011 06:06PM
You're not getting C right. You don't, EVER include C files. If you ever see someone including a C file, you smack them, if someone suggest you include a C file you smack them twice.

I assume you are a starting programmer?

As an example, you have 2 C files, and 1 header file:

main.c
#include 
#include "file2.h"
int main(int argc, char** argv)
{
  function_in_file_2(10);
}
file2.c
#include 
#include "file2.h"
void function_in_file_2(int bla)
{
  printf("%i\n", bla);
}
file2.h
#ifndef FILE2_H
#define FILE2_H

void function_in_file_2(int bla);

#endif//FILE2_H

Then you need to compile the 2 C files, and link them together as 1 executable. The header file (file2.h) lets the compiler know that the functions in file2.c exist while it is compiling main.c. Without ever touching file2.c.
Re: Lua to build my in game levels
February 15, 2011 06:14PM
LOL, I am totally sorry for that faux pas. I have been coding for a while but the library conflict is new to me, especially since I avoid using external apis. lol, I really never had to use them before. I will try that tonight. One question though, why do you have that empty include in the first line of your example code?
#include 
#include "file2.h"



Edited 1 time(s). Last edit at 02/15/2011 06:23PM by owen.
Re: Lua to build my in game levels
February 16, 2011 10:39AM
Stupid forum :P you could have seen it if you quoted me. But it's < stdio.h>, force of habit to always include it.
Re: Lua to build my in game levels
February 16, 2011 06:15PM
well every thing is nice and separated now and I am no longer getting the macro redefined error. The shape stretching bug is still in there but now I suspect that it is as a result of some kinda loop bug in my code. Thanx for your help. :)

All I have to do now is loop through the directory and pickup the Lua files. For those interested here is a nearly finished stage in Lua code: [pastie.org] (one thing I noticed is that the Hex colors are different when they reach on the C side but I have not had a chance to debug that as yet )
Re: Lua to build my in game levels
February 18, 2011 06:06PM
So I'm am pretty much on track for my next update/release except I have discovered another issue. I cannot seem to pass hex colors from lua to C correctly. Every color I pass from lua to C comes across as Cyan or Black.

here is how I declare and use the color in the LUA;
--primary colours
 GRRLIB_SILVER  =0xC0C0C0FF
 GRRLIB_RED     =0xFF0000FF
 GRRLIB_LIME    =0x00FF00FF

 set_obj_color( 1, GRRLIB_RED, 0 );

on the c side I do this;

static int lua_set_obj_color(lua_State *L) {
	q_set_color( luaL_checkint(L, 1), luaL_checklong(L, 2) );
    return 0;
}

lua_register(l, "set_obj_color", lua_set_obj_color );

int q_set_color(int p, u32 c ) {
	o=obj_info[p];
	set_color( c, &o ); 
	obj_info[p]=o;				
    return 0;
}

I've tried luaL_checkint and luaL_checklong both with the same wrong results. If I hardcode a value in the lua_set_obj_color() code then it works fine. I am not sure what I am doing wrong.
Re: Lua to build my in game levels
February 18, 2011 08:30PM
Most likely, the number is converted to a float, which is causing your problems. Lua has a tendency to convert numbers to floats. Maybe you should pass an array of 4 numbers, for R,G,B and A. And make the final color in C code.
Re: Lua to build my in game levels
February 19, 2011 04:32AM
Lua should be using doubles rather than floats though, and they have enough mantissa bits to handle 32-bit ints. Check what LUA_NUMBER is defined to in luaconf.h.
Re: Lua to build my in game levels
February 19, 2011 09:20AM
I thought the wii didn't have a 64bit floating point unit and thus converts doubles to normal floats? But I could be wrong.

Also, something to consider, the byte ordering for the wii is the other way around then for intel.
Re: Lua to build my in game levels
February 19, 2011 09:37AM
The wii has 32 64-bit floating point registers that can munch doubles without breaking a sweat.
Re: Lua to build my in game levels
February 19, 2011 03:13PM
So i can just change the type of LUA_NUMBER defined in luaconf.h OR do I have to re-install the entire LUA library into devkitpro. I am not at my computer right now.
Re: Lua to build my in game levels
February 19, 2011 09:26PM
My Luaconf.h file has the LUA_NUMBER set to double, still doesn't work. here is the file: [pastie.org]
Re: Lua to build my in game levels
February 19, 2011 09:27PM
My conf.h file has the LUA_NUMBER set to double, still doesn't work. here is the file that I am currently using: [pastie.org]
Re: Lua to build my in game levels
February 19, 2011 10:48PM
Its working now. I should be using luaL_checknumber(L, 2) instead of luaL_checklong(L, 2) when retrieving the Hex value. Thanks for you help. now I can finally move on. :)
Sorry, only registered users may post in this forum.

Click here to login