Jump to content

link on var or enum?


kino

Recommended Posts

how to create link on var or enum e.g :factinfo[factnum][1]==factinfo[factnum]["rang1"]?

if you mean a table then.

local  factinfo={} 
factinfo[factnum]={} 
  
--then it's ready to use like. 
  
for i=1 , 10 do  
  
 factinfo[factnum][i]="rang"..i 
  
end 
  

Link to comment
how to create link on var or enum e.g :factinfo[factnum][1]==factinfo[factnum]["rang1"]?

if you mean a table then.

local  factinfo={} 
factinfo[factnum]={} 
  
--then it's ready to use like. 
  
for i=1 , 10 do  
  
 factinfo[factnum][i]="rang"..i 
  
end 
  

no, see exmple psevdo lua code

 factinfo={} 
> factinfo[factnum]={} 
> for i=1,10 do factinfo[i]={} end 
> factinfo[1][1]=3.14 
> print(factinfo[1][1]) 
3.14 
> print(factinfo[1]["data1"]) 
3.14 
> factinfo[1]["data"]=6 
  
> print(factinfo[1][1]) 
6 
  

Link to comment

you can achieve what you want with metatables and metamethods.

  
local  factinfo={} 
setmetatable (factinfo, 
 { 
 __index = function(theTable, index)  
                 if type(index) == "number" then return theTable[factnum]["rang" .. tostring(index)];  end; 
                     return theTable[factnum][index]; 
            end, 
 __newindex = function(theTable, index, value) 
                 if type(index) == "number" then 
                      rawset (theTable[factnum], "rang" .. tostring(index), value); 
                 else  
                      rawset (theTable[factnum], index, value);  
                 end;  
            end 
 }); 
  

now, you can do this....

  
factinfo [1] = 56; 
print (factinfo["rang1"]); -- 56 
print (factinfo[1]); -- 56 
  
factinfo["rang2"] = 5; 
print (factinfo[2]); -- 5 
print(factinfo["rang2"]); -- 5 
print(factinfo[factnum]["rang2"]); -- 5 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...