View Full Version : Lua scripting for a beginner (myself)!

06-18-2009, 01:04 AM

I'm sitting and doing some small scale reading on lua, looking over some code. The thing is, I'm a complete novice when it comes to (any) code, although I understand some of the syntax.

So today, it's lua. I want to edit/refine some code for personal use, in this case it is a game that uses lua files. What I am doing in particular, is extending a bunch of console commands for the game. I'm not editing the existing code, as I'm sure it would screw up something along the line. :P

Below is an example of the code I am editing.

cheat = {

giveAbrams = function(num)
local dist = 5
local x = 25 - dist
local y = 25 - dist

if ((num == nil) or (num < 1)) then
num = 1

for count = 1, num, 1 do
local xx = x + count * dist
local yy = y + count * dist
local zz = game.getLandHeight(xx, yy)
game.selectEntity(game.spawnTank("tank_abrams_muss", xx, yy, zz, 0, "usa"))

console.log("CHEAT >> Spawned ", num, " Abrams")


And in the console I type cheat.giveAbrams(1) to spawn an Abrams.
Even I understand the basics of this, and I can do some simple edit (coordinates, entity name and so on), but what I want to do is CHANGE this a bit.

In this game, there is a lot of different types of vehicles available, which means I would have to copy/paste the above code, changing function name and the entity name.

What if I had something similar to this:

cheat = {

giveTank = function(tank)
giveCar = function(car)
giveAir = function(air)



so I could basically type something like cheat.giveTank(abrams), cheat.giveAir(apache) etc.

Does this make any sense at all for you guys? I'm sorry for the long post, but I want to learn something, and I think by examining code made by others, I learn alot! Trial and error I guess =)