rednet.open("top")
local items = {}
local amount = {}
local place = ""
local oldplace = ""
function receive()
local senderId, message, distance = rednet.receive()
local senderId2, message2, distance = rednet.receive()
print("message received")
for k,v in ipairs(items) do
if message == v then
senderId, message, distance = rednet.receive()
oldplace = amount[k]
place = amount[k] + message
table.remove(amount, oldplace)
table.insert(amount, oldplace, place)
elseif message ~= v then
table.insert(items, message)
table.insert(amount, message2)
print("inserted stuff")
end
end
for i = 1,#items do
print(items[i])
end
for i = 1,#amount do
print(amount[i])
end
print("function done")
receive()
end
term.clear()
term.setCursorPos(1,1)
receive()
Im trying to set up an inventory system and this code is suppose to check to see if the name of the block is in the table and then if it is it will then take the amount of that block and add the new amount to it then remove the amount from the table and insert the new amount but it wont do that it skips from line 9 to line 29 and idk why
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Code not working :(
Started by xbsktball10x, 03 July 2013 - 10:08 PMPosted 04 July 2013 - 12:08 AM
Posted 04 July 2013 - 02:06 AM
Can you please post the code of the program that is sending the information?
Posted 04 July 2013 - 06:08 AM
Turtles are blind, how do you get item types?
Posted 04 July 2013 - 06:18 AM
As Shazz asked, can you please post all related code to the problem, also if there are any error messages at a later stage post the exact message.
As for why lines 9-29 don't run that is because there is nothing in the items table, thus the loop never needs to run, so it doesn't. You will need to populate the table on line 2, not just create it.
Also as opposed to recursively calling receive i.e.
To expand on what I said previously recursion is bad to use as it can quite rapidly fill up the function stack. The function stack contains all the functions that have been called since the stack was created, and in CC this stack has a size limit of 256 entries. A stack is first in last out (FILO) meaning that has we call each function it gets pushed onto the stack, and must be popped off the stack before we can get to the entry below it. This is when a function completes it is popped off the stack so the previous function can continue from where it was. So knowing this when we call 256 functions (within one another) we will get a stack overflow. To avoid this we must allow the function to return, where possible, before calling another function.
To put a real world example for a stack. Lets think of a bunch of books. You're reading book 1 when it suddenly tells you that you must start reading book 2 before continuing. So you put book 1 aside (push onto the stack) and open book 2. As you're reading through book 2 it tells you that you must go to book 3 and read that book before continuing, so you put that book on top of book 1 in the pile (push book onto the stack) and open book 3. This process continues for another 4 books, which each in turn you put on the top of the pile. Once you have completed book 7 you close it and take book 6 off the top of the pile (pop off the stack), you then continue to read book 6 to the end, close it, and take book 5 off the pile on continue to read this book to the end. This repeats until eventually you're back to book 1 and the pile (stack) is empty. Now a stack overflow, think of that as you have too many books in the pile and it falls over. Also of course, if you're reading book 23 and then suddenly want to read book 4, well you cannot do that without first taking each book off the pile until you get down to book 4. The same rule applies to stacks.
As for why lines 9-29 don't run that is because there is nothing in the items table, thus the loop never needs to run, so it doesn't. You will need to populate the table on line 2, not just create it.
Also as opposed to recursively calling receive i.e.
function receive()
--# some code
receive()
end
receive()
you should use loops
function receive()
--# some code
end
while true do
receive()
end
Using the recursive way your program will error after ~256 times of running with a stack overflow. Using loops is the way to not have this happen.To expand on what I said previously recursion is bad to use as it can quite rapidly fill up the function stack. The function stack contains all the functions that have been called since the stack was created, and in CC this stack has a size limit of 256 entries. A stack is first in last out (FILO) meaning that has we call each function it gets pushed onto the stack, and must be popped off the stack before we can get to the entry below it. This is when a function completes it is popped off the stack so the previous function can continue from where it was. So knowing this when we call 256 functions (within one another) we will get a stack overflow. To avoid this we must allow the function to return, where possible, before calling another function.
To put a real world example for a stack. Lets think of a bunch of books. You're reading book 1 when it suddenly tells you that you must start reading book 2 before continuing. So you put book 1 aside (push onto the stack) and open book 2. As you're reading through book 2 it tells you that you must go to book 3 and read that book before continuing, so you put that book on top of book 1 in the pile (push book onto the stack) and open book 3. This process continues for another 4 books, which each in turn you put on the top of the pile. Once you have completed book 7 you close it and take book 6 off the top of the pile (pop off the stack), you then continue to read book 6 to the end, close it, and take book 5 off the pile on continue to read this book to the end. This repeats until eventually you're back to book 1 and the pile (stack) is empty. Now a stack overflow, think of that as you have too many books in the pile and it falls over. Also of course, if you're reading book 23 and then suddenly want to read book 4, well you cannot do that without first taking each book off the pile until you get down to book 4. The same rule applies to stacks.
Posted 06 July 2013 - 12:40 AM
Sorry guys i havent been on but let me maybe specify a little more. This is the server and i have one computer setup so that i can quickly input all the types of blocks that are in the inventory system. im currently working out the turtle part of this but basically i will sort the incoming blocks and items and put them into there correct turtles. the turtles will constantley check to see if any blocks have but put into it. If blocks have been entered it will count how many blocks it has then put them in a deep storage unit and then it will send a message of what chest it is. ie if its the stone chest it will send a message saying stone then how many it counted to be added. The server will then accept the message and run through the table until it finds the item. once it finds the position of that item. so kind of like a password and username login system it will then store the key that item is in the table at and then in that same position on the amount table it will add the new amount to the old amount and then delete the old amount and then add the new value back into that spot. so in theory it should keep track of how many blocks of each item i have. which i will then use this too put on a huge monitor and display for easy tracking.
Posted 06 July 2013 - 01:59 AM
So you will manually sort the blocks into turtles that are designed to receive only specific items? Or are you planning using the interactive sorter from MiscPeripherals?im currently working out the turtle part of this but basically i will sort the incoming blocks and items and put them into there correct turtles.
Also I did detail what the problem was with the code
As for why lines 9-29 don't run that is because there is nothing in the items table, thus the loop never needs to run, so it doesn't. You will need to populate the table on line 2, not just create it.
Posted 06 July 2013 - 02:13 AM
Im gonna sort the items using buildcraft pipes so only 1 item or block type will fill into the turtle. Thank you i tried that and now im working out more bugs lol but it helped alot thanks
Posted 06 July 2013 - 02:52 AM
Ok so here is a new version of the code and it is working a little bit better,
So now my problem is that the function runs perfect if you are adding amounts to the bricks. however, if i am trying to add to stone it doesnt add to it. instead it creates a new variable in the table called stone and even when i try to add to this new variable it still creates another. I switched stone and brick around and same results. If im not adding to the first one in the table then it skips to creating a new entry. Any thoughts? my assumption is that if checks the message == v part and doesnt move past the first spot in the table but i dont know why it wouldnt check the other values
rednet.open("top")
local blocks ={"brick", "stone"}
local amount = {1,1}
local function receive()
local senderId, message, distance = rednet.receive()
local senderId2, message2, distance2 = rednet.receive()
for i, v in ipairs(blocks) do
if message == v then
print(blocks[i])
message2 = (tonumber(message2))
local amounts = amount[i]
print(amounts)
amounts = (tonumber(amounts))
local newamount = (amounts + message2)
table.remove(amount, amounts)
table.insert(amount, amounts, newamount)
print(amount[amounts])
break
elseif message ~= v then
table.insert(blocks, message)
print("added".." "..message)
table.insert(amount, message2)
print("added".." "..message2)
break
end
end
end
while true do
receive()
end
So now my problem is that the function runs perfect if you are adding amounts to the bricks. however, if i am trying to add to stone it doesnt add to it. instead it creates a new variable in the table called stone and even when i try to add to this new variable it still creates another. I switched stone and brick around and same results. If im not adding to the first one in the table then it skips to creating a new entry. Any thoughts? my assumption is that if checks the message == v part and doesnt move past the first spot in the table but i dont know why it wouldnt check the other values
Posted 06 July 2013 - 03:39 AM
ok well instead of attempting to figure out why it is not working, I'm going to suggest a far easier way.
the easiest way is to actually use a table as a key/value pair not an indexed value. This means making the following change.
Then it makes the process of checking easier.
the easiest way is to actually use a table as a key/value pair not an indexed value. This means making the following change.
local items = {
["bricks"] = 0,
["stone"] = 0,
--# etc for the other values
}
Then it makes the process of checking easier.
local function receive()
local _, item = rednet.receive()
local _, qty = rednet.receive()
if items[item] then --# if the item name sent is in our table
items[item] = items[item] + qty --# add the quantity to the old total
else
print("unrecognised item: "..tostring(item))
end
end
Posted 06 July 2013 - 03:56 AM
local function receive() local _, item = rednet.receive() local _, qty = rednet.receive() if items[item] then --# if the item name sent is in our table items[item] = items[item] + qty --# add the quantity to the old total else print("unrecognised item: "..tostring(item)) end end
okay so would i add to the table the same way? also in the lines
local function receive()
local _, item = rednet.receive()
local _, qty = rednet.receive()
the "_", is that suppose to senderId, message, distance?
Thank you for all the help ive seriously been at this for days
Posted 06 July 2013 - 04:15 AM
Oh no. to add new item names you do thisokay so would i add to the table the same way? also in the lines
items["item_name"] = item_quantity
Well you're not using the senderId variable, so I just changed it's name to something shorter, and you're also not using the distance variable, so I excluded it… you don't need to define any of the variables at the end that you do not want, but you do have to define (in some way) the earlier variables, even if it is just a `_`the "_", is that suppose to senderId, message, distance?
No problems :)/>Thank you for all the help ive seriously been at this for days
Posted 06 July 2013 - 04:18 AM
nvm :)/> but ill give this a try!
Posted 30 July 2013 - 09:28 PM
rednet.open("top")
local monitor = peripheral.wrap("right")
local new = false
local items = {
["stone "] = 0,
["brick "] = 0,
["dirt "] = 0,
["wodndor"] = 0,
["cobble "] = 0,
}
monitor.clear()
monitor.setCursorPos(1,1)
local function receive()
local _, item= rednet.receive(1)
local _, qty = rednet.receive(1)
if items[item] then
new = true
items[item] = items[item] + qty
else
print("unrecognized item: "..tostring(item))
end
for k,v in pairs(items) do
if new == true then
monitor.clear()
monitor.setCursorPos(1,1)
end
monitor.write(k..":"..v)
local x,y = monitor.getCursorPos()
print(y)
print(x)
x = x - 9
y = y+1
if y == 28 then
x = x + 10
y = 1
end
monitor.setCursorPos(x,y)
end
end
while true do
receive()
end
Hey guys Im back and everything is seeming to work perfectly. However my issue now is im trying to make a board of monitors that displays how many items I have of each type. I have it set up so that when the turtle receives a new block it sends a signal to this server with the name of the block and then how many it has. it works perfectly but, it will not count correctly if alot are coming in. for example if i get a thing of cobble and a thing of stone at the same time it will be in the procceses of adding to the cobble and will miss the message from the stone turtle. any ideas on how to prevent this?
Posted 31 July 2013 - 02:37 AM
No the message should be queued in the event stack.
If you wish you could try this
which will pull a rednet message from the event queue (just like rednet.receive does). It shouldn't make a difference, but who knows…
Also I suggest that you move the messages into the same message, it will reduce the possibility of missing any rednet messages or something. You could do something like "cobblestone:64" and then split them by ':'…
example code:
When given say "Mossy Cobblestone:64" it will return the name "Mossy Cobblestone" and the count "64" as separate variables…
If you wish you could try this
local _, p = os.pullEvent("rednet_message")
which will pull a rednet message from the event queue (just like rednet.receive does). It shouldn't make a difference, but who knows…
Also I suggest that you move the messages into the same message, it will reduce the possibility of missing any rednet messages or something. You could do something like "cobblestone:64" and then split them by ':'…
example code:
local function getNameCount( str )
return str:match( "(.-):(/>/>/>%d+)" )
end
local name, count = getNameCount( fullMessage )
When given say "Mossy Cobblestone:64" it will return the name "Mossy Cobblestone" and the count "64" as separate variables…
Posted 04 August 2013 - 02:35 AM
Hello i wrote a program called "bj" that uses the new speakers to talk.
When i run my program it says, "bios:337: [string "bj" ]:3: unexpected symbol
What am i doing wrong?
also i posted here because i cant start new topics.
(i am also fairly new to cc)
When i run my program it says, "bios:337: [string "bj" ]:3: unexpected symbol
while true do
local event = os.pullEvent ("redstone")
if == "true" then
clear()
shell.run("speak", 'Welcome, Adventurer!")
What am i doing wrong?
also i posted here because i cant start new topics.
(i am also fairly new to cc)