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
Code not working :(
#1
Posted 03 July 2013 - 11:08 PM
#2
Posted 04 July 2013 - 01:06 AM
#3
Posted 04 July 2013 - 05:08 AM
#4
Posted 04 July 2013 - 05:18 AM
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() endUsing 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.
#5
Posted 05 July 2013 - 11:40 PM
#6
Posted 06 July 2013 - 12:59 AM
xbsktball10x, on 05 July 2013 - 11:40 PM, said:
Also I did detail what the problem was with the code
theoriginalbit, on 04 July 2013 - 05:18 AM, said:
#7
Posted 06 July 2013 - 01:13 AM
#8
Posted 06 July 2013 - 01:52 AM
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
#9
Posted 06 July 2013 - 02:39 AM
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
#10
Posted 06 July 2013 - 02:56 AM
theoriginalbit, on 06 July 2013 - 02:39 AM, said:
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
#11
Posted 06 July 2013 - 03:15 AM
xbsktball10x, on 06 July 2013 - 02:56 AM, said:
items["item_name"] = item_quantity
xbsktball10x, on 06 July 2013 - 02:56 AM, said:
xbsktball10x, on 06 July 2013 - 02:56 AM, said:
#12
Posted 06 July 2013 - 03:18 AM
#13
Posted 30 July 2013 - 08: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?
#14
Posted 31 July 2013 - 01:37 AM
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...
#15
Posted 04 August 2013 - 01:35 AM
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)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users