This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Geforce Fan's profile picture

Using a table to remember variables and get those variables by calling the number in the sequence of which they where created.

Started by Geforce Fan, 30 October 2012 - 11:12 PM
Geforce Fan #1
Posted 31 October 2012 - 12:12 AM
This code doesn't seem to remember the variables like the title says, it only seems to remember the latest one not the older ones
THE APPLICATION CODE[VERY messy]
Spoiler



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
towsx = {}
towsx[isx] = x
towsy = {}
towsy[isx] = y
print(isx)
isx= isx + 1
end

--
--[[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
function drawPath()
term.setCursorPos(1,10)
lineE = "-----------------------------------------------------"
term.write(""..lineE.."") ;
term.setCursorPos(1, 12)
term.write(""..lineE.."") ;
drawMenu()
end
function drawMenu()
term.setCursorPos(1,18)
term.write(""..lineE.."") ;
term.setCursorPos(1,19)
term.write("Buy tower Sell Tower") ; --9

end
function menuClicks()
_2, button2, x2, y2 = os.pullEvent ("mouse_click") ;
if y2 ==19 and x2 <9 then
placeTower()
elseif y2 ==19 and x2 >9 and x2 <19 then
print("hi") ;
else
menuClicks()
end
end
--[[Notes:
Need a menu bar function w/ "add a tower"
--]]


drawPath()
placeTower()
Doyle3694 #2
Posted 31 October 2012 - 12:21 AM
please, indent your code
remiX #3
Posted 31 October 2012 - 12:43 AM
I'm not quite getting what your problem is. could you explain a little bit more :P/>/>
Orwell #4
Posted 31 October 2012 - 12:46 AM
Ehm, one thing I can make of this is that you're using the variable isx as a number, but declared it as a string ("1").
Geforce Fan #5
Posted 31 October 2012 - 12:50 AM
please, indent your code
I never understood indenting. It's a waste of time. I find it really hard to read indented code, too.
EDIT:
I just devoloped my own version of indenting. It's easy to read yet it really easy for me to do.

I'm not quite getting what your problem is. could you explain a little bit more :P/>/>
When I write more to my table in the code, it erases the old data in the table rather than adding to it.


Ehm, one thing I can make of this is that you're using the variable isx as a number, but declared it as a string ("1").
LUA finds what kind of thingy a variable is automatically.
Lyqyd #6
Posted 31 October 2012 - 12:56 AM
please, indent your code
I never understood indenting. It's a waste of time. I find it really hard to read indented code, too.

I'm not quite getting what your problem is. could you explain a little bit more :P/>/>
When I write more to my table in the code, it erases the old data in the table rather than adding to it.


Ehm, one thing I can make of this is that you're using the variable isx as a number, but declared it as a string ("1").
LUA finds what kind of thingy a variable is automatically.

Indenting is valuable for seeing the scope of various blocks as well as the flow of the logic in the program. It also makes it significantly easier for most people to read the code.

When you write new data to a table at an index you've already written to, it will of course overwrite the old data at that index. This is normal behavior. If you're misusing the table, expect to get undesired results.

Lua treats table[1] and table["1"] as different indices, for good reason. If you index it as a string once and as a number another time, don't expect it to work.

You need to not be so snippy and know-it-all with those trying to help you, especially when they're right.
Geforce Fan #7
Posted 31 October 2012 - 01:03 AM
please, indent your code
I never understood indenting. It's a waste of time. I find it really hard to read indented code, too.

I'm not quite getting what your problem is. could you explain a little bit more :P/>/>
When I write more to my table in the code, it erases the old data in the table rather than adding to it.


Ehm, one thing I can make of this is that you're using the variable isx as a number, but declared it as a string ("1").
LUA finds what kind of thingy a variable is automatically.

Indenting is valuable for seeing the scope of various blocks as well as the flow of the logic in the program. It also makes it significantly easier for most people to read the code.

When you write new data to a table at an index you've already written to, it will of course overwrite the old data at that index. This is normal behavior. If you're misusing the table, expect to get undesired results.

Lua treats table[1] and table["1"] as different indices, for good reason. If you index it as a string once and as a number another time, don't expect it to work.

You need to not be so snippy and know-it-all with those trying to help you, especially when they're right.
Thanks
To the bold text
I get like this if it's either late at night and I've gotten crudloads of errors in a program or if it's just REALLY late at night
remiX #8
Posted 31 October 2012 - 01:14 AM
Early in the morning for me :P/>/>
Geforce Fan #9
Posted 31 October 2012 - 02:09 AM
please, indent your code
I never understood indenting. It's a waste of time. I find it really hard to read indented code, too.

I'm not quite getting what your problem is. could you explain a little bit more :P/>/>
When I write more to my table in the code, it erases the old data in the table rather than adding to it.


Ehm, one thing I can make of this is that you're using the variable isx as a number, but declared it as a string ("1").
LUA finds what kind of thingy a variable is automatically.

Indenting is valuable for seeing the scope of various blocks as well as the flow of the logic in the program. It also makes it significantly easier for most people to read the code.

When you write new data to a table at an index you've already written to, it will of course overwrite the old data at that index. This is normal behavior. If you're misusing the table, expect to get undesired results.

Lua treats table[1] and table["1"] as different indices, for good reason. If you index it as a string once and as a number another time, don't expect it to work.

You need to not be so snippy and know-it-all with those trying to help you, especially when they're right.
Thanks for try to help, but I tried it and it didn't work. Do I need to run another command or is this just not possible?
Lyqyd #10
Posted 31 October 2012 - 02:13 AM
Hmm? What'd you try that didn't work? I'm not sure what you mean.
Geforce Fan #11
Posted 31 October 2012 - 09:55 PM
I need something to keep track of the number of the table[the key] and add new stuff to the table WITHOUT erasing the old stuff.
remiX #12
Posted 31 October 2012 - 10:34 PM
I need something to keep track of the number of the table[the key] and add new stuff to the table WITHOUT erasing the old stuff.

table.insert(table,"text")
Doesn't delete what is already in the table.
ChunLing #13
Posted 31 October 2012 - 11:11 PM
You want a normal queue structure. Just keep expanding the table sequentially. Insert works, but is slower.