RigidChips
Would you like to react to this message? Create an account in a few clicks or log in to continue.

AAARGH!! Lua!!

5 posters

Go down

AAARGH!! Lua!! Empty AAARGH!! Lua!!

Post by Maurice Sun Apr 29, 2012 2:03 pm

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
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by RA2lover Sun Apr 29, 2012 6:17 pm

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

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
RA2lover
Walker
Walker

Posts : 382
Join date : 2010-10-11
Age : 29
Location : Brazil

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Mon Apr 30, 2012 2:54 pm

*BOOM*




You know what that was? That was my head exploding.
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Mon Apr 30, 2012 7:32 pm

Maurice wrote:*BOOM*




You know what that was? That was my head exploding.

Meaning you finally get it?

MrSparks
Tank
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Tue May 01, 2012 2:28 pm

No, meaning "IT'S JUST TOO MUCH!!!"
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Rainman Fri May 04, 2012 8:28 pm

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...
Rainman
Rainman
Tank
Tank

Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Sat May 05, 2012 12:29 am

^This guy, this guy right here... he gets it.
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by JosefYurnov99 Sun May 06, 2012 12:30 pm

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
JosefYurnov99
Plane
Plane

Posts : 43
Join date : 2011-03-11
Age : 28
Location : America

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Tue May 08, 2012 3:59 pm

Ok, I'll give a tutorial a shot.

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
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Rainman Tue May 08, 2012 4:38 pm

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 Very Happy
Rainman
Rainman
Tank
Tank

Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Tue May 08, 2012 5:52 pm

*tentatively* Yeah...
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Thu May 10, 2012 11:30 am

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:
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
And so forth.

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
But what if you want to do one thing if it's true and another if it's false? There's an else for that.
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
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Thu May 10, 2012 2:24 pm

Okay... I'm with you so far...
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Rainman Thu May 10, 2012 10:52 pm

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
Rainman
Tank
Tank

Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Thu May 17, 2012 2:04 pm

A while loop is the basic form of loop.
Code:
while {condition} do
  --do stuff until {condition} is false
end
It is important that you do not create an endless loop. Why? Because the script, and thus RC, will not continue until the loop is complete. So if the loop never completes then RC just hangs/freezes/crashes.

A for loop is a more sophisticated form of loop.
Code:
for counter = StartValue,EndValue,StepValue do
  --do stuff
end
You do not have to include StepValue. It defaults to 1 if you omit it.

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
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Thu May 17, 2012 7:26 pm

Alright, I got the first bit, but then you lost me.
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Thu May 17, 2012 9:29 pm

Maurice wrote:Alright, I got the first bit, but then you lost me.
Define "first bit"

MrSparks
Tank
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Rainman Thu May 17, 2012 9:55 pm

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
Rainman
Tank
Tank

Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Thu May 17, 2012 10:57 pm

Rainman wrote:Sparks: Did I understand what I read properly?
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: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.
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.

MrSparks
Tank
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Maurice Fri May 18, 2012 2:33 pm

Okay, I think I understand now.
Maurice
Maurice
Hover
Hover

Posts : 153
Join date : 2011-10-07
Age : 28
Location : The early 1970's

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Sat May 26, 2012 11:17 am

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.

MrSparks
Tank
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Sat Jun 02, 2012 1:43 pm

Before I cover arrays I will quickly demonstrate the use of one very important built-in function.
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.
This function is your friend when you need to debug/troubleshoot/diagnose and fix your misbehaving code.

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
Looks simple so far, yes? But that's only how you make an array, not how you work with one.
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"
And yes, you can use them with loops to great effect.
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
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Rainman Sat Jun 02, 2012 2:28 pm

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
Rainman
Tank
Tank

Posts : 80
Join date : 2011-02-14
Age : 31
Location : New England, US

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by RA2lover Sat Jun 02, 2012 3:33 pm

_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.
RA2lover
RA2lover
Walker
Walker

Posts : 382
Join date : 2010-10-11
Age : 29
Location : Brazil

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by MrSparks Sat Jun 02, 2012 5:28 pm

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
Tank

Posts : 73
Join date : 2012-01-05
Age : 33
Location : Michigan

Back to top Go down

AAARGH!! Lua!! Empty Re: AAARGH!! Lua!!

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum