lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> From: Ron Hudson []
> What is the preprocessor that C uses?

Assuming you are using gcc, I think it is cpp.

> I wonder if it could be used with LUA as well

I believe so, it should able from the command line.

> example - I just wrote a StarTrek game in Lua, it would have been good
> to
> be able to say
> -----------------------------------------------------
> #define ENTERPRISE 4
> if Sector[x..y] = ENTERPRISE then write("+") end

Unless you are using parameterised macros I'm not sure there is much
advantage to using macros in Lua. Declaring constants as locals speeds
them up since locals use indices, and globals use a string look up.

local ENTERPRISE = 4
if Sector[x..y] = ENTERPRISE then write("+") end

> -----------------------------------------------------
> btw1  I used tables:  x=5 y=5  Sector[x..y] = somthng
>                         for Sector[55]...
> is there a better way? Tables were nice because I did not have to
> about out of bounds.  This is an opinion question.

Bear in mind that you are creating strings here. The string
concatenation operator coerces the numbers into strings, then joins
them. In this instance you may only get 2 extra strings as Lua does not
store duplicates, so both "5"s will point to the same string in Lua's

x=5 y=5  Sector[x + y*MAX_WIDTH] = something -- for Sector[55]...

...should be a lot faster and give you less hiccups with garbage

Or you have a two dimensional array:

x=5 y=5  Sector[y][x] = something -- for Sector[55]...

Aside: mmm, in Python you could do:
	Sector[(x,y)] = value
But in Lua, the array index is hashed from the table address and not the
contents. I.e.
	Sector[{3,3}] = 7
	print( Sector[{3,3}] ) -- would not print 7, most likely, nil
> -------------------------------------------------------
> btw2  I found that lua 4.0    write() == lua 5.0  io.write ==
> So a simple edit/search/replace fixes that.. right?

Yes, or you can do:

	write = io.write

> Is there a place to upload my star trek game? (for comments :^)
> ron.

In can be uploaded to the wiki and a link added to your homepage: