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

[Lua][Error] Attempt to call a string

Started by wrothmonk, 16 February 2013 - 05:32 PM
wrothmonk #1
Posted 16 February 2013 - 06:32 PM
EDIT: Solved problem, the function needed to be something else other than "craft".

I'm attempting to create a code for crafting tools in a crafty turtle. There is a function called "craft" in the code, when attempting to use it however, I get the error saying its not a function, but actually a string. edit: Actual error is "craft:(line the craft function was called during the program running): attempt to call string". So for crafting a sword it comes up with "craft:43: attempt to call string" and when crafting a pickaxe it comes up with "craft:53: attempt to call string".


craft
Spoilerfunction cleararea()
[indent=1]for slot = 1, 14, 1 do[/indent]
[indent=2]turtle.select(slot)[/indent]
[indent=2]if turtle.compareTo(15) == true then[/indent]
[indent=3]turtle.transferTo(15, turtle.getItemCount(slot))[/indent]
[indent=2]elseif turtle.compareTo(16) == true then[/indent]
[indent=3]turtle.transferTo(16, turtle.getItemCount(slot))[/indent]
[indent=2]elseif turtle.compareTo(14) == true then[/indent]
[indent=2]else[/indent]
[indent=3]print("Improper item in turtle inventory.")[/indent]
[indent=2]end[/indent]
[indent=1]end[/indent]
end

function dump()
[indent=1]turtle.select(15)[/indent]
[indent=1]turtle.dropDown(turtle.getItemCount(15))[/indent]
[indent=1]turtle.select(16)[/indent]
[indent=1]turtle.dropDown(turtle.getItemCount(16)[/indent]
end

function craft()
[indent=1]dump()[/indent]
[indent=1]turtle.craft(1)[/indent]
[indent=1]print("Please collect crafted item")[/indent]
end

function handle()
[indent=1]for slot = 6, 10, 4 do[/indent]

[indent=1]turtle.select(16)[/indent]
[indent=1]turtle.transferTo(slot, 1)[/indent]
end
end

function sword()
[indent=1]cleararea()[/indent]
[indent=1]for slot = 2, 6, 4 do[/indent]
[indent=2]turtle.select(15)[/indent]
[indent=2]turtle.transferTo(slot, 1)[/indent]
[indent=1]end[/indent]
[indent=1]turtle.select(16)[/indent]
[indent=1]turtle.transferTo(10, 1)[/indent]
[indent=1]craft()[/indent]
end

function pickaxe()
[indent=1]cleararea()[/indent]
[indent=1]for slot = 1, 3, 1 do[/indent]
[indent=2]turtle.select(15)[/indent]
[indent=2]turtle.transferTo(slot, 1)[/indent]
[indent=1]end[/indent]
[indent=1]handle()[/indent]
[indent=1]craft()[/indent]
end

print("Please insert sticks into slot 16.")
print("Please insert material into slot 15.")
print("Pleae remove any items in slot 14.")
print("What would you like to craft?")
craft = io.read()
if craft == "sword" then
[indent=1]sword()[/indent]
elseif craft == "pickaxe" then
[indent=1]pickaxe()[/indent]
else
[indent=1]print("I do not know how to craft that.")[/indent]
end

I've also run a test where it just calls the craft function and it came up with the same error with the line again being the line where the function was called.

part of code edited
Spoilerprint("Please insert sticks into slot 16.")
print("Please insert material into slot 15.")
print("Pleae remove any items in slot 14.")
print("What would you like to craft?")
craft = io.read()
if craft == "sword" then
[indent=1]sword()[/indent]
elseif craft == "pickaxe" then
[indent=1]pickaxe()[/indent]
elseif craft == "craft" then

[indent=1]craft()[/indent]
else
[indent=1]print("I do not know how to craft that.")[/indent]
end
tesla1889 #2
Posted 16 February 2013 - 09:03 PM
what is dump defined as?
LordIkol #3
Posted 16 February 2013 - 09:07 PM
error must be somewhere else
i took the code generated dump and cleararea function as blank and it works.

i think dump removes the items in slot 15 and 16 and cleararea removes all items in slot 1 to 14

so if you want help please post the whole code so that we can figure out whats the problem.

Greets Loki
wrothmonk #4
Posted 17 February 2013 - 03:52 AM
Alright, updating the topic now, I didn't think you would need the rest as I assumed by the error it was a problem with defining the function.
wrothmonk #5
Posted 17 February 2013 - 06:46 AM
I cant believe I didnt try this earlier. Apparently theres something weird with attempting to make a function call craft(), I changed it to make() and it works perfectly now.
remiX #6
Posted 17 February 2013 - 07:41 AM
I cant believe I didnt try this earlier. Apparently theres something weird with attempting to make a function call craft(), I changed it to make() and it works perfectly now.

It's because defining a function called craft will overwrite all the turtle.craft() functions.