Thread Tools Display Modes
01-21-12, 03:38 PM   #1
avmezaiii
A Defias Bandit
Join Date: Jan 2012
Posts: 3
Question lua

i was searching the net for a lua forum, and i found wowinterfaces forums for wow addons, which is exactly the topic of the book i'm reading. i was wondering if someone with experience in making addons could explain a thing or two and if i get a response i'll post the examples, they are short and concise but confusing nonetheless. thanks in advance.
  Reply With Quote
01-21-12, 03:50 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
It's generally easier if you start out with a description of what you're doing or what you're trying to learn or any specific questions you may have. There are plenty of people here who may just have the answers you need.

We're just not entirely sure what you want explained.

(PS- I moved your thread to the developer section of our forums. Welcome to WoWInterface!)
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-21-12, 05:22 PM   #3
avmezaiii
A Defias Bandit
Join Date: Jan 2012
Posts: 3
thanks

ok great. well like i said i just started reading about lua, and i'm confused with long strings and also blocks.

the example for a long string was [ [ "this is a long string and I can include 'and"] ]. Then second an example was given as [ ==["This is a long string and I can include 'and"]==]. Does this mean that == precedes [[ as A and a?

The second confusion with blocks is why a local variable assigned as do i=10 then do i=15 doesn't bind a new value to i as it would without do.
  Reply With Quote
01-21-12, 06:27 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I generally just use [[string]] for long strings. Since You are using the double-brackes to denote it, and not quotes, you can use quotes in your string without the parser getting confused. I am fairly sure that [==[string]==] is just another way of writing it. Two equal signs (==) are used for comparison normally, not assignment.

Code:
var = 1
print(var == 1)
> true
print(var == 2)
> false

Local variables are only accessible within wherever they are defined - this is called "scope". Here is the section of Programming in Lua about local variables and scope: http://www.lua.org/pil/4.2.html So you can do something like this:
Code:
function()
     local myVar = "apple"
     if myVar == "apple" then
          local myVar = "orange"   --this says to keep our new myVar value local to this if-block
          print(myVar)   --this would print out "orange"
     end
     print(myVar)   --this would still print out "apple" since it is now outside of the scope where myVar was changed to "orange"
end
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-21-12, 06:38 PM   #5
avmezaiii
A Defias Bandit
Join Date: Jan 2012
Posts: 3
thanks a bunch

i guess i just complicated it a bit. but that's about what i got out of it, but you have to admit that the immutable aspect of Lua makes the local variables and scope a little awkward, at least to me it did. thanks a bunch, i can finish my notes on the chapter now.
  Reply With Quote
01-22-12, 03:27 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
The way Lua reads the long string notation is you can have your starting and ending brackets, [[ and ]] respectively contain any number of equal signs in them as long as both of them contain the same number of such. For example, all of these would produce the same result.

Code:
local var = "string";
local var = [[string]];
local var = [=[string]=];
local var = [==[string]==];
local var = [===[string]===];
... and so on. The purpose of this would be to enable the programmer to choose a delimiter in case one of them are included in the string. This is more common when used to modify the comment symbol -- into a long comment, which can span multiple lines.
Code:
-- This is a normal comment. This can only be one line.

--[[ This is a long comment.
It spans multiple lines. ]]


In more advanced techniques, you can use long comments inside a line, although this is not recommended.
Code:
local var = --[[some random comment]] "string";
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-22-12, 09:54 AM   #7
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Originally Posted by SDPhantom View Post

In more advanced techniques, you can use long comments inside a line, although this is not recommended.
Code:
local var = --[[some random comment]] "string";
Why is this not recommended? I have noticed that at least when it comes to WOW that certain methods of commenting cause errors, or rather my experience has been that it errors sometimes when using excessive commenting... Main place i see it happen is if i attempt to "long" comment out a function so that i do not have to go to every line and add -- it will give me an error about depreciation. Is there a way to comment out many lines without using -- on each line and not have errors?
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
01-22-12, 10:58 AM   #8
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Not a reply to the last post but the list of changes to Lua 5.1 has this snippet
The long string/long comment syntax ([[string]]) does not allow nesting.
You can use the new syntax ([=[string]=]) in these cases.
So the 2 are not exactly equivalent and I think it also replies Grimsin's question indirectly.

Inside your long comment there was probably another comment and the --[[]] notation doesn't allow nesting in Lua 5.1 (that's what WoW is running now right?)
  Reply With Quote
01-22-12, 01:08 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Yes. This is why it errored, and WoW is on 5.1.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-22-12, 01:26 PM   #10
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by Grimsin View Post
Why is this not recommended?
It makes the code less readable when the line is excessively segmented. For your sake and the sake of anyone who wishes to learn from your code, you should make comments either on the lines preceding the one in question or at the end of the line. Here are examples of recommended comment positions.
Code:
-- Comment
local var=value;
Code:
local var=value;-- Comment




Originally Posted by Grimsin View Post
I have noticed that at least when it comes to WOW that certain methods of commenting cause errors, or rather my experience has been that it errors sometimes when using excessive commenting... Main place i see it happen is if i attempt to "long" comment out a function so that i do not have to go to every line and add -- it will give me an error about depreciation. Is there a way to comment out many lines without using -- on each line and not have errors?
You need to be aware of what the text you're commenting contains. The following would cause an error.
Code:
--[[
local var=table1[table2[key]]+someothervar;
]]
Notice the line commented out contains the end delimiter and Lua will trip over this. You should use this instead.
Code:
--[=[
local var=table1[table2[key]]+someothervar;
]=]
This commonly happens when commenting out entire blocks of code that either have the same problem or already contain long comments already delimited by --[[ and ]].
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » lua


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off