Thread Tools Display Modes
03-03-15, 05:45 PM   #1
florian_9598
A Defias Bandit
Join Date: Mar 2015
Posts: 3
Lua dynamic class name possible ?

Hello everyone;

I'm new in LUA, i want to set a dynamic name class, for exemple :

Lua Code:
  1. Frame:SetPoint("TOPLEFT",Test.About._G[Test],"TOPLEFT",-100,-49)

It's not work ! :'(

But this, is good :

Lua Code:
  1. Frame:SetPoint("TOPLEFT",_G[Test].About.Test,"TOPLEFT",-100,-49)

How to dynamically assign the name of a class after the first object ?

Ex Test.About._G[Test], Test.About.(Test), Test.About.{Test} ?
  Reply With Quote
03-03-15, 06:07 PM   #2
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
You need a simpler example

local a = {} --a is a table
a.a = 1
a.b = 2
c = "a"
print(a[c]) --should print 1
c = "b"
print(a[c]) --should print 2
  Reply With Quote
03-03-15, 06:35 PM   #3
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I don't really understand what you're looking for, but I can tell you that this:
Code:
Test.About._G[Test]
works out to be equivalent to this:
Code:
Test["About"]["_G"][Test]
because when you're using dot instead of brackets, the key is intrepreted literally (or some other explanation that's terminologically correct)

You probably want this:
Code:
Test.About[_G[Test]]
__________________
Grab your sword and fight the Horde!
  Reply With Quote
03-04-15, 05:46 AM   #4
florian_9598
A Defias Bandit
Join Date: Mar 2015
Posts: 3
Hello and thanks for your reply.
Sorry, but you dont understand what i want.

Imagine i want to create ten frame each with a different name, ex:

Lua Code:
  1. for i=0,10 do
  2.     _G["ImagineTestFrame"..i] = CreateFrame("Frame","ImagineTestFrame"..i,UIPanelButtonTemplate)
  3. end

All good, no problem !
I have ImagineTestFrame0, ImagineTestFrame1, ImagineTestFrame2, ImagineTestFrame3 ...

If I want to use class, i can this, ex :

Lua Code:
  1. Imagine = CreateFrame("Frame",Imagine,UIParent)
  2. Imagine.TestFrame0 = CreateFrame("Frame",Imagine.TestFrame0,UIParent)
  3. Imagine.TestFrame1 = CreateFrame("Frame",Imagine.TestFrame1,UIParent)
  4. Imagine.TestFrame2 = CreateFrame("Frame",Imagine.TestFrame2,UIParent)
  5. Imagine.TestFrame3 = CreateFrame("Frame",Imagine.TestFrame3,UIParent)

All good, i have Imagine.TestFrame0,Imagine.TestFrame1,Imagine.TestFrame2,Imagine.TestFrame3

But if i want use my for function to automatical create lots of TestFrame with class system, how can i do ?
For exemple what i want and not work :'( :

Lua Code:
  1. Imagine = CreateFrame("Frame",Imagine,UIParent)
  2. for i=0,10 do
  3.     Imagine._G["TestFrame"..i] = CreateFrame("Frame",Imagine._G["TestFrame"..i],UIParent)
  4. end

I have "attempt to index field '_G' (a nil value)" error.

Thank for your help.
  Reply With Quote
03-04-15, 05:53 AM   #5
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Lua Code:
  1. Imagine = CreateFrame("Frame",Imagine,UIParent)
  2. for i=0,10 do
  3.     Imagine["TestFrame"..i] = CreateFrame("Frame",Imagine["TestFrame"..i],UIParent)
  4. end
  Reply With Quote
03-04-15, 06:05 AM   #6
florian_9598
A Defias Bandit
Join Date: Mar 2015
Posts: 3
Originally Posted by Banknorris View Post
Lua Code:
  1. Imagine = CreateFrame("Frame",Imagine,UIParent)
  2. for i=0,10 do
  3.     Imagine["TestFrame"..i] = CreateFrame("Frame",Imagine["TestFrame"..i],UIParent)
  4. end
Work,

You have solve my problem, thank so much .
  Reply With Quote
03-04-15, 06:13 AM   #7
Dorwido
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 54
Beside that you should unterstand what _G is, that is the global table in lua holding all the global stuff, there is no need at all to ever attach it like you did
__________________
Auction Analytics
http://www.wowauction.org/
  Reply With Quote
03-04-15, 10:10 AM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Also this part:

Code:
Imagine["TestFrame"..i] = CreateFrame("Frame",Imagine["TestFrame"..i],UIParent)
...performs a table lookup, finds no value (because the frame hasn't been created and assigned to that key in that table yet, and even if it had been, a table is not a valid value for use as a frame name) and results in passing nil to CreateFrame, so you should just do that directly:

Code:
Imagine["TestFrame"..i] = CreateFrame("Frame",nil,UIParent)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Lua dynamic class name possible ?

Thread Tools
Display Modes

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