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

function arguments expected

Started by DjTranceFire, 25 January 2015 - 03:44 PM
DjTranceFire #1
Posted 25 January 2015 - 04:44 PM
Hey there.
I'm realy trying to learn all that lua stuff and how to use ComputerCraft for usefull stuff.
On every project that i started the time comes that i cant fix my problems because i'm to new in lua.
Because of that i'm trying to learn everything step by step instead of just jumping right into a big project.
I found a nice little tutorial on youtube and i'm trying to follow it step by step.
But it seems like i'm stuck now. The code that i used works fine on the youtube video.

My setup is easy. Its just a computer ontop of a ME Interface. On one side of the interface a chest.
Got some wood in my ME Drive and the Computer can find that wood if i run this:

Spoiler

me = peripheral.wrap("bottom")
liste = me.getAvailableItems()

for nummer, item in pairs(liste) do
	print(item.id.." "..item.dmg.." "..item.qty.." "..item.name)
end


But when i try to suck some items out of the ME and push them into the chest i just get

bios:366: [string "test"]:6: function arguments expected

This code seems to work fine in the video i'm watching:

Spoiler

me = peripheral.wrap("bottom")
liste = me.getAvailableItems()

for nummer, item in pairs(liste) do
--	print(item.id.." "..item.dmg.." "..item.qty.." "..item.name)
	if item.id == minecraft:planks and item.dmg == 0 then
		liste[nummer].qty = 17
		ret = me.extractItem(liste[nummer], "west")
		print("rausgeworfen")
		print("ret = "..ret)
	end
end


I dont get why this is working in the video but not on my part.
Anyone here that maybe can find the problem and help me getting better in lua?
SquidDev #2
Posted 25 January 2015 - 05:50 PM
The trouble lies here:

if item.id == minecraft:planks and item.dmg == 0 then

minecraft:planks should probably be "minecraft.planks" (notice the quote marks: this is a string, so needs to have them).
InDieTasten #3
Posted 25 January 2015 - 05:50 PM
I don't know the environment with your "minecraft:planks", but I would guess that you problem is associated with it. Are you sure it needing to be a ":" rather than "." ?

Already answered right when I pushed that Post button -.- hate it when this happens :D/>
Edited on 25 January 2015 - 04:52 PM
DjTranceFire #4
Posted 25 January 2015 - 06:17 PM
I don't know the environment with your "minecraft:planks", but I would guess that you problem is associated with it. Are you sure it needing to be a ":" rather than "." ?

Already answered right when I pushed that Post button -.- hate it when this happens :D/>

When i print the item.id it prints minecraft:planks instead of minecraft.planks
Spoiler

The trouble lies here:

if item.id == minecraft:planks and item.dmg == 0 then

minecraft:planks should probably be "minecraft.planks" (notice the quote marks: this is a string, so needs to have them).

Already tested it and it does not work.

item.id == minecraft:planks
function arguments expected
(line 6)

item.id == minecraft.planks
attempt to index ? (a nil value)
(line 6)

item.id == "minecraft:planks"
attemp to call nil
(line 8)

item.id == "minecraft.planks"
does nothing.
Edited on 25 January 2015 - 05:21 PM
Quintuple Agent #5
Posted 25 January 2015 - 06:25 PM
Since "minecraft:planks" Threw back an error on a different line, it looks like that one is the one that worked.Looking at line 8, you have 'nummer' instead of number, I see that you have that at the start in the for loop, just make sure those are both the same.
Edited on 25 January 2015 - 05:31 PM
DjTranceFire #6
Posted 25 January 2015 - 06:29 PM
Looking at line 8, you have 'nummer' instead of number, I see that you have that at the start in the for loop, just make sure those are both the same.

Thats ok, nummer is the german word for number.
Its located in line 4, 7 and 8 and its everytime nummer instead of number so this cant be the problem.
Quintuple Agent #7
Posted 25 January 2015 - 06:31 PM
Ok, Thought it might be in a different language but was not sure.

How are you coping this code to and from the forum? Direct copy paste from the server files or are you writing it line by line. Because if it is not a direct copy make sure you spelled everything right on line 8 including capitals, because the code looks right.
Edited on 25 January 2015 - 05:34 PM
DjTranceFire #8
Posted 25 January 2015 - 06:44 PM
I directly copy it from the server. Everything here is exactly the same that i'm trying to use.
Quintuple Agent #9
Posted 25 January 2015 - 06:55 PM
When I copied over the code .id always returns a number, so I don't know how it goes to line 8 when when made it a string.
When I changed it over to 5 (number ID for wood planks) and tested it the program worked fine.

Edit: Oh, strange, it does return strings for you. Must be changed on different versions.
Edited on 25 January 2015 - 06:07 PM
InDieTasten #10
Posted 25 January 2015 - 07:01 PM
When i print the item.id it prints minecraft:planks instead of minecraft.planks
Well print does use the "tostring operation" on every argument.
So you should be able to just check like:

tostring(item.id) == "minecraft:planks"

I could imagine it is using metatables with a tostring function passed.
Why don't you test the type of item.id ? If it is in fact a table, the code above should work.
DjTranceFire #11
Posted 25 January 2015 - 07:27 PM
Edit: Oh, strange, it does return strings for you. Must be changed on different versions.

That should be because you are using 1.6.4 and i'm using 1.7.10.
For what i know there are no ID's in 1.7.10.


Well print does use the "tostring operation" on every argument.
So you should be able to just check like:

tostring(item.id) == "minecraft:planks"

I could imagine it is using metatables with a tostring function passed.
Why don't you test the type of item.id ? If it is in fact a table, the code above should work.

So like this?:
if tostring(item.id) == "minecraft:planks" and item.dmg == 0 then
If yes, thats exactly the same output like this:
if item.id == "minecraft:planks" and item.dmg == 0 then


both just give this error:
test:8: attempt to call nil



For everyone who wants to try the code, this is my current setup. The chest it to the west direction so you maybe have to change that if your chest ist not on the west side.
Spoiler
InDieTasten #12
Posted 25 January 2015 - 07:38 PM
both just give this error
You should try to do only one comparison to locate the problem source better. leave your dmg-check out of the line for debugging purposes and check whether it runs.
Although this error seems really really weird, because you don't call any functions in this line.

I guess there are metatables throwing errors and pointing them to you as you do index operations to the item.

I would like to help you in a more direct way of communication. If you have skype, maybe pm me your username ;)/>
SquidDev #13
Posted 25 January 2015 - 09:14 PM
Although this error seems really really weird, because you don't call any functions in this line.

I guess there are metatables throwing errors and pointing them to you as you do index operations to the item.

I don't think metatables should come into it. Line 8 should be:

ret = me.extractItem(liste[nummer], "west")

So I guess that me.extractItem does not exist. What does for k,v in pairs(me) do print(k) end produce?
DjTranceFire #14
Posted 25 January 2015 - 11:06 PM
What does for k,v in pairs(me) do print(k) end produce?

Its a huge list of available options. Cant read everything that it prints.
Is there any way to split it into pages or save the output to a file?



EDIT:
Got it, just used the monitor to print everything out.

This seems to be every function:
Spoiler

getStoredPower
getAdvancedMethodsData
pullItem
destroyStack
getCraftingCPUs
getIdlePowerUsage
listMethods
exportItem
getInventorySize
getItemDetail
pullItemIntoSlot
doc
pushItemIntoSlot
getStackInSlot
getAllStacks
swapStacks
getAvgPowerInjection
getAvgPowerUsage
condenseItems
listSources
hasValidTarget
getMaxStoredPower
getAvailableItems
expandStack
getInventoryName
requestCrafting
pushItem

So for some reasons there is no extractItem function available even if it was in older versions. :/
I just tested the exportItem function but it seems like the the way its used seems to be changed.
When i try to just change extractItem to exportItem i get this:


test:8: Failed to convert arg 'maxAmount' value 'west' to 'Integer'

The problem this time is obvious but now i dont know how to use the command with the right arguments.
Is there any way to get something like a "man page" for the commands?
Edited on 25 January 2015 - 10:27 PM
HPWebcamAble #15
Posted 25 January 2015 - 11:16 PM
Is there any way to split it into pages or save the output to a file?

textutils.pagedTabulate(me) might work
DjTranceFire #16
Posted 26 January 2015 - 12:24 PM
Ok for now i got it to work the way it should.

Spoiler

me = peripheral.wrap("bottom")
liste = me.getAvailableItems()

for nummer, item in pairs(liste) do
--	  print(item.id.." "..item.dmg.." "..item.qty.." "..item.name)
		if item.id == "minecraft:planks" then
				liste[nummer].qty = 17
				me.exportItem(liste[nummer], 1, 0)
				print("rausgeworfen")
		end
end

exportItem needs the item, the ammount you want to have and the slot of the inventory where the item should go.
btw.. The ME-Interface has to point into the right direction else it wont work.
so with me.exportItem(liste[nummer], 1, 0) its throwing the 1 planks into the first slot of the chest (slot 0).
Now i have the next problem. I know that there are infos about the id, dmg, qty and the name of the items inside the ME.
Is there any way to get more informmations about whats inside the table?
Because with the current infos i dont know anything about the NBT Data and if i want to pull an item with a specific nbt data i dont know how.


And one more problem with the code from my first post.

	ret = me.exportItem(liste[nummer], 1, 0)
	print("ret = "..ret)
Its throwing out an error for the print.
attemp to concatenate string and table
All i want to do is print the number of items that are exportet.
So in this example it should print
 ret = 1 
Whats wrong with this code?
Edited on 26 January 2015 - 11:29 AM
HPWebcamAble #17
Posted 27 January 2015 - 12:46 AM
And one more problem with the code from my first post.

	ret = me.exportItem(liste[nummer], 1, 0)
	print("ret = "..ret)
Its throwing out an error for the print.
attemp to concatenate string and table
All i want to do is print the number of items that are exportet.
So in this example it should print
 ret = 1 
Whats wrong with this code?

Seems like exportItem returns a table. Try


ret = me.exportItem(liste[nummer], 1, 0)
print("ret = "..tostring(ret))