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

Set variable as return of function

Started by BrunoZockt, 03 March 2017 - 08:13 PM
BrunoZockt #1
Posted 03 March 2017 - 09:13 PM
Hey Guys,

it's me again, sorry…

This time I have a problem with setting a variable.
I basically want the variable to be the return of a function, but I can't figure out how to do this.

This is how I defined my variable (…wrong ^_^/> ):

local TorchSlot = SlotCalculator("T")

This is what the function looks like:

function SlotCalculator(code)
  if code == "T" then
	item = "minecraft:torch"
  end
  for i = 1, #slots do
	if slots[i] == code then
	  if ItemCount(item, i) ~= false and ItemCount(item, i) > 0 then
		return i
	  end
	end
  end
end

The table for understanding:

slots = {
"I", "I", "I", "_",
"_", "_", "_", "_",
"_", "_", "T", "T",
"T", "T", "A", "C"}

function ItemCount:

function ItemCount(Block, Slot)
  if Slot == 0 then
	return false
  end
  if Version >= 1.64 then
	local Data = turtle.getItemDetail(Slot)
	if Data == nil then
	  return 0
	elseif Data.name == Block then
	  return Data.count
	else
	  return false
	end
  else
	return turtle.getItemCount(Slot)
  end
end

When my program gets to the point, where it trys to call this variable it Errors out ("expected number").
I think that the variable has the function and not it's return information as value.
So, I want something like this:

local TorchSlot = (return of SlotCalculator("T"))

Thanks in advance,
Bruno
Edited on 04 March 2017 - 10:13 PM
Dog #2
Posted 03 March 2017 - 09:44 PM
You were doing it right…

local TorchSlot = SlotCalculator("T")

The only thing that stands out to me is this - you're only looking for false values, not nil…

if ItemCount(item, i) ~= false and ...

I would recommend doing this instead (it will bypass false and nil values)…

if ItemCount(item, i) and ...

Basically, eliminate the ~= false from the line and it will only continue if true.
Edited on 03 March 2017 - 08:45 PM
Sewbacca #3
Posted 03 March 2017 - 11:32 PM
Okay, i think i didn't understand everything, but there is maybe a logical error.
Correct me if i am (probably) wrong.
slots = {
"I", "I", "I", "_",
"_", "_", "_", "_",
"_", "_", "T", "T",
"T", "T", "A", "C"}
If you run SlotCalculator("I"), you would always get 1 as a return value.

What does ItemCount()?
Edited on 03 March 2017 - 10:34 PM
Bomb Bloke #4
Posted 03 March 2017 - 11:56 PM
If this line is triggering an "expected number" error:

local TorchSlot = SlotCalculator("T")

… then that means it's following on from an incomplete line which could be continued using a number, but can't be continued with the "local" keyword.

If you still don't see the problem, post your full code.
BrunoZockt #5
Posted 04 March 2017 - 01:39 PM
You were doing it right…

local TorchSlot = SlotCalculator("T")

The only thing that stands out to me is this - you're only looking for false values, not nil…

if ItemCount(item, i) ~= false and ...

I would recommend doing this instead (it will bypass false and nil values)…

if ItemCount(item, i) and ...

Basically, eliminate the ~= false from the line and it will only continue if true.

The function can't return nil, so that can't be the problem.

Okay, i think i didn't understand everything, but there is maybe a logical error.
Correct me if i am (probably) wrong.
slots = {
"I", "I", "I", "_",
"_", "_", "_", "_",
"_", "_", "T", "T",
"T", "T", "A", "C"}
If you run SlotCalculator("I"), you would always get 1 as a return value.

What does ItemCount()?
It should always return 1 then, yes.
ItemCount() does the same as turtle.getItemCount() but it only returns a number when the correct item is in the Slot, otherwise it returns false.

If this line is triggering an "expected number" error:

local TorchSlot = SlotCalculator("T")

… then that means it's following on from an incomplete line which could be continued using a number, but can't be continued with the "local" keyword.

If you still don't see the problem, post your full code.

That line isn't triggering the Error, the Error gets triggered by every line using that variable, e.g. turtle.select(TorchSlot).

Full code: http://pastebin.com/fptxKN9n
Edited on 04 March 2017 - 12:41 PM
Dog #6
Posted 04 March 2017 - 01:43 PM
What do you get if you put a print statement right after local TorchSlot = SlotCalculator("T") and print the value of TorchSlot?

EDIT: OK, I *think* I've found at least part of the problem. In your itemCount function you have one instance in which you return 0, but your slotCalculator code discards 0 (only looks for greater than 0). If itemCount returns 0 then it looks to me like slotCalculator returns nothing (or nil).
Edited on 04 March 2017 - 12:47 PM
BrunoZockt #7
Posted 04 March 2017 - 03:23 PM
What do you get if you put a print statement right after local TorchSlot = SlotCalculator("T") and print the value of TorchSlot? EDIT: OK, I *think* I've found at least part of the problem. In your itemCount function you have one instance in which you return 0, but your slotCalculator code discards 0 (only looks for greater than 0). If itemCount returns 0 then it looks to me like slotCalculator returns nothing (or nil).

This can't be the problem neither because it should iterate again if it returns 0 and I made sure that there is at least one slot with the right item in it.
Bomb Bloke #8
Posted 04 March 2017 - 11:06 PM
Dog's diagnosis looks correct to me: if no "minecraft:torch" items are found in any of the slots marked "T", then SlotCalculator will never find a match, never return a number, and ends up returning nil instead. Since you never check to see if this has happened (if not FackelSlot then …), you later crash when you attempt to call functions which expect numbers.

I suggest having your ItemCount function print something out every time it's called - you want it to report which slot it's looking at, what it's looking for, what type of item it actually found, and what it's about to return. Then have your script halt immediately after calling SlotCalculator so's you can review the results.
BrunoZockt #9
Posted 04 March 2017 - 11:11 PM
Dog's diagnosis looks correct to me: if no "minecraft:torch" items are found in any of the slots marked "T", then SlotCalculator will never find a match, never return a number, and ends up returning nil instead. Since you never check to see if this has happened (if not FackelSlot then …), you later crash when you attempt to call functions which expect numbers.

I suggest having your ItemCount function print something out every time it's called - you want it to report which slot it's looking at, what it's looking for, what type of item it actually found, and what it's about to return. Then have your script halt immediately after calling SlotCalculator so's you can review the results.

I did what u suggested and SlotCalculator() returned nothing, not nil, simply nothing. But as said, this Error even occurs when there are torches in the right slots. So there must be another mistake.
I'm going to check what exactly the ItemCount() function does tomorrow (12 hours) and will report if I find out anything new.
Edited on 04 March 2017 - 10:19 PM