WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Tutorials & Other Helpful Info. (https://www.wowinterface.com/forums/forumdisplay.php?f=12)
-   -   Creating a Development Tutorial (https://www.wowinterface.com/forums/showthread.php?t=56494)

candrid 08-03-18 12:36 PM

Creating a Development Tutorial
 
Hey Friends!

So many people help me understand what some of the development things mean. I decided I would heavily comment code and try to explain what I am doing/meaning but I did not want to stop there. I am going to make a tutorial on creation. Below is some example code, showing this idea, anything you want to see?

Code:

local addonName, addonData = ... -- This line means you are setting two local variables, addonName and addonData to equal multiple things.

-- Configuration
local camSpeed = 0.4  -- The speed of the rotation, higher is faster
local returnView = true -- This makes the camera return to its original position when you return from AFK.

local isAFK = false -- When this is default false the player is active, we use the trigger to switch it on.
local smoothCamera -- Creates the Variable "smoothCamera."

-- Functions (the things the addon does)
local function beginMovement (speed)
        smoothCamera = GetCVar("cameraSmoothStyle") -- the CVar "cameraSmoothStyle" determines the style of the camera.
        if (returnView) then -- An if statement is like a true and false, a yes or no.
                SaveView(5)
        end -- Ends the if statement.


Please feel free to correct me in errors.

Thanks in advance!

myrroddin 08-03-18 01:21 PM

There is lots of good stuff on Wowpedia/Gamepedia: https://wow.gamepedia.com/Wowpedia:I..._customization

myrroddin 08-03-18 01:27 PM

Also, your definition of "Global variable" is wrong.
Lua Code:
  1. someVar -- global variable
  2. local someVar -- local variable to file scope
  3. function MyFunc() -- global function
  4. local function MyFunc() -- local function to file scope
  5.  
  6. -- addonName is the string name of your addon, like "BigWigs" or "My Awesome Addon"
  7. -- addonData is the private table passed to each Lua file within the same addon. useful for things like localization through multiple Lua files
  8. -- ... is a variable argument, or vararg
  9. local addonName, addonData = ...

candrid 08-03-18 03:00 PM

Quote:

Originally Posted by myrroddin (Post 329289)
Also, your definition of "Global variable" is wrong.
Lua Code:
  1. someVar -- global variable
  2. local someVar -- local variable to file scope
  3. function MyFunc() -- global function
  4. local function MyFunc() -- local function to file scope
  5.  
  6. -- addonName is the string name of your addon, like "BigWigs" or "My Awesome Addon"
  7. -- addonData is the private table passed to each Lua file within the same addon. useful for things like localization through multiple Lua files
  8. -- ... is a variable argument, or vararg
  9. local addonName, addonData = ...

Awesome thanks!

MunkDev 08-03-18 03:23 PM

Quote:

Originally Posted by candrid (Post 329287)
If statements must end on the same "line" they start per beginning. Example here. They line up vertically. That is a "Block."

Lua Code:
  1. -- ...
  2.     if (returnView) then -- An if statement is like a true and false, a yes or no.
  3.         SaveView(5)
  4.     end -- If statements must end on the same "line" they start per beginning. Example here. They line up vertically. That is a "Block."

No, they don't. end means it's the end of the scope, but Lua is not sensitive to indentation. You can just as easily write your entire addon on one line and it would still work, just be a clusterfuck to read and understand.

Lua Code:
  1. if (returnView) then SaveView(5) end

candrid 08-03-18 03:43 PM

Quote:

Originally Posted by MunkDev (Post 329292)
Lua Code:
  1. -- ...
  2.     if (returnView) then -- An if statement is like a true and false, a yes or no.
  3.         SaveView(5)
  4.     end -- If statements must end on the same "line" they start per beginning. Example here. They line up vertically. That is a "Block."

No, they don't. end means it's the end of the scope, but Lua is not sensitive to indentation. You can just as easily write your entire addon on one line and it would still work, just be a clusterfuck to read and understand.

Lua Code:
  1. if (returnView) then SaveView(5) end

Thank you! I did not know that.

Seerah 08-03-18 03:58 PM

You forgot to close your function with an end.

myrroddin already explained to you what the variables in your vararg portion will be. I just wanted to give you reference on what a vararg is. https://www.lua.org/pil/5.2.html

Kanegasi 08-03-18 06:07 PM

Quote:

Originally Posted by MunkDev (Post 329292)
Lua Code:
  1. -- ...
  2.     if (returnView) then -- An if statement is like a true and false, a yes or no.
  3.         SaveView(5)
  4.     end -- If statements must end on the same "line" they start per beginning. Example here. They line up vertically. That is a "Block."

No, they don't. end means it's the end of the scope, but Lua is not sensitive to indentation. You can just as easily write your entire addon on one line and it would still work, just be a clusterfuck to read and understand.

Lua Code:
  1. if (returnView) then SaveView(5) end

This reminds me of the abomination I made my NoCombatText addon into years ago when I was in the prime of learning Lua.

The following is a valid addon, using code tags instead of highlight tags for maximum effect:

Code:

local b,c,d,e,f,g,h,j,k,l,n,r,s,u,v,w,y,z=COMBAT,nil,EVENTS_LABEL,PET,
CreateFrame('frame'),'|cff00ff00',PetFrame,PLAYER,'player',PlayerFrame,
'NoCombatText','|cffff0000',' ','UNIT_COMBAT',{j=1,o=1,e=1,},SlashCmdList,
VIDEO_OPTIONS_DISABLED,VIDEO_OPTIONS_ENABLED function f:p(m)nctsv=v for i=1,
NUM_CHAT_WINDOWS do c=_G['ChatFrame'..i]if c:IsEventRegistered('CHAT_MSG_SYSTEM'
)then c:AddMessage('|cffffff00'..n..': '..string.lower(m))end end end function
f:q(x)x:UnregisterEvent(u)end SLASH_NCTPL1='/playerct'function w:NCTPL()if v.j
then l:RegisterUnitEvent(u,k,'vehicle')v.j=nil f:p(j..s..b..s..d..s..r..z)else
f:q(l)v.j=1 f:p(j..s..b..s..d..s..g..y)end end SLASH_NCTPE1='/petct'function
w:NCTPE()if v.e then h:RegisterUnitEvent(u,'pet',k)v.e=nil f:p(e..s..b..s..d..
s..r..z)else f:q(h)v.e=1 f:p(e..s..b..s..d..s..g..y)end end f:RegisterEvent(
'ADDON_LOADED')f:SetScript('OnEvent',function(_,_,a)if a==n and nctsv then
v=nctsv nctsv=nil end if v.j then f:q(l)end if v.e then f:q(h)end nctsv=v end)

I still tend to ignore common organization methods like spacing things or using more lines, and this addon is still somewhat of a giant mess of "compact code" even now, but this is a great reminder about how far I've come.

JDoubleU00 08-12-18 11:37 AM

Quote:

Originally Posted by Kanegasi (Post 329299)
This reminds me of the abomination I made my NoCombatText addon into years ago when I was in the prime of learning Lua.

The following is a valid addon, using code tags instead of highlight tags for maximum effect:

Code:

local b,c,d,e,f,g,h,j,k,l,n,r,s,u,v,w,y,z=COMBAT,nil,EVENTS_LABEL,PET,
CreateFrame('frame'),'|cff00ff00',PetFrame,PLAYER,'player',PlayerFrame,
'NoCombatText','|cffff0000',' ','UNIT_COMBAT',{j=1,o=1,e=1,},SlashCmdList,
VIDEO_OPTIONS_DISABLED,VIDEO_OPTIONS_ENABLED function f:p(m)nctsv=v for i=1,
NUM_CHAT_WINDOWS do c=_G['ChatFrame'..i]if c:IsEventRegistered('CHAT_MSG_SYSTEM'
)then c:AddMessage('|cffffff00'..n..': '..string.lower(m))end end end function
f:q(x)x:UnregisterEvent(u)end SLASH_NCTPL1='/playerct'function w:NCTPL()if v.j
then l:RegisterUnitEvent(u,k,'vehicle')v.j=nil f:p(j..s..b..s..d..s..r..z)else
f:q(l)v.j=1 f:p(j..s..b..s..d..s..g..y)end end SLASH_NCTPE1='/petct'function
w:NCTPE()if v.e then h:RegisterUnitEvent(u,'pet',k)v.e=nil f:p(e..s..b..s..d..
s..r..z)else f:q(h)v.e=1 f:p(e..s..b..s..d..s..g..y)end end f:RegisterEvent(
'ADDON_LOADED')f:SetScript('OnEvent',function(_,_,a)if a==n and nctsv then
v=nctsv nctsv=nil end if v.j then f:q(l)end if v.e then f:q(h)end nctsv=v end)

I still tend to ignore common organization methods like spacing things or using more lines, and this addon is still somewhat of a giant mess of "compact code" even now, but this is a great reminder about how far I've come.

Clearly a wall of text. :)


All times are GMT -6. The time now is 07:19 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI