• Pass in generated string as a variable name?
    2 replies, posted
I have a disgusting working version, and then a new method that is less disgusting but I can't seem to get working correctly. Here is the error: Error: Line 14 'SetTexture' (number expected, got string) Broken: [code] local num_0 = surface.GetTextureID ("halflife/num_0") local num_1 = surface.GetTextureID ("halflife/num_1") --etc 0 through 9 local timeTable = splitString(TimeLeft) local spacing = 35 for k, v in pairs(timeTable) do for i = 0, 9 do if v == tostring(i) then texture = ("num_" .. i) spacing = spacing + 35 print(texture) end end surface.SetTexture( texture ) surface.SetDrawColor( HUD_Color ) surface.DrawTexturedRect (250 + spacing ,250,30,30) end [/code] Working: [code] local num_0 = surface.GetTextureID ("halflife/num_0") local num_1 = surface.GetTextureID ("halflife/num_1") --etc 0 through 9 local timeTable = splitString(TimeLeft) local spacing = 35 for k, v in pairs(timeTable) do if v == "0" then texture = num_0 spacing = spacing + 35 elseif v == "1" then texture = num_1 spacing = spacing + 35 elseif v == "2" then texture = num_2 spacing = spacing + 35 elseif v == "3" then texture = num_3 spacing = spacing + 35 elseif v == "4" then texture = num_4 spacing = spacing + 35 elseif v == "5" then texture = num_5 spacing = spacing + 35 elseif v == "6" then texture = num_5 spacing = spacing + 35 elseif v == "7" then texture = num_7 spacing = spacing + 35 elseif v == "8" then texture = num_8 spacing = spacing + 35 elseif v == "9" then texture = num_9 spacing = spacing + 35 end surface.SetTexture( texture ) surface.SetDrawColor( HUD_Color ) surface.DrawTexturedRect (250 + spacing ,250,30,30) end [/code]
You can't access variables like that, put your textures in a table and access them from there. local MyT = {} MyT.num_0 = surface.GetTextureID ("halflife/num_0") ... surface.SetTexture( MyT[ "num_" .. i )
Thanks once again boss man!
Sorry, you need to Log In to post a reply to this thread.