WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Lua dynamic class name possible ? (https://www.wowinterface.com/forums/showthread.php?t=52027)

florian_9598 03-03-15 05:45 PM

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} ?

Banknorris 03-03-15 06:07 PM

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

Lombra 03-03-15 06:35 PM

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]]

florian_9598 03-04-15 05:46 AM

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.

Banknorris 03-04-15 05:53 AM

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

florian_9598 03-04-15 06:05 AM

Quote:

Originally Posted by Banknorris (Post 307257)
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 :).

Dorwido 03-04-15 06:13 AM

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

Phanx 03-04-15 10:10 AM

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)


All times are GMT -6. The time now is 06:19 PM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI