103 posts
Posted 05 June 2014 - 07:21 PM
Or are there better ways to handle this:
function tryForward_track(fd)
while not turtle.forward() do
if turtle.detect() then
turtle.dig()
end
end
fd = fd + 1
end
Is it possible to take a local variable - pass it to a function (in an API)?
Basically the above function is in my API; and my program calls that to keep track of how far it's moving forward - but I get the error: "attempt to perform arithmetic function __add on nil and number"
Edited on 05 June 2014 - 05:22 PM
8543 posts
Posted 05 June 2014 - 07:44 PM
Moved to Ask a Pro.
570 posts
Posted 05 June 2014 - 07:47 PM
Can you post the code that uses your API function? The error message states that 'fd' is nil.
463 posts
Location
Germany
Posted 05 June 2014 - 07:58 PM
I'm pretty sure numbers are passed byval, not byref, which would mean that:
local something = 123
tryForward_track(something)
print(something) -- will print 123
Of course, this is not the cause of the problem. I just wanted to say that you will have to add a return including fd, and then allocate the return to the base variable again, like this:
local something = 123
something = tryForward_track(something)
print(something) -- will print 124
For your first problem, why the arithmetical error ocurrs, you have to post the whole code.
103 posts
Posted 05 June 2014 - 08:03 PM
Sorry Lyqyd - wasn't paying attention to what forum I was in! :(/>/>
I actually just wiped out the code and went a different direction - but the line in my program calling that function was simply:
fd = tryForward_track(fd)
the very first line of the code was:
local fd = 0
I did do a work around tho; I simply wrote a small function called 'tryForward_track' - and then said 'fd=fd+1' and 'tryForward()'; it works out the same… But I'd like to know what I did wrong in that code above to get the error…
Edited on 05 June 2014 - 06:07 PM
463 posts
Location
Germany
Posted 05 June 2014 - 08:14 PM
Your code above will cause the error if you use it the second time. You forgot to return fd
function tryForward_track(fd)
while not turtle.forward() do
if turtle.detect() then
turtle.dig()
end
end
fd = fd + 1
return fd
end
Another possible way: If tryForward_track is in the same file as fd variable, you could just remove the fd argument
function tryForward_track()
while not turtle.forward() do
if turtle.detect() then
turtle.dig()
end
end
fd = fd + 1
end
(this way, fd is an upvalue, meaning it is declared in a block above it,
the old way, fd would become local, and when you would assign a new value to it, you would edit the local value, not the upvalue.)
Edited on 05 June 2014 - 06:15 PM
103 posts
Posted 05 June 2014 - 09:03 PM
Ah, I wasn't sure if I should use return in that case or not; got it! Thanks :)/>
171 posts
Location
Eastern USA
Posted 05 June 2014 - 09:45 PM
You can also put your value into a table, because tables are passed by reference
local x = {val=2}
function foo(x)
x.val = x.val + 1
end
foo(x)
print(x.val) --prints 3
215 posts
Location
Netherlands
Posted 05 June 2014 - 11:21 PM
Maybe alot of people answered my question, but I am going to take a shot to clarify it. Maybe to give you a little bit of clarification about variables.
foo = "bar" --#Global variable, can be used everywhere
if 1 ~= 0 then
local foo = "bar" --#Local variable that can only (!) be used in the if-statement
end
function foo()
local bar = "foobar" --#Local variable that can only be used in this specific block
end
Now, blocks can be if-statement, while- and for-loops and functions. That last part is where you are interested in. You can pass variables through by giving the function variables separated by comma's in the brackets. These variables can range from strings to numbers. Not to sure about integers. These variables that you put in between the brackets only apply to the function (that block). Demonstration:
function calculate(foo, bar)
a = foo
b = bar
end