Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
yesiwear.pants

attempt to index upvalue

Question

I wrote a simple tower building script/program, it works, but then I thought " I'm going to use this code a lot" so I started an Inventory.lua filled with functions no scripting of its own.

 

If I run this I get attempt to index upvalue 'Inventory' (a boolean value)

 

Inventory.lua

local robot = require ("robot")

slot = 1

function nextFull()
if robot.count(slot) == 0 then
  slot = slot + 1
  robot.select(slot)
end
end

build.lua

local tArgs = {...}
local length = tonumber(tArgs[1])
local width = tonumber(tArgs[2])
local height = tonumber(tArgs[3])
local robot = require ("robot")
local Inventory = require ("Inventory")
local slot = 1

toggle = true


print ("Enter Length for Structure")
length = io.read()
print ("Width?")
width = io.read()
print = ("Height?")
height = io.read()

length = length - 1
width = width - 1

robot.select(1)

function block()
  if toggle == true then
    for i=2,length do
      Inventory.nextFull()
      robot.placeDown()
      robot.forward()
      toggle = false
    end
  else
    for j=2,width do
      Inventory.nextFull()
      robot.placeDown()
      robot.forward()
      toggle = true
    end
  end
end

function row()
block()
robot.turnRight()
end

function layer()
  for k=0,3 do
    row()
  end
end

layer()
for l=1,height do
  robot.up()
  layer()
end

robot.turnRight()
robot.back()
robot.back()
robot.select(16)

for m=1,height do
  robot.down()
  robot.place()
end
Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0
  • Solution

Do you mean attempt to index 'slots'? The value Inventory does not exist in your code. 

 

If this is the case, then the problem is that your library doesn't return the function you define. The module system in OpenComputers differs from that in ComputerCraft, where

this would be valid. In OpenComputers, the module is expected to either return something, or to place something in package.loaded[modulename]. This is then returned to the requiring file. If neither of this happens, require simply returns true, to indicate that the file was found and loaded, which results in the variable slots in your code being a boolean. 

 

There are two ways to fix this problem. The first way is to leave Invertory.lua(slots.lua if my guess is right) the same, and to call the function nextFull directly. This should be possible since you placed it in the global environment. This happens, but is in my opion a sloppy solution, since you dump all your variables in the global environment. 

 

A better approach would be to return the function nextFull from your module, either directly or in a table. That would lead to the following code:

local robot = require ("robot")

slot = 1

local function nextFull()
  if robot.count(slot) == 0 then
    slot = slot + 1
    robot.select(slot)
  end
end

return {nextFull = nextFull}

This way you can add new functions to the module later on. Now you won't have to change the way build.lua works, and your module can be extended with other functions later on. 

Link to post
Share on other sites
  • 0

I apologize that was a derp on my part. yes the file is currently named slots. I made a copy named Inventory and forgot to change build.lua

slots is still there for now

and as a side note Ive never used CC

 

Edit : that worked thank you

          had to restart the robot to take effect I guess

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.