LoTROInterface

LoTROInterface (https://www.lotrointerface.com/forums/index.php)
-   Lua Programming Help (L) (https://www.lotrointerface.com/forums/forumdisplay.php?f=50)
-   -   Map tracker (https://www.lotrointerface.com/forums/showthread.php?t=2048)

crys17p 09-14-2013 01:40 PM

Map tracker
 
Hello guys i am new to lua and i have a problem. I am making a kind of map tracker for a turtle bot in minecraft(yes i know they have forums specifically for that but i am not getting a response form them). The map tracker stores the last five moves made into an array. Afterwards if the robot has a certain level of fuel it retraces it's steps by doing the inverse of the five last steps.

The robot stores the steps correctly the problem is when it enters the condition with low fuel that the values of the array become all nil and none of the conditions match and hence does nothing. It is strange because if i don't use the if statements for checking fuel level and fuel item count it works. Thank you for your response in advance.


map = {}
j = 5
i = 5


if turtle.getFuelLevel() < 100 then

if turtle.getItemCount(1) == 0 then

for count = 1,j do

if map[i] == 1 then
turtle.back()
end

if map[i] == 2 then
turtle.forward()
end

if map[i] == 3 then
turtle.down()
end

if map[i] == 4 then
turtle.up()
end

if map[i] == 5 then
turtle.turnRight()
end

if map[i] == 6 then
turtle.turnLeft()
end
i = i - 1
end

else
turtle.select(1)
turtle.refuel()
end


else
i = 5
turtle.forward()
map[1] = 1

turtle.up()
map[2] = 3


turtle.forward()
map[3] = 1


turtle.up()
map[4] = 3


turtle.back()
map[5] = 2


end

Equendil 09-17-2013 10:44 AM

I have no idea what minecraft is about let alone how its scripting capabilities work but if that's a script that is called every so often by the game engine, what you are doing here is systematically create a new empty array you assign to 'map' every time the script is called, so naturally, it 'forgets' what you have done the last time it was called and you end up with 'nil' values.

You could either move the creation of the 'map' array to some initialization script if that's possible or failing that, make sure you only create the 'map' array once if it does not exist:

Code:

if map == nil then
map = {};
end



All times are GMT -5. The time now is 05:25 AM.

vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI