PDA

View Full Version : Lua is interesting



User137
01-02-2014, 02:45 PM
Just to clear this off my mind, i really want to promote LUA scripting. I may be seeking ways to use it in pascal games in the future, because it has huge potential and the language is easy and feature rich. There exist many games already that use Lua, and gives players ways to add modded content. But it's not only mods i'm interested about, it's when i think about designing a UI using OpenGL graphics, i can't use Lazarus to draw it like TForm and components. I would be able to make editor that uses Lua scripts to access an API that implements features in the program that UI is allowed to use.

Now if you are completely new to the language, i would mainly direct you to Google, but i can give few examples. Here is 1 script i wrote to Minecraft mod, which lets me program kind of bots that do things like mining for me. It's almost like modding a mod :D
http://pastebin.com/JaJQcv4q
As you may see the syntax is very pascal-like. With exception that all blocks are multiline and are finished with "end". No ";" are used to finish lines, and variables are not needed to initialize or even have types. So some simple scripts could be:

To print numbers from 2 to 10:

for i = 2, 10 do
print(i)
end

And, although not recommended but possible by language, changing variable type on the fly:

i = 10
print(i)
i = "Number is "..i
print(i)
Would print "Number is 10".

But in application, we could make API like UI, and call it like "panel = ui.createPanel()" and so on, giving access to the pascal executable. If we can reduce some of the complicated program logic to this kind of simple scripts, we don't always need to compile the whole program to see major changes in it, if only the script is modified. More people knows how to work with scripts, than specific language source code, so it would also encourage more people joining on projects.

laggyluk
01-02-2014, 06:03 PM
agree, for me lua is mind bending
this example stores functions in a table using function name as 'index' and let's them execute later


spawnClocks = {}

function spawnBomber()
...
template.from(sharkBomberTemplate, worldContainer, params);
end

function setupSpawnClocks()
for k,v in pairs(spawnClocks) do v = nil end
if gameLevel==1 then
spawnClocks[spawnBomber] =
{
maxTime = 15,
minTime = 10,
timer = 4
}
end
end

function spawnEnemy()
for k,v in pairs(spawnClocks) do
v.timer= v.timer - deltaT;
if v.timer <=0 then
v.timer = math.random(v.minTime,v.maxTime)
k() --execute spawnBomber
end
end
return 1;
end

SilverWarior
01-02-2014, 09:27 PM
Yes I must admit that LUA is quite good. I to started using LUA for the first time in Minecraft's ComputerCraft mod.
And by using ComputerCraft mod along with RaillCraft mod I managed to make full automated raill system (automated departures based on predefined timetable, computer controllable raill crossings, each station had its own display with arrival times for incoming trains, etc.) but then the server got griefed and I lost everything.
I wish ComputerCraft would alow Importing and Exporing LUA files in Multiplayer games.

But if I ever decide to use a scripting language I would probably go and use DWScript instead. Why?
Since DWScript bases on Delphi like language it means that I can quickly move part of the code from Script to native code or vice-versa by simply copying it. You can't do this with LUA. You would have to do translation from LUA to Pascal and vice-versa.
Also based on many tests which you can find on internet DWScript has verry good performance in comparison to native Delphi code.

Cybermonkey
01-02-2014, 10:04 PM
So if you want to see a little retro like Lua interpreter, grab it on http://cmb.retrogamecoding.org/ (The zip contains binaries for Windows and Linux 32 & 64 bit). It might benecessary to instal liblua5.2 on Linux.
Here's an example script for ChipmonkeyLua:

--' my favourite plasma demo
--' ported from smallbasic to yabasic to lua with CMLua
--' by cybermonkey
activepage (1)
cls()
cols={}
cols[0]={}
cols[1]={}
cols[2]={}

for i=0,255 do
cols[0][i]= math.abs(128 - 127 * math.sin(i * math.pi / 32))
cols[1][i]= math.abs(128 - 127 * math.sin(i * math.pi/ 64))
cols[2][i]= math.abs(128 - 127 * math.sin(i * math.pi / 128))
end

time1=gettickcount()
for y = 1,599 do
for x =1,799 do
c = int((math.sin(x/35)*128+math.sin(y/28)*32 + math.sin((x+y)/16)*64))

if c >255 then
c = c - 256
end
if c < 0 then
c = 256 + c
end

r = cols[0][c]

if r > 255 then
r = r - 256
end
if r < 0 then
r = 256 + c
end
g = cols[1][c]

if g >255 then
g = g - 256
end
if g < 0 then
g = 256 + c
end
b = cols[2][c]
if b>255 then
b = b - 256
end
if b < 0 then
b = 256 + c
end

ink ( hrgb (r, g, b))
pset (x, y)

end
end
time2=gettickcount()
ink (hrgb (0,200,200))
locate (0,40,"")
print ("Time needed: "..(time2-time1).." ms")
visualpage(1)

The result is this:
1236

All is done in Free Pascal using (hold your breath ...): ptcgraph, ptccrt and ptcmouse units. ;D

It also has an interactive mode which looks like a vintage BASIC interpreter.
1237

paul_nicholls
02-02-2014, 02:28 AM
Lua IS awesome, but I've been thinking of redoing my old scripting language so I can use it easily on any platform I want :)

User137
02-02-2014, 03:51 PM
I wish ComputerCraft would alow Importing and Exporing LUA files in Multiplayer games.
It does have shell commands for pastebin put and get. I wrote my programs with notepad++.

SilverWarior
02-02-2014, 08:45 PM
It does have shell commands for pastebin put and get. I wrote my programs with notepad++.

True but most Minecraft multiplayer servers have that blocked due to posible security risk of ComputerCraft.

pitfiend
03-02-2014, 06:07 PM
Found a minor bug, I use an international english layout to be able to use spanish (my primary language) accents, so " or ' chars need a space hit after to appear in any input. Unfortunately the interpreter didn't print/accept it, just the space. Setting back a US layout allows me to work fine.