AAARGH!! Lua!!
5 posters
RigidChips :: Rigid-Chips :: Tutorials :: Request a Tutorial
Page 1 of 1
AAARGH!! Lua!!
I have read and re-read JHaskly's Lua tutorial and it still doesn't make sense to me. Can anyone explain to me how it works and how to use it? I need to know so I can perfect my Space Cruiser.
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
in order to have a lua script to run in rigidchips, you need to declare functions which the program runs - you need either a main() function(ran on init, reset and every frame or a set of OnInit, OnReset and OnFrame(when they get called is rather obvious) functions. most scripters use only main here.
they can be declared like this
other uses for functions include shortening code when its used alot, or making it look cleaner when its used only once in a cycle. these functions can output values using return.
model-side variables and built-in functions are always uppercase only, instead of lowercase(notable exceptions are out(RCScript's print) and _VEL(chip), which works on RCScript but not on RCLua.
anyway, here's some easy-to-learn but important stuff for code.
if statements and for loops.
if statements enable players to set branches.
they involve a condition which sets them.
most used operators are ==(equal to), >(larger than),<(obvious), >=(equal or larger), <=(equal or smaller), and ~=(not equal to)
you can also have uses for and, or and not, but these get mostly used only for boolean logic.
for loops enable you to spam stuff in a faster-to-code way.
for loops can use 2 values(start and end), where step will default to 1, 3 values(start, end and step) or an iterator function.
they can be declared like this
- Code:
function functionnamehere(argumentshere,separatedbycommas)
--insert lua code here(-- is the equivalent of a comment on other languages. stuff after it until the end of the line doesnt run
end
other uses for functions include shortening code when its used alot, or making it look cleaner when its used only once in a cycle. these functions can output values using return.
- Code:
--unoptimized stuff
asdf=372819
var1 = 3^2+3*2+4
var2 = 9^2+2*2+4
var3 = 4^2+1*2+4
var4 = 9^2+9*2+4
var5 = 14^2+14*2+4
var6 = asdf^2+asdf*2+4
- Code:
--somewhat better
function qwerty(a,b,c)
return a^2+b*2+c
end
var1 = qwerty(3,3,4)
var2 = qwerty(9,2,4)
var3 = qwerty(4,1,4)
var4 = qwerty(9,9,4)
var5 = qwerty(14,14,4)
var6 = qwerty(asdf,asdf,4)
model-side variables and built-in functions are always uppercase only, instead of lowercase(notable exceptions are out(RCScript's print) and _VEL(chip), which works on RCScript but not on RCLua.
anyway, here's some easy-to-learn but important stuff for code.
if statements and for loops.
if statements enable players to set branches.
they involve a condition which sets them.
most used operators are ==(equal to), >(larger than),<(obvious), >=(equal or larger), <=(equal or smaller), and ~=(not equal to)
you can also have uses for and, or and not, but these get mostly used only for boolean logic.
- Code:
function IsOver9000(number)
if number>9000 then
out(0,number.." is over 9000") --here .. concatenates stuff together into a string.
elseif number>8000 then --if the statement is false, rest goes here with another one
out(0,number.. " is over 8000. not as epic as over 9000 though.")
else--useful for when you have enough logic not to need another condition.
out(0,number.." is too small to be epic")
end
for loops enable you to spam stuff in a faster-to-code way.
- Code:
--fail
out(0,"this is line 0 of model's output")
out(1,"this is line 1 of model's output")
out(2,"this is line 2 of model's output")
out(3,"this is line 3 of model's output")
out(4,"this is line 4 of model's output")
out(5,"this is line 5 of model's output")
out(6,"this is line 6 of model's output")
out(7,"this is line 7 of model's output")
out(8,"this is line 8 of model's output")
out(9,"this is line 9 of model's output")
out(10,"this is line 10 of model's output")
- Code:
for n=0,10 do
out(n,"this is line "..n.." of model's output")
end
for loops can use 2 values(start and end), where step will default to 1, 3 values(start, end and step) or an iterator function.
- Code:
table = {"asdf",239993, "hi", "iliketrains"}
for k,v in pairs(table) do
out(k, "table's value for element "..k.." is "..v)
end
RA2lover- Walker
- Posts : 382
Join date : 2010-10-11
Age : 29
Location : Brazil
Re: AAARGH!! Lua!!
*BOOM*
You know what that was? That was my head exploding.
You know what that was? That was my head exploding.
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
Maurice wrote:*BOOM*
You know what that was? That was my head exploding.
Meaning you finally get it?
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
No, meaning "IT'S JUST TOO MUCH!!!"
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
I'm with Maurice here. I've seen a lot of exchanges that go like the following:
A: "Hey, I'm a beginner/novice programmer and I want to learn more about Lua, can you help me out?"
B: "Sure! Just text wall text wall jargon text wall jargon jargon complicated explanation using notation A likely isn't familiar with jargon jargon text wall text."
A: " O_O "
B: "So yeah, see? it's simple enough once you know it."
A, thinking to self: Yeah, but I don't know it yet, that was the point...
A: "Hey, I'm a beginner/novice programmer and I want to learn more about Lua, can you help me out?"
B: "Sure! Just text wall text wall jargon text wall jargon jargon complicated explanation using notation A likely isn't familiar with jargon jargon text wall text."
A: " O_O "
B: "So yeah, see? it's simple enough once you know it."
A, thinking to self: Yeah, but I don't know it yet, that was the point...
Rainman- Tank
- Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US
Re: AAARGH!! Lua!!
^This guy, this guy right here... he gets it.
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
I'm with you guys, every explanation I have recieved has been far too complicated to be of any use to me. I sat down one day to make a Lua Heads up Display. I found a post on exactly that, however upon reading through it, I was just as confused, if not more than when I started. I'm getting pretty good at using script functions, but there are things that script can't do, so I would really enjoy a simplified lesson in Lua. A "Lua for dummies" so to speak.
JosefYurnov99- Plane
- Posts : 43
Join date : 2011-03-11
Age : 28
Location : America
Re: AAARGH!! Lua!!
Ok, I'll give a tutorial a shot.
Comments{text that is not run} work thusly:
There are 4 primary functions; OnInit(), OnReset(), OnFrame(), and Main().
Usually you just need Main(), not the On* functions
However, in some circumstances, you need to run a set of instructions only when the model is first loaded or only when it is respawned.
With me so far?
Comments{text that is not run} work thusly:
- Code:
-- this is a comment
this is code that is run -- this is a comment on the same line as code
There are 4 primary functions; OnInit(), OnReset(), OnFrame(), and Main().
Usually you just need Main(), not the On* functions
- Code:
function Main()
--all of your code
end
However, in some circumstances, you need to run a set of instructions only when the model is first loaded or only when it is respawned.
- Code:
function OnInit()
--code that is run only when the model is loaded
end
function OnReset()
--code that is run only when the model is respawned
end
function OnFrame()
--code that is run every frame other than the frame the model is loaded/respawned
end
With me so far?
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
Yup! You've already delivered more useful, intelligible information than the other Lua 'explanations' I've read combined. Can't wait for the next installment
Rainman- Tank
- Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US
Re: AAARGH!! Lua!!
*tentatively* Yeah...
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
Rainman, I hope you're exaggerating.
In this section I tried for the same level of overclarity, but it came out with a bit less organization
The majority of Lua is assignment. Assignment works thusly:
Lua supports all of your basic mathematical operations, following the standard Order of Operations.
(pemdas/Please Excuse My Dear Aunt Sally/Please Email My Dad A Shark/People Expect More Drugs And Sex)
() parenthesis
^ exponents
* multiplication
/ division
+ addition
- subtraction
Now, for the fun part: Boolean Logic and Decisions
In Boolean Logic, a statement is either true or false. There is no maybe, sometimes, or "I dunno".
The logical comparison operators are:
== equal
~= not equal
< less than
> greater than
<= less than or equal {not greater than}
>= greater than or equal {not less than}
The simplest of the decisions is the if-then
And if you have more cases than just 2? elseif is there for you.
If you need clarification on anything so far then speak up because otherwise the next installment is loops and concatenation.
In this section I tried for the same level of overclarity, but it came out with a bit less organization
The majority of Lua is assignment. Assignment works thusly:
- Code:
FOO = 1 --sets the value of a variable named "FOO" to 1
FOO = BAR -- sets the value of FOO to the value of BAR
FOO = "BAR" -- sets to value of FOO to "BAR" {A string}
FOO = BAR + 2 --sets the value of FOO to the sum of BAR and 2
Lua supports all of your basic mathematical operations, following the standard Order of Operations.
(pemdas/Please Excuse My Dear Aunt Sally/Please Email My Dad A Shark/People Expect More Drugs And Sex)
() parenthesis
^ exponents
* multiplication
/ division
+ addition
- subtraction
Now, for the fun part: Boolean Logic and Decisions
In Boolean Logic, a statement is either true or false. There is no maybe, sometimes, or "I dunno".
The logical comparison operators are:
== equal
~= not equal
< less than
> greater than
<= less than or equal {not greater than}
>= greater than or equal {not less than}
The simplest of the decisions is the if-then
- Code:
if FOO == BAR then
--do things if FOO equals BAR
end
- Code:
if FOO == BAR then
--do stuff if they're equal
else
--do other stuff if they're not equal
end
And if you have more cases than just 2? elseif is there for you.
- Code:
if FOO > BAR then
--do stuff if FOO is greater than BAR
elseif FOO < BAR then
--do other stuff if FOO is less than BAR
else
--do stuff if none of the above are true, which in this case is if FOO equals BAR
end
If you need clarification on anything so far then speak up because otherwise the next installment is loops and concatenation.
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
Okay... I'm with you so far...
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
Yup, definitely staying with you. I had lots of Boolean logic in one of my uni courses last semester, so I implicitly understood that part, which only made the rest easier to follow.
Rainman- Tank
- Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US
Re: AAARGH!! Lua!!
A while loop is the basic form of loop.
A for loop is a more sophisticated form of loop.
Typically a for loop is used with concatenation, especially in chainguns/spamcannons
Still with me? We're almost to the end.
- Code:
while {condition} do
--do stuff until {condition} is false
end
A for loop is a more sophisticated form of loop.
- Code:
for counter = StartValue,EndValue,StepValue do
--do stuff
end
Typically a for loop is used with concatenation, especially in chainguns/spamcannons
- Code:
for count = 1,6 do
_G["GUNANGLE"..count] = _G["GUNANGLE"..count]+5
end
Still with me? We're almost to the end.
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
Alright, I got the first bit, but then you lost me.
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
Define "first bit"Maurice wrote:Alright, I got the first bit, but then you lost me.
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
Maurice wrote:Alright, I got the first bit, but then you lost me.
I think I got this one.
The first bit, the "while" loop, is the simplest kind of loop. It tells the computer, "While {Something I tell the computer to look for} is happening, do [A pile of stuff I want to happen then] until {Something I tell the computer to look for} isn't happening anymore."
It's the most straightforward, but if you accidentally write the code so that there isn't something to make it stop, it never stops and the game freezes/crashes. So to avoid that, a programmer often wants to use more advanced codes like the next part, "for" loops.
If I understand it right, "For" causes [stuff you want to happen at a certain point] to happen during a specific interval of values on a "control" variable. You write two or three values in the {}; the value you want the [stuff happening] to start at, the value you want the [stuff happening] to end at. The third number helps the computer create a bunch of numbers in between the start value you picked and the end value you picked; each one increases from the value before it by the third number you just chose. If you don't bother to write in the third number, the computer acts as if the third number = 1 (So 1,6,1 counts 1, 2, 3, 4, 5, 6 *DONE*, while 1,6, 2 counts 1, 3, 5, 6 *DONE*)
This will be spiffy and useful for stuff like chainguns because it means instead of having to laboriously script and copy-paste umpteen lines of code so that gun 1 be in position A at time 1, then position b at time 2, yadda yadda, all the way up to position J at time 10, you can use the For loop to cause it to constantly be increasing its position from A to J over time 1 to time 10, all in one small block of code.
Maurice: Did I clear it up any?
Sparks: Did I understand what I read properly?
Rainman- Tank
- Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US
Re: AAARGH!! Lua!!
For the most part, I think so, I think you're a tiny bit off on the implementation of the for loop, though. Your last example would be 1,3,5. It wouldn't do 6 because 5+2=7 and seven is outside the range.Rainman wrote:Sparks: Did I understand what I read properly?
Not quite. The code reduction in the example wouldn't be eliminating separate lines for positions A to J, it'd be for eliminating separate blocks of code for barrels 1 to 6 in a Gatling/rotary gun. Positions, like for a gun where the arm chips are stored in a box and flipped up to fire, would require using arrays. Which I haven't covered yet and actually forgot about until you brought up a situation where they'd be the perfect tool. Arrays are actually quite useful and I don't use them as often as I probably should. Which is probably why they slipped my mind. Blocks of text like this are why I've taken such a long time between posts in the tutorial proper.Rainman wrote:This will be spiffy and useful for stuff like chainguns because it means instead of having to laboriously script and copy-paste umpteen lines of code so that gun 1 be in position A at time 1, then position b at time 2, yadda yadda, all the way up to position J at time 10, you can use the For loop to cause it to constantly be increasing its position from A to J over time 1 to time 10, all in one small block of code.
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
Okay, I think I understand now.
Maurice- Hover
- Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's
Re: AAARGH!! Lua!!
Unfortunately, I'm not going to be able to post a chapter on arrays for awhile. Between complications with dad's estate, garage sale season, and finding unexpected illogical behavior in my array example, It's going to be awhile before I have a simplified explanation that I'm sure doesn't miss something important.
For now, look over the documentation and tell me if there's any functions you'd like me to explain. I don't know how to properly use most of the vector ones other than the sets of _X, _EX, and _VX, so keep that in mind. I'd love to see Chris220 or DanielLC or somebody else who understands the vector functions do a tutorial like this for them.
For now, look over the documentation and tell me if there's any functions you'd like me to explain. I don't know how to properly use most of the vector ones other than the sets of _X, _EX, and _VX, so keep that in mind. I'd love to see Chris220 or DanielLC or somebody else who understands the vector functions do a tutorial like this for them.
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
Before I cover arrays I will quickly demonstrate the use of one very important built-in function.
RCLua arrays are magical faeries that do magical things that don't always make sense.
The elements of an array are indexed, that is to say identified, by number. Most programming languages index arrays starting at 0. But not Lua. Lua starts at 1. Never try to print an array like you would a variable. it will crash RC.
Recursion is possible.
Arrays and _G[] with usable RCLua
A couple notes on this code:
It's not the best way to do what it does(drawing lines along the path of the wheels), but it allows me to demonstrate some of the odd behaviors of arrays+_G[] fairly well.
When concatenating to the name of an array, the index must be outside _G[].
When using an array to store chipnames, you have to use _G[] to reference the chipname.
_X(), _Y(), and _Z() return X, Y, and Z coordinates of a chip respectively
_SETCOLOR(n) sets the drawing color to n.
_MOVE3D(x,y,z) moves the drawing point around in RC world.
_LINE3D(x,y,z) draws a line from the previous _MOVE3D() or _LINE3D() to the specified point in RC world.
math.mod(x,y) returns the remainder(modulo) of x/y.
math.random(n) returns a random number between 0 and n inclusive
Did you understand all that? If not, what needs clearing up?
- Code:
out(0,"blah") --prints the word "blah" on line 0, which is the top line of the display
out(24,foo)--prints the contents of the foo variable on line 24,the bottom line of the display out() can write on.
RCLua arrays are magical faeries that do magical things that don't always make sense.
- Code:
foo = {} --creates an empty array
bar = {5,"red"} --creates an array with 2 elements
The elements of an array are indexed, that is to say identified, by number. Most programming languages index arrays starting at 0. But not Lua. Lua starts at 1. Never try to print an array like you would a variable. it will crash RC.
- Code:
foo = {"one","two","three","four","five","six","seven","eight","nine","ten"}
out(1,foo[1])--outputs the first element of foo, which is "one", to line 1
out(2,foo[2])--outputs "two", the second element of foo, to line 2
foo[3]="drei"-- changes "three",the third element of foo, to "drei"
- Code:
foo = {"one","two","three","four","five","six","seven","eight","nine","ten"}
for count=1,10 do
out(count,foo[count])
end
Recursion is possible.
- Code:
foo={"grey"}
bar={foo,"red"}--you can put an array in another array. Could be useful in some situations.
out(1,bar[1][1])--prints "grey" on line 1
fubar={32,45}
fubar[3]=fubar --you can put an array in itself. I don't know why you'd want to, but you can.
out(1,fubar[3][3][2])--prints 45 on line 1 regardless of how many [3]s deep you go.
Arrays and _G[] with usable RCLua
A couple notes on this code:
It's not the best way to do what it does(drawing lines along the path of the wheels), but it allows me to demonstrate some of the odd behaviors of arrays+_G[] fairly well.
When concatenating to the name of an array, the index must be outside _G[].
When using an array to store chipnames, you have to use _G[] to reference the chipname.
_X(), _Y(), and _Z() return X, Y, and Z coordinates of a chip respectively
_SETCOLOR(n) sets the drawing color to n.
_MOVE3D(x,y,z) moves the drawing point around in RC world.
_LINE3D(x,y,z) draws a line from the previous _MOVE3D() or _LINE3D() to the specified point in RC world.
math.mod(x,y) returns the remainder(modulo) of x/y.
math.random(n) returns a random number between 0 and n inclusive
- Code:
function init()
time=1
wtbl={"FRWL","FLWL","RLWL","RRWL"}--creates an array containing the chipnames of the wheels of a car
for whl=1,4 do
_G[wtbl[whl].."tbx"]={}--creates FRWLtbx, FLWLtbx, etc.
_G[wtbl[whl].."tby"]={}
_G[wtbl[whl].."tbz"]={}
end
end
function OnReset()
init()
end
function OnInit()
init()
end
function main()
for w=1,4 do
_G[wtbl[w].."tbx"][time]=_X(_G[wtbl[w]])--stores X coordinates of wheels in tables.
_G[wtbl[w].."tby"][time]=_Y(_G[wtbl[w]]) --same for y
_G[wtbl[w].."tbz"][time]=_Z(_G[wtbl[w]]) --same for z
end
time=math.mod(time,99)+1
for w=1,4 do
_MOVE3D( --broken up for clarity
_G[wtbl[w].."tbx"][time],
_G[wtbl[w].."tby"][time],
_G[wtbl[w].."tbz"][time]
)
for i=0,97 do
mark=math.mod(time+i,99)+1
_SETCOLOR(math.random(16777215))--16777215 in decimal == FFFFFF in hexadecimal
_LINE3D(--same
_G[wtbl[w].."tbx"][mark],
_G[wtbl[w].."tby"][mark],
_G[wtbl[w].."tbz"][mark]
)
end
end
end
Did you understand all that? If not, what needs clearing up?
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
Re: AAARGH!! Lua!!
I 'm pretty sure I follow the all algorithm logic and the codes you just laid out. The only unknown for me is the function _G[], which I don't recall seeing any of the umpteen times I've read through the Rigidchips' English Documentation thread. I don't really have an idea of what it is and what it does.
Rainman- Tank
- Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US
Re: AAARGH!! Lua!!
_G is a table, not a function. it contains EVERYTHING!!!111eleven - although its mostly used to get variables using dynamic names.
say you have a model with a variable asdf.
_G["asdf"] will make you able to read(and write to) it.
use concatenation and you'll be able to set multiple variables easily without repeating all the code with an if-elseif-elseif-... massacre.
it's what virtually all japanese models use to manage their guns.
say you have a model with a variable asdf.
_G["asdf"] will make you able to read(and write to) it.
use concatenation and you'll be able to set multiple variables easily without repeating all the code with an if-elseif-elseif-... massacre.
it's what virtually all japanese models use to manage their guns.
RA2lover- Walker
- Posts : 382
Join date : 2010-10-11
Age : 29
Location : Brazil
Re: AAARGH!! Lua!!
Rainman wrote:I 'm pretty sure I follow the all algorithm logic and the codes you just laid out. The only unknown for me is the function _G[], which I don't recall seeing any of the umpteen times I've read through the Rigidchips' English Documentation thread. I don't really have an idea of what it is and what it does.
That's because it isn't in the Rigidchips English Documentation. I thought I explained it well enough in the snippet at the end of the section on looping, but I guess not.
RA2lover, I don't know if you're correct, but you might be so I won't say you're wrong. Your explanation is not the same as my understanding of _G[] but they are compatible. Infact, the more I think about it, the more logical it appears. So yes, I say you are right and you just taught me something I'm surprised I didn't figure out myself. Thankyou.
MrSparks- Tank
- Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan
RigidChips :: Rigid-Chips :: Tutorials :: Request a Tutorial
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum