This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Variable names with variables in them
Started by Geforce Fan, 30 October 2012 - 09:15 PMPosted 30 October 2012 - 10:15 PM
I need help doing what the title says. I'm doing stuff like x"..y.." = "#" but it just gives my an error. Solutions?
Posted 30 October 2012 - 10:26 PM
Besides that you got concatenation wrong, it's also9 ot possible that way. Easiest and cleanest way to do this is using_tables.
proper concatenation:
And for tables:
proper concatenation:
x.."y"
instead of:
x"..y.."
And for tables:
vars = {} -- the table
x='bla'
vars[x..'y'] = 'stuff'
print( vars[x..'y'] )
Posted 30 October 2012 - 10:41 PM
How do I add more to that table too and even READ that table, though? -.-
And it doesn't work. I just put in that 2 times, changing the varubles to what I needed, and the vars varuble was diffrent the second time, and it's giving me attempt to concentrate nil and string.
And it doesn't work. I just put in that 2 times, changing the varubles to what I needed, and the vars varuble was diffrent the second time, and it's giving me attempt to concentrate nil and string.
Posted 30 October 2012 - 10:49 PM
We can't help you debug your code without seeing your code.
Posted 30 October 2012 - 10:50 PM
That means that your variable wasn't declared yet. Try my example, it works. 'vars' is the table you put variables into. then 'x' is declared as the string 'bla' a variable that you combine with something else the define the variable name. Then with "vars[x..'y']='test' " you put the string 'test' in the table at the key 'blay'. Then with print(vars[x..'y']) you print what's at that key.
Posted 30 October 2012 - 10:51 PM
I already have my varuble declaired as "1"
I'm just gona put some of the code here:
term.clear() term.setCursorPos(1,1)
isx="1"
--game: tower defence
--FUNCTIONS
fs.delete("TDGchache") ;
function write(text)
term.write(text)
end
function newTower( x, y )
term.setCursorPos(x,y)
write("T")
--TABLES and USEABLE version
tower = "tower"
towsx = {}
towsx[tower..'isx'] = '"..x.."'
towsy = {}
towsy[tower..'isx'] = '"..y.."'
isx="isx+1"
--
--[[towx.."isx" = ""..x..""
towy.."isx" = ""..y..""
isx = "isx + 1"--]]
--[[addt = fs.open("TDGchache", "a") ;
addt.writeLine(""..x.." "..y.."")
addt.close()--]]
menuClicks()
end
function placeTower()
_, button, o, t = os.pullEvent ("mouse_click") ;
if t ==10 then
placeTower()
elseif t ==11 then
placeTower()
elseif t ==12 then
placeTower()
elseif t ==19 then
placeTower()
elseif t ==18 then
placeTower()
else
newTower( o, t ) ;
end
end
Put –changed after a line you made changes in.ANOTHER EDIT:
I put in "tower = "tower"" and it seems to work, but I havn't tested if it actually got the varuble.
YET ANOTHER EDIT!!!:
I had it print what it's getting, it just gets "..x.." I'ved changed the code above to the code I'm using.
Posted 30 October 2012 - 11:03 PM
Anyone?
Posted 30 October 2012 - 11:17 PM
HECK yes I solved it. I just had to make '"..x.."' to x and it worked. LOL.
'
Off topic:
I just released that there's a piece of my code that I can't hardly understand. I pretty much put it there by accident, but hey, it works! :P/>/>
'
Off topic:
I just released that there's a piece of my code that I can't hardly understand. I pretty much put it there by accident, but hey, it works! :P/>/>
Posted 30 October 2012 - 11:25 PM
Now how do I get the charts with isx being 1 when the last written one had isx being 2? Because if I can't, then I need a diffrent code. It has to be able to remeber the old ones.
Posted 30 October 2012 - 11:33 PM
It doesn't even remeber the old stuff.
If I set isx to 1, it just gives me the latest even though with the latest isx was 2. Help
If I set isx to 1, it just gives me the latest even though with the latest isx was 2. Help
Posted 31 October 2012 - 01:41 AM
You know that isx = "isx+1" does not add isx and 1, right? It just makes it a string with the value of "isx+1". That might definitely be your problem.
Just declare isx like this:
Just declare isx like this:
local isx = 1
and increment it like this:
isx = isx + 1