Hi all,
I'm just starting with Lua and have a very basic understanding of html and javascript. I know that loops exist and are essential, but I'm having a hard time conceptualizing what is going on.
Specifically, I'm currently going through this tutorial, https://github.com/kikito/love-tile-tut ... 1d-strings, on Github that is focusing on building a 2d map on love. The full source code for this section is here: https://github.com/kikito/love-tile-tut ... s/main.lua.
I'm guessing that this is pretty simple code for non-beginners, so I'm just going to ask a few questions about it instead of posting specific lines.
I understand what's going on in the top part of love.load(), but the bottom part (TileTable() and below) is very perplexing. So:
1.What is the best way to visualize/explain what the for loops are executing? I'm thinking in circles trying to understand the logic.
2.What is the code doing with the gmatch:("[\n]+") and gmatch:(".") commands? The tutorial briefly calls it pattern matching, but I have yet to figure out what exactly that means.
3.I think I understand the loops in love.draw(), but at the same time I don't think I really understand it. An explanation of those loops would be incredible.
If there are any other good resources for getting a better understanding of loops, etc. please feel free to share!
Thanks!
Help conceptualizing loops/pattern matching
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Help conceptualizing loops/pattern matching
First, welcome to the forums!
I can help with a bit of things.
2) Pattern matching is used to find a string inside a string, or better yet, a pattern in a string. It's relatively complicated at first, but once you get the hang of it, using pattern matching with gmatch or gsub is a really powerful tool for string manipulations.
3) Love.draw() is where you should put any code that draws things to the screen. Love.update() is where you put your game logic, which means updating variable values, doing calculations, checking if a music is playing, etc.
Love.load() is ran at game startup and should be used to load textures, music and any other files. This is only executed once.
Basically, you'll want to load your textures and audio in love.load, then calculate and do stuff in the love.update, and draw everything in love.draw. After that, love.update gets called once again, stuff is updated there, love.draw runs again drawing things, etc...
~le awesome drawnroutine
As for what does the gmatch function return i'm not sure, since i barely used iterators on Lua and i'm a negatory myself with pattern matching, so i can't really help out.
You may want to consider coding out a few simpler things, in order to better understand the callbacks (love.load, update and draw are actually called "callbacks") and how they are used/activated during runtime.
Correct me if i'm wrong, especially on the pattern matching section. I tend to provide innacurate info from time to time.
I can help with a bit of things.
2) Pattern matching is used to find a string inside a string, or better yet, a pattern in a string. It's relatively complicated at first, but once you get the hang of it, using pattern matching with gmatch or gsub is a really powerful tool for string manipulations.
Code: Select all
--Example: In windows, whilst opening a file, you can select files of an especific format:
*.txt, *.jpg, *.zip
--or simply any file of any given format
All files (*.*)
-- In this case, "*" means any characters before the dot "." and any characters after it.
-- Bear in mind though that lua pattern matching is different. A nice reference can be found here:
--http://lua-users.org/wiki/PatternsTutorial
Love.load() is ran at game startup and should be used to load textures, music and any other files. This is only executed once.
Basically, you'll want to load your textures and audio in love.load, then calculate and do stuff in the love.update, and draw everything in love.draw. After that, love.update gets called once again, stuff is updated there, love.draw runs again drawing things, etc...
Code: Select all
--The default run routine looks like this:
______<________<______<___
| |
\/ /\
Start -> love.load() -> love.update(dt) -> love.draw()--|
As for what does the gmatch function return i'm not sure, since i barely used iterators on Lua and i'm a negatory myself with pattern matching, so i can't really help out.
You may want to consider coding out a few simpler things, in order to better understand the callbacks (love.load, update and draw are actually called "callbacks") and how they are used/activated during runtime.
Correct me if i'm wrong, especially on the pattern matching section. I tend to provide innacurate info from time to time.
https://github.com/Sulunia
Re: Help conceptualizing loops/pattern matching
Hello and welcome to the forums.
I can't explain it very well but "string.gmatch(sz, pattern)" means: iterate all occurrences of "pattern" that are contained inside that string "sz". The pattern "." means any single character so "string.gmatch(sz, ".")" means: iterate each character in the string.
https://www.lua.org/pil/4.3.4.html
Pattern matching is a complicated concept, I would stay away from it if you're just starting out.2.What is the code doing with the gmatch:("[\n]+") and gmatch:(".") commands? The tutorial briefly calls it pattern matching, but I have yet to figure out what exactly that means.
I can't explain it very well but "string.gmatch(sz, pattern)" means: iterate all occurrences of "pattern" that are contained inside that string "sz". The pattern "." means any single character so "string.gmatch(sz, ".")" means: iterate each character in the string.
A loop is basically a piece of code that is executed a number of times. There are different types of loops. "for i = 1, 10 do" means repeat this block of code 10 times. "for c in string.gmatch(sz, '.') do" is a loop too, iterating over the characters in a string.1.What is the best way to visualize/explain what the for loops are executing? I'm thinking in circles trying to understand the logic.
I think the code examples you are looking at are a too complicated, for starters you may want to read the official Lua docs:3.I think I understand the loops in love.draw(), but at the same time I don't think I really understand it. An explanation of those loops would be incredible.
https://www.lua.org/pil/4.3.4.html
Re: Help conceptualizing loops/pattern matching
Patterns are wildcards on steroids, and that's an understatement. With regular wildcards, you can use * to match a part of a filename. For example, if you have the files "one", "two", three", using a pattern like "t*" will match files "two" and "three", while using a pattern like "*o*" will match files "one" and "two".
But with the usual wildcards *, ? that come from UNIX times, you're limited to matching "zero or more characters" (*) or "exactly 1 character" (?). Patterns extend the power for matching, giving you much more control of what to match. Lua patterns are a subset, with a slightly different syntax, of what's called "regular expressions", or shortly, "regexes". Lua lacks one fundamental operator of regexes, called alternation (which can be thought of as an OR operator: match one subpattern OR another subpattern), but it includes the rest of basic operators.
For example, by using pattern matching you can construct a pattern that only matches valid emails, rejecting those which have certain invalid characters. If you used wildcards, you'd be limited by something like "*@*.*" and the pattern would not be able to reject invalid characters (the set of invalid characters varies depending on whether it's before or after the "@") or invalid emails such as those starting with a ".". However, patterns allow you to specify the whole set of valid characters for each part, reject those that start or end with a ".", and in general, ensure it's actually valid.
Wildcards are just a small subset of patterns. They are not syntax-compatible. The ? in a wildcard is a . in a pattern, meaning any character, and the * in a wildcard is a .* in a pattern, meaning zero or more occurrences (*) of any character (.). But patterns allow you to apply the "zero or more occurrences" operator to anything: not just ".", but even complete subpatterns. For example, the pattern ^(ab)*$ means the start of the string (^), followed by zero or more occurrences of the string "ab", followed by the end of the string ($), therefore the empty string would match (zero occurrences), the string "ab" would also match (one occurrence), the string "abab" would match too (two occurrences) and so on, but no other string would match it, e.g. "aabb" would not match.
For more information, take a read at this: https://www.lua.org/pil/20.1.html and the next few sections. You'll see the power of patterns.
But with the usual wildcards *, ? that come from UNIX times, you're limited to matching "zero or more characters" (*) or "exactly 1 character" (?). Patterns extend the power for matching, giving you much more control of what to match. Lua patterns are a subset, with a slightly different syntax, of what's called "regular expressions", or shortly, "regexes". Lua lacks one fundamental operator of regexes, called alternation (which can be thought of as an OR operator: match one subpattern OR another subpattern), but it includes the rest of basic operators.
For example, by using pattern matching you can construct a pattern that only matches valid emails, rejecting those which have certain invalid characters. If you used wildcards, you'd be limited by something like "*@*.*" and the pattern would not be able to reject invalid characters (the set of invalid characters varies depending on whether it's before or after the "@") or invalid emails such as those starting with a ".". However, patterns allow you to specify the whole set of valid characters for each part, reject those that start or end with a ".", and in general, ensure it's actually valid.
Wildcards are just a small subset of patterns. They are not syntax-compatible. The ? in a wildcard is a . in a pattern, meaning any character, and the * in a wildcard is a .* in a pattern, meaning zero or more occurrences (*) of any character (.). But patterns allow you to apply the "zero or more occurrences" operator to anything: not just ".", but even complete subpatterns. For example, the pattern ^(ab)*$ means the start of the string (^), followed by zero or more occurrences of the string "ab", followed by the end of the string ($), therefore the empty string would match (zero occurrences), the string "ab" would also match (one occurrence), the string "abab" would match too (two occurrences) and so on, but no other string would match it, e.g. "aabb" would not match.
For more information, take a read at this: https://www.lua.org/pil/20.1.html and the next few sections. You'll see the power of patterns.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], slime and 9 guests