Author Topic: Learning Lua  (Read 626 times)

Offline Joseph Hewitt

  • Administrator
  • Hero Member
  • *****
  • Posts: 2552
    • View Profile
    • http://www.gearheadrpg.com
Learning Lua
« on: October 31, 2010, 05:27:38 AM »
I'm learning Lua with an eye to replacing ASL. There's a blog post about it here:

http://www.gearheadrpg.com/?p=93

I have two big questions that need to be resolved before I can start experimenting in earnest. The first is about the best way to store and trigger the scripts, but I'm not going to worry about that just yet. The second thing is that I need a way for the component scripts of a randomly generated plot to be combined. This one is trickier.

Any ideas?

Offline SharkD

  • Hero Member
  • *****
  • Posts: 1009
    • View Profile
    • Isometricland
Re: Learning Lua
« Reply #1 on: November 03, 2010, 08:56:26 PM »
I am fluent in Lua.  :) I'd be happy to help.

There are three methods using tables (hash arrays) that I can think of:

Code: [Select]
-- METHOD 1 - creating the scripts and then copying them into a table

-- initialize the scripts table
scripts = {}

-- create the script
function myscript_1()
print("first method")
end

-- store a copy (not a pointer!) of the script inside the table
scripts[1] = myscript_1


-- overwrite the original script to show that they are independent
function myscript_1()
print("changed method")
end

-- execute the script stored inside the table
scripts[1]()

-- execute the overwritten script via the original pointer - notice the output is different since there are now two separate copies!!
myscript_1()
Code: [Select]
-- METHOD 2 - creating the scripts inside the tables to begin with

-- initialize the scripts table
scripts = {}

-- create a script inside the table
scripts[1] = function ()
print("second method")
end

-- exscute the script
scripts[1]()
Code: [Select]
-- METHOD 3 - creating the scripts and then storing strings referencing them inside a table, which are later evaluated as code

-- initialize the scripts table
scripts = {}

-- create the script
function myscript_2()
print("third method")
end

-- create a string-ified copy of the code you want to execute inside the table
scripts[1] = "myscript_2()"

-- evaluate the string and execute it as if it were code
assert(loadstring(scripts[1]))()

Since you are using tables, you can manipulate them just like other tables (or arrays), such as choosing items randomly.

I would personally go with method 2, since method 1 clutters memory needlessly with multiple identical copies of the same function, and method 3's evaluation of strings-as-code is *very* poor programming practice and a *huge* waste of resources.

Here's another example using method 2, this time using three scripts, shorthand syntax and a "for" loop to iterate through the table:

Code: [Select]
-- initialize and populate the scripts table
scripts =
{
function ()
print("Hello,")
end,
function ()
print("world!")
end,
function ()
print("Success!")
end
}


-- calling each script individually
scripts[1]()
scripts[2]()
scripts[3]()

-- iterating through the table via numerical indices
for i = 1, 3 do
scripts[i]()
end

-- iterating through using the "ipairs" Lua function
for i, myscript in ipairs(scripts) do
myscript()
end

You can also assign unique names to the table items, which allows you to do things like sort the scripts alphabetically by name:

Code: [Select]
-- initialize and populate the scripts table
scripts =
{
   firstscript = function ()
      print("Hello,")
   end,
   secondscript = function ()
      print("world!")
   end,
   thirdscript = function ()
      print("Success!")
   end
}

-- calling each script individually
scripts["firstscript"]()
scripts["secondscript"]()
scripts["thirdscript"]()

-- or using different syntax...
scripts.firstscript()
scripts.secondscript()
scripts.thirdscript()

-- iterating through the table using the "pairs" Lua function
for i, myscript in pairs(scripts) do
   myscript()
end

-- sort the table alphabetically
table.sort(scripts)

-- iterating through the table using the "pairs" Lua function - the output should now be different!
for i, myscript in pairs(scripts) do
   myscript()
end

Hope that helps!
« Last Edit: November 03, 2010, 10:38:54 PM by SharkD »

Offline SharkD

  • Hero Member
  • *****
  • Posts: 1009
    • View Profile
    • Isometricland
Re: Learning Lua
« Reply #2 on: November 03, 2010, 09:23:23 PM »
As for "combining" scripts, I'd have to see a specific example, as I'm not sure exactly what you mean. Usually you facilitate this sort of thing by creating generic scripts that implement the actual behaviors but accept parameters as input, and then dealing with the parameters using wrapper scripts.
« Last Edit: November 03, 2010, 09:55:31 PM by SharkD »