Jump to content




[SOLVED] Editing a table doesn't work

turtle lua

3 replies to this topic

#1 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

Posted 01 March 2017 - 09:44 PM

Hey Guys,

it's been a while but I'm finally motivated again so I continued programming on my Strip-mining program, which takes all ores with it aswell. Full code: http://pastebin.com/fptxKN9n
But I got 2 Problems, that I couldn't solve after hours of work, the following one and another that I will post seperatly (http://www.computerc...ut-never-after/).
So, I'm working on a menu structure for my program and I have a table defining the y-axis where stuff gets written.
So, when for example the fuellevel is set to infinity then every other line on that page can go 1 line up.

objects = {["1"] = 1, ["2"] = 2, ["3"] = 3, ["4"] = 4, ["5"] = 5, ["6"] = 6, ["7"] = 7, ["8"] = 8, ["9"] = 9, ["10"] = 10, ["11"] = 11, ["12"] = 12, ["13"] = 13}
the table (I gave the variables this stupid string names because I was hoping it could fix it, but obviously didn't, so don't get confused)

function MoveObjects(deleted)
  for k, v in ipairs(objects) do
	if tonumber(k) > deleted and k < 12 then --every line after the deleted goes 1 up, except the last 2 because they are fixed
	  v = v - 1
	end
  end
end
the function that should move every Line up

if turtle.getFuelLevel ~= "infinity" then
	printLeft("Fuellevel:", objects["3"])
	if FuelLevel >= neededFuel*1.1 then
	  Fuellevel = "perfect"
	elseif FuelLevel >= neededFuel then
	  Fuellevel = "ready"
	elseif FuelLevel > neededFuel*0.8 then
	  Fuellevel = "risky"
	else
	  Fuellevel = "low"
	  printCentered("WARNING!!!", objects["3"])
	end
	if help[1] == false then
	  help[1] = FuelLevel
	end
	if Color() then
	  if Fuellevel == "perfect" then
		term.setBackgroundColor(colors.lime)
	  elseif Fuellevel == "ready" or Fuellevel == "risky" then
		term.setBackgroundColor(colors.yellow)
	  elseif Fuellevel == "low" then
		term.setBackgroundColor(colors.red)
	  end
	  printRight(FuelLevel.."/"..neededFuel, objects["3"])
	  term.setBackgroundColor(colors.black)
	else
	  if Fuellevel == "perfect" then
		printRight("+ "..FuelLevel.."/"..neededFuel, objects["3"])
	  elseif Fuellevel == "ready" or Fuellevel == "risky" then
		printRight("+/- "..FuelLevel.."/"..neededFuel, objects["3"])
	  elseif Fuellevel == "low" then
		printRight("- "..FuelLevel.."/"..neededFuel, objects["3"])
	  end
	end
  else
	MoveObjects(3) <---------------------------------
  end
example where this function gets called (ignore the rest of the code, its ugly :P )

The code doesn't throw an error but the function simply seems not to work because everything else just prints at its normal line and the "deleted line" just stays clear.

If there is anything not clear feel free to ask me, if I made misleading spelling or grammar mistakes comment them aswell, I am german and don't speak the best english :)

Thanks in advance,
Bruno

Edited by BrunoZockt, 02 March 2017 - 04:01 PM.


#2 supernicejohn

  • Members
  • 98 posts
  • LocationSweden

Posted 01 March 2017 - 10:26 PM

Two problems as far as I can see, one is that the iterator 'ipairs' wants a numbered table, and the loop wont run a single time, because all it finds is strings (even if those strings are representations of those numbers). It will loop if you remove the "" and be left with, for example ...[2] = 2,...
The second problem can be hard to spot, it's this line:
v = v + 1
It doesn't do what you (presumably) want to do, it's the same thing as writing:
objects[3] = 3 -- for example
do
   local v = objects[3]
   v = v - 1
end -- here, v is no more, since it is local to that scope, and objects[3] was never changed.
print(objects[3]) -- will print 3.
So instead you'd want objects[k] = objects[k] - 1.

There is also a handy function called table.remove( (table) ,n), which will do almost what you want, it'll just kick down (or up, or left, however one thinks about it!) 11 and 12 as well, but perhaps those could be moved outside of the table since they don't need to be moved.
Looks good otherwise, just a small detail :)

#3 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

Posted 01 March 2017 - 11:14 PM

Thanks a lot for your efford!
The Number-string-thing was just because nothing worked, so I was trying everything (stupid 😁).
However, the tip with the objects[k] = objects[k] - 1 makes totally sense, I just couldn't figure it out. I can't try it right now, but I will tomorrow and then I write whether it works.
Thanks again,
Bruno

#4 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

Posted 02 March 2017 - 03:15 PM

View PostBrunoZockt, on 01 March 2017 - 11:14 PM, said:

Thanks a lot for your efford!
The Number-string-thing was just because nothing worked, so I was trying everything (stupid ������).
However, the tip with the objects[k] = objects[k] - 1 makes totally sense, I just couldn't figure it out. I can't try it right now, but I will tomorrow and then I write whether it works.
Thanks again,
Bruno

I tried it out now and it works perfectly! Thanks!

Bruno





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users