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

[lua][Question] "if not table["somthing"] then"

Started by Henness, 01 February 2013 - 05:49 PM
Henness #1
Posted 01 February 2013 - 06:49 PM
I can't seem to get this small piece of code to work is there another way of doing it that I don't know about.


if not _tSave["StartPos"] then
  _tSave.StartPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
end

or


if not _tSave.StartPos then
  _tSave.StartPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
end

or


if _tSave["StartPos"] == nil then
  _tSave.StartPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
end

or


if _tSave.StartPos == nil then
  _tSave.StartPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
end

If you need it in context its online 731. http://pastebin.com/Zi8Y1S0s
theoriginalbit #2
Posted 01 February 2013 - 06:50 PM
had the . and [] the wrong way around

if not _tSave.StartPos then

_tSave["StartPos"] = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
end

I cant remember why, but there is a reason you cant use . when assigning a key for a table… you can use it as a key in a table, but not a key for a table.
Henness #3
Posted 01 February 2013 - 07:10 PM
Well aparently my code was working I was just being retarded and had a typo further up when calling the table again.
raineth #4
Posted 01 February 2013 - 07:23 PM
I can't seem to get this small piece of code to work is there another way of doing it that I don't know about.
Can you explain how it doesn't work? It looks fine to me (and StartPos appears to be saved correctly if I try running the script.)

I guess your problem is that you think it's not being loaded correctly, and you're going to kick yourself when I point out that you use startPos everywhere else in your code.

edit: I see you already noticed. :)/>