The Super Quick Guide to Using LuaVerge
Displaying 1-3 of 3 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Overkill

Ahem. This assumes you know a little BIT about Verge and Lua, but after this you should be ready to go.

The Super Quick Guide to Using LuaVerge

First things first, to enable Lua scripting, you need to add the line "lua 1" to your verge.cfg. This replaces VergeC with Lua as the scripting language.

So okay, great, now we're using Lua! What do you want to do next? Well, you'll want to make a system.lua. Basically the rule is: Anything you'd name with a .vc extension before, you now name with a .lua extension.

Okay, so make a file named system.lua and then put something like the following in there:
-- "autoexec" needs to be lowercase here or it'll whine!
function autoexec()
while not v3.key[v3.SCAN_ENTER] do
v3.ShowPage()
end
v3.Exit("You pressed enter!")
end
Okay, that should be enough to get you going. Next, here's the next rule to using code in Lua. A lot of stuff in VC is accessible in Lua, but not everything. Well, trial and error should figure this out.

Basically you just put "v3.whatever" instead of "whatever", and save for a few adjustments everything works the same.

Note there ARE a few noticeable changes though:
v3.entity[0].x -- Used instead of VC's entity.x[0], similar for other builtin array-of-struct things.
v3.b1 -- ERROR! The "button" variables don't exist. Whatever, make your own.
v3.key[v3.SCAN_LEFT] -- This returns a BOOLEAN, not an int.
Note that a lot of places that use ints to represent "truth" values in VC are now booleans. But there are sometimes a few stray ones that still take ints, so you need to use the following knowledge:
local n = VALUE and 1 or 0 -- Convert boolean into number
local b = VALUE ~= 0 -- Convert number into a boolean
Note that a lot of oddities like these were what further drove me into making the vx library. It makes things fairly different from the way they're in VC.

That should be enough to get you going.

Please add any helpful info about Lua and LuaVerge here. Questions relating to LuaVerge problems should go in the Verge Help boards.

Posted on 2008-12-17 14:29:28 (last edited on 2008-12-17 21:36:22)

Overkill

Other useful tidbits!

To get the length of a string (or list-style table for that matter) don't use len(), use a number sign # in front the string/array instead:
mystring = "Hello"
#mystring -- Gets the length of mystring, which is 5.


To convert a number (or other value) into a string, don't use str(). Use tostring() instead:
tostring(3) -- converts 3 into "3"


VERY important! Only false and nil can be used as "false" values in an if or while statement. Everything else will always evaluate to true:
if "" then print("HEY") end -- true! always executes
if 0 then print("HEY") end -- true! always executes
if {} then print("HEY") end -- true! always executes
if false then print("HEY") end -- false! never executes
if nil then print("HEY") end -- false! never executes


So it's very, very, very important to make sure your if statements compare against something (like if #name > 0 then ... instead of just if #name then ...) unless you only want to check if an expression is not nil.

Now for a BIG gotcha: when a variable or something does not exist, it'll evaluate to nil. If you try and call a nil-value (a function that doesn't exist), or try to do any sort of arithmetic on a nil-value (a variable that doesn't exist), then Lua will error (among other ways to cause it). You can use Lua's quirky if statements to check if something exists though by going like if thevariableyouwanttocheckfor then ... (which only happens if the variable is not nil or false) or by going tostring(thevariableyouwanttocheckfor) (converts to the string "nil" which you could log or something).

Posted on 2008-12-23 23:04:49

Biggs

It's kind of funny: you don't have to use the v3 namespace prefix to program in Lua, at least I haven't had to -- things work like normal.

If I have an outdated version, however, and if the "v3.X" prefix is now required, then there are a couple of things you can try, to remove the need for it (I'm not able to test things right now):

1. Try the following (in case it works, it's the easiest solution): at the top of each .lua file, write:

require "v3"

2. If that didn't work, then we must load the v3 table into _G (the global, default namespace table). Here's a function that takes a table, and puts its members in _G (I think it copies ints, strs, and bools, but functions, tables, blocks, files, and threads have their references copied):


function ImportTable(t)
for k, v in pairs(t) do
_G[k] = v
end
end


Now call this function at the top of each lua file, like so:


ImportTable(v3)


Note: a lua script is executed when loaded, so order is significant. If you want, you can put ImportTable() in a separate lua module script (e.g. filename"utils.lua") and then write in your calling script:

require "utils"

Remember, though, your module script ("utils.lua") requires this at the top:

module("utils", package.seeall)

And if utils.lua is in a subdirectory relative to the calling script (e.g. "scripts\utils.lua"), then your require must do this:

require "scripts.utils"

Good day!

Posted on 2008-12-27 12:28:41 (last edited on 2008-12-27 12:29:39)


Displaying 1-3 of 3 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.