[Learning LUA] Lesson 3: Values and Operators is up!

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
SpiderFighter
Posts: 789
Joined: Thu Apr 12, 2012 4:15 pm

Re: [LUA NEWBIES] Let's do this!!

Post by SpiderFighter »

Ryeath_Greystalk wrote:*raises hand*

I already have two questions on your lesson.

1. x = "you're ugly!";
print(x);

x = "you're ugly!"; print(x)

Does the semi colon serve a purpose here? I don't recall having used a semi colon in my travels yet.
The semicolon is (as far as I know) used to tidy up your code, and is completely optional. I've seen it used in quite a few scripts here, so it varies by programmer.

[EDIT] Ok, after doing a bit of research, here's the way I understand it (and PLEASE someone come in and save us if this is wrong): A locally defined variable is limited to the block in which it is declared. So, to use your examples:
Ryeath_Greystalk wrote: example:
script_a

x = 42 -- global
local y = 18 -- local to script_a

function meaningOfLife()
local z = 21 -- local to script_a.meaningOfLife()
a = 969 -- This would be global, I believe. But this is way above me atm, so I'm not positive (in fact, I changed it once already!)
print x -- prints 42 (global)
print y -- prints 18 (local to script_a)
print z -- prints 21 (local to script_a.meaningOfLife())
print a -- prints 969 (???? global or local)
end
See Local Variables and Blocks for more information. Sorry I can't be more help right now; I'm learning too. :)
Oh and put your hand down. You can shout out any questions; we're informal here. Just clean up if you eat, please.
User avatar
Drakkan
Posts: 1318
Joined: Mon Dec 31, 2012 12:25 am

Re: [LUA NEWBIES] Let's do this!!

Post by Drakkan »

this is excelent idea, really welcomed and thanks for your effort !
Breath from the unpromising waters.
Eye of the Atlantis
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: [LUA NEWBIES] Let's do this!!

Post by msyblade »

Weeeelllllllllll, my friend! This is just awesome. I'm gonna try to explain the true benefits of this thread idea.
A: It is going to be in chronological order, people wanting to learn, are going to get lessons in the order they were learned by you, from the floor up. This is as opposed to how most of us here learned, doing far too complex functions by copy/pasting, and trying to troubleshoot when it all looks like greek. Then learning the more basic principles later, and puking on yourself when you see how you butchered it earlier on.
B: The language. Dont get me wrong, in about 4 pages the language here is going to sound like advanced nano-physics (As each property is defined and analyzed), like most detailed scripting discussions. However, here it is going to progress from plain english, into script talk, gradually. All in the same thread! We can SEE the metamorphosis occur.
C: We are going to make mistakes (for instance, I'm not familiar with the ; use either.) But NOONE be discouraged by this, it will create a discussion back and forth, completely breaking down and analyzing what went wrong, and how it can be more easily understood. This will prove to be a valuable occurence when compared to the message that was 7 lines long with no responses. Some ppl might not "get" it on the first pass.
D: It has the potential to become the "Scripting questions? Apply here!" thread, which 70% of the modders here could make use of.
E: SpiderFighter, you may end up BEING that guy that answers the scripting questions here, rather than the one asking them. That is just a COOL prospect that will inspire those around you.
Anyway, thats my two cents :) WTFG Spidey! Great idea, and likely helpful to many, many people around here.

Guess I could throw in a handy scripting tip while I'm here then. (I'm not ALL psychobabble.)
This is more to start a discussion and dissect the use of a term. I see it frequently used incorrectly, and I too learned "Street Lua",so while I have my head wrapped around it, I am going to have a hard time explaining it. The term causing the trouble is "and".
The nature of the word "and" leads to it being used as a "combiner", rather than a "seperator". (neither term is technical, just my slang to try and help explain). for instance, if you want DIFFERENT things checked, "and" is used.
edit -(Put some duct tape and elmers glue on it, thanx wordsworth for the heads up!)
ie:
function countercheck()
if counter_1== 0
and --seperating the counters
counter_2== 0
then
door_1: open
end
However, if you are causing something to happen, and want it to do multiple things, and is not used to "combine" them.
ie:
function countercheck2()
if counter_1== 0
and
counter_2== 0
then
door_1: open --and is not used here to "combine" the effects
door_2: open --nor here
door_3: close
end

Hope that made sense to the everyman!
Again, as will be true for every post in this thread, please correct me if i'm wrong, or elaborate if you have insight!
Last edited by msyblade on Wed Apr 03, 2013 5:05 pm, edited 3 times in total.
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
User avatar
SpiderFighter
Posts: 789
Joined: Thu Apr 12, 2012 4:15 pm

Re: [LUA NEWBIES] Let's do this!!

Post by SpiderFighter »

Very kind words, guys. Thank you; I'm glad that this is going to be well received. I hope you don't mind msyblade, but I put a link to your post in the OP (under a new header called "Useful Tips (found in this thread)." I'll put other useful posts there as well. Let's make this THE thread for learning lua, tips, tricks, and (general) questions and answers!
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: [LUA NEWBIES] Let's do this!!

Post by Ryeath_Greystalk »

Thanks for that msyblade.

I had to read your post a couple times before I saw the point you were trying to make, which was not to use 'and' to combine the openDoor() commands at the end.

--correct
door_1:open()
door_2:open()
door_3:open()

--incorrect
door_1:open() and
door_2:open() and
door_3:open()

I don't want to get ahead of SpiderFighter, but logic functions is one area where a have a little bit of prior experience from wiring chips up on a breadboard back in high school.

(capitals for clarity, use lower case in script)

AND: All inputs must be true for the output to be true otherwise the output is false.
OR: If any of the inputs are true then the output is true. If all inputs are false then output is false.
NOT(inverter): Output is opposite of input. NOT true = false. NOT false = true

--as far as I know the following are not lua commands but can be simulated--
NAND: Inverted output of an AND. All inputs must be true for output to be false
NOR: Inverted output of an OR. If any input is true then output is false
XOR: If all inputs are the same(true or false) then output is false. If any two inputs are different then output is true.

Who wants to be the first to break out the boolean algebra? :twisted:
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: [LUA NEWBIES] Let's do this!!

Post by Grimfan »

Good idea Spiderfighter. There's a lot to lua so any thread like this is worthwhile. :)

I don't want to even touch boolean algebra! It makes my head spin! :shock:
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: [LUA NEWBIES] Let's do this!!

Post by JohnWordsworth »

Cool thread SpiderFighter - great idea.

Just a few notes...

1. @msyblade: I think you made a typo and might want to change = to == in your example, 'if counter_1 = 0' will throw an error I believe (or will be even more confusing if it actually assigns 0, but I think Lau spots this error). For tutorial purposes / newcomers, common comparisons you can do in an if statement are...

== True if two values are EXACTLY the same. So, a == b is true if a = 1 and b = 1, but not if a = 1 and b = 1.001.
~= True if two values are different. The opposite of ==. So, a ~= b is true if a = 1 and b = 200.
> True if the left value is greater (bigger) than the right value. So, a > b if a = 100 and b = 50.
< True if the left value is less (smaller) than the right value. So, a < b if a = 50 and b = 100.
>= True if the left value is greater than the right value, OR they are the same. So, a >= b if a = 10, b = 10.
<= True if the left value is less than the right value, the opposite of >=.

2. Just to clarify @SpiderFighter, you are correct in that a variable declared in the middle of, well - anywhere, WITHOUT the prefix local, means that the variable is global and will stick around forever. So,

Code: Select all

function prepareGlobals() 
  x = 1;
end

prepareGlobals();
print(x); --Will print out the number 1, as set in prepare globals.
I think this has two important implications...

a. Just remember that global variables can be changed anywhere. So if you set a variable x in a function and then call another function that changes x, you might not realise where it has been changed. This means that you really are better off using local variables everywhere that you can, except for the one or two places you want a global variable.

b. Another good reason for using local variables wherever possible is that the global variables will hang around forever. So, they are constantly taking up memory and sitting in the save game. Don't get me wrong - a few numbers here and there don't matter, but long text strings and tables that are only used once don't need to sit in memory forever.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
SpiderFighter
Posts: 789
Joined: Thu Apr 12, 2012 4:15 pm

Re: [LUA NEWBIES] Let's do this!!

Post by SpiderFighter »

Thanks to you guys for adding to the conversation and knowledge base. I just wanted to pop in and say there will be more lessons coming on Monday; it's just that weekends are busy for me. Cheers!
User avatar
Neikun
Posts: 2457
Joined: Thu Sep 13, 2012 1:06 pm
Location: New Brunswick, Canada
Contact:

Re: [LUA NEWBIES] Let's do this!!

Post by Neikun »

Duh.. Who goes to class on weekends?
"I'm okay with being referred to as a goddess."
Community Model Request Thread
See what I'm working on right now: Neikun's Workshop
Lead Coordinator for Legends of the Northern Realms Project
  • Message me to join in!
Alcator
Posts: 37
Joined: Fri Apr 13, 2012 9:59 am

Correction to "Case sensitive language" bit.

Post by Alcator »

3. Lua is case-sensitive. The word Goromorg is not the same as goromorg. Words reserved for use within lua can be used as variables simply by changing the case (Then instead of then, for example).
All right, while the paragraph is technically correct, it also represents a programming sin so great that it has to be exorcised immediately:

About variables
Variables are basically memory spaces that can be referred to by their name and can hold values. They are useful if your program needs to keep some information, manipulate it, recall it later, or even make decisions (= take different paths) based on that information.

As such, few things to remember:
  1. Give your variables meaningful names. A variable named "Then" might only be acceptable if you intended to store some past time moment or past situation, but a much better (because "much clearer") name would be "originalName", "previousManaCost" or "spidersBeforeKillingCount"
  2. Follow one "naming convention" in all your code, if possible. It does not matter if your naming convention is camelCase (first word in all lowercase, then capitalize first letter of each next word, avoid "a"), if it's underscoring_style (using underscore as separator of words) or ThisStyleWhichIDontRememberTheNameOf ; what matters is that you stick to it. Sticking to one naming convention makes your code much more readable to anyone else.
  3. It is beneficial if the name of the variable indicates what kind of values is/are stored inside:
    • storeHeroName -- the "Name" bit indicates that the likely value is a String (a sequence of characters (character = "1 hit on a keyboard")).
    • killedSpiderCount -- the "Count" not only indicates that it's a number, it even says that it will be Whole number.
    • isAwake -- the fact that the name is in a form of a question that can be answered with yes/no indicates that this is a BOOL variable (it holds "true" or "false").
    • tableTorchFuel -- the presence of "table" indicates that the variable is an array (table) and holds multiple values.
Post Reply