Jump to content




Set variable as return of function

lua utility

8 replies to this topic

#1 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

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 by BrunoZockt, 04 March 2017 - 11:13 PM.


#2 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

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 by Dog, 03 March 2017 - 09:45 PM.


#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

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 by Sewbacca, 03 March 2017 - 11:34 PM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#5 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

Posted 04 March 2017 - 01:39 PM

View PostDog, on 03 March 2017 - 09:44 PM, said:

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.

View PostSewbacca, on 03 March 2017 - 11:32 PM, said:

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.

View PostBomb Bloke, on 03 March 2017 - 11:56 PM, said:

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 by BrunoZockt, 04 March 2017 - 01:41 PM.


#6 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

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 by Dog, 04 March 2017 - 01:47 PM.


#7 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

Posted 04 March 2017 - 03:23 PM

View PostDog, on 04 March 2017 - 01:43 PM, said:

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.

#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#9 BrunoZockt

  • Members
  • 56 posts
  • LocationGermany

Posted 04 March 2017 - 11:11 PM

View PostBomb Bloke, on 04 March 2017 - 11:06 PM, said:

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 by BrunoZockt, 04 March 2017 - 11:19 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users