1. Post #1
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    I'm doing a little coding for the ComputerCraft mod on minecraft and I creating a program. Programs are ran by the LUA API therefore LUA does apply.
    Anyhow, I want to handle rednet.receive( )'s Returns, which are 'senderID' and 'message', which we be presented using the print() code, however, it returns nill and causes a force close on my program. To give an idea what the 'rednet' code does is allow rapid redstone (aka, wiring) pulses to send/read data.
    rednet.receive( timeout ) -- Returns: senderID, message
    Wiki-page
    http://computercraft.info/wiki/index...e=Rednet_(API)

    Edited:

    Oh dear, what has facepunch changed this time.


    New problem


    Script is complete and done!
    Get the script for Minecraft: ComputerCraft!
    http://cc-get.djranger.com/scripts/view/49

  2. Post #2
    Andrew McWatters
    Dennab
    March 2011
    4,658 Posts
    the first step in handling Lua returns is calling it Lua and not LUA so you don't look ignorant

    the next step is to deal with the values on the stack, if you're working with the Lua C API, that means you're dealing with the stack directly

    you are working with the C API, correct?
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 2Funny Funny x 2 (list)

  3. Post #3
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    the first step in handling Lua returns is calling it Lua and not LUA so you don't look ignorant[SUP][1][/SUP]

    the next step is to deal with the values on the stack, if you're working with the Lua C API, that means you're dealing with the stack directly[SUP][2][/SUP]

    you are working with the C API, correct?[SUP][3][/SUP]
    DO NOT BAN PEOPLE IF THEY SAY IF THEY WANTED IT INDENTED, I HAVE UPDATED THE CODE FOR INDENTS!

    1. -mis-interperation-.
    2. Can you elaborate? I'm still new to the Lua coding
    Here's the code I'm working on.
    print("Loading Program...")
    print("Loading Sub-Programs...")
    function broadcast()
    	print("What would you like to broadcast?")
    	bmesg = read()
    	rednet.open("back")
    	rednet.broadcast(""..(bmesg).."")
    	print("Broadcasted ["..(bmesg).."] to all computers")
    	subend()
    end
    print("Loaded Broadcaster...")
    function private()
    	print("What computer? (Computer ID?)")
    	id = read()
    	print("Message?")
    	pmesg = read()
    	rednet.open("back")
    	rednet.send((id), (pmesg))
    	print("Sent ["..(pmesg).."] to computer ["..(id).."]")
    	subend()
    end
    print("Loaded Private Messager...")
    function announcer()
    	print("Press enter to annouce")
    	nope = read()
    	rednet.open("back")
    	rednet.annouce()
    	print("Annouced")
    	subend()
    end
    print("Loaded Announcer")
    function get()
    	print("How long would you like to wait for a message?")
    	timeout = read()
    	rednet.open("back")
    	rednet.receive((timeout))
    	print("Message came from computer ["..(senderID).."] with the message ["..(message).."]")
    	subend()
    end
    print("Loaded Message Getter..")
    print("Loaded Programs")
    print("Loading Menus")
    function main()
    	print("P2P Chat V2 Loaded.\nCreated By JM Co. Computers\n1. Get a message\n2. Send a Message\n'quit' to quit.")
    	mainselect = read()
    	if mainselect == "1" then
    		get()
    		elseif mainselect == "2" then
    			messagemenu()
    		elseif mainselect == "quit" then
    	end
    end
    function portmenu()
    	print("1. Open a port\n2. Close a port")
    	portselect = read()
    	if portselect == "1" then
    		portopen()
    		elseif portselect == "2" then
    			portclose()
    	end
    end
    function messagemenu()
    	print("1. Private Message\n2. Broadcast Message\n3. Announce (No message)")
    	messelect = read()
    	if messelect == "1" then
    		private()
    		elseif messelect == "2" then
    			broadcast()
    		elseif messelect == "3" then
    			announcer()
    	end
    end
    print("Loaded program, opening menu")
    	function subend()
    	main()
    end
    main()
    
    Trouble code
    function get()
    	print("How long would you like to wait for a message?")
    	timeout = read()
    	rednet.open("back")
    	rednet.receive((timeout))
    	print("Message came from computer ["..(senderID).."] with the message ["..(message).."]")
    	subend()
    end
    
    'rednet.open()' opens the side of the block to allow sending/receiving data, this case the back, and only accepts string.

    'rednet.receive()' will start listening for a ping to start data transfer, will only accept numbers to set how long to wait till it gets a ping.
    'senderID' Is one of the returns that shows the ID of the computer
    'message' self explanatory.
    Also, anyone know how to detect if a value is nill (I know it means no data) so I can run a other print() command if it receives no data.

    3. Again, still new.

  4. Post #4
    Andrew McWatters
    Dennab
    March 2011
    4,658 Posts
    You are not working with the Lua (C) API, you are working with Lua.

    Please indent your code, as I can't be bothered to read your code if you can't bother to properly indent blocks or space out functions.

    (User was banned for this post ("why are you replying if you can't be bothered to help" - garry))
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 2 (list)

  5. Post #5
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    You are not working with the Lua (C) API, you are working with Lua.

    Please indent your code, as I can't be bothered to read your code if you can't bother to properly indent blocks or space out functions.
    Sorry, how the program interperates the Lua code, I didn't bother, but I could fix it.

    !Fixed!

  6. Post #6
    Gold Member
    subenji99's Avatar
    May 2005
    3,572 Posts
    Also, anyone know how to detect if a value is nill (I know it means no data) so I can run a other print() command if it receives no data.
    if not variable then
        --code
    end
    
    --or
    
    if variable == nil then --if you want to distinguish nil from false
        --code
    end

  7. Post #7
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    if not variable then
        --code
    end
    
    --or
    
    if variable == nil then --if you want to distinguish nil from false
        --code
    end
    Thank you.

    Edited:

    (User was banned for this post ("why are you replying if you can't be bothered to help" - garry))
    Damn you Garry.
    He was our only help.

    Edited:

    Well, at-least its an hour ban.

    Edited:

    I see what happened, I didn't state the change to my code since he asked for it to be indented.

    Edited:

    Updated

    Edited:

    Found that Lua will still accept variables if they have not been asigned at all, stating that using just 'sendID' or 'message' will be considered variables, therefore, there must be a command to get the return value.

    Edited:

    Also, someone unban amc, there was a mistake on my side.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Friendly Friendly x 2 (list)

  8. Post #8
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    I still need help by the way
    Reply With Quote Edit / Delete Reply United States Show Events Friendly Friendly x 1 (list)

  9. Post #9
    Andrew McWatters
    Dennab
    March 2011
    4,658 Posts
    local senderID, message = rednet.receive(arguments)
    

    is what I believe you're looking for.

    My request for you to indent your code was not one that I was intending to come off as hostile or rude. I apologize if that may have been the case.

    It's simply that it would provide yourself and others a convenience to have nice, quickly readable code, which in the future may allow you to have your answers provided faster.

    Please tell me if the code above is confusing to you. The idea is that you're provided values back from a function, and you store them in variables in the order they're returned.

    The function you have mentioned apparently returns two values (correct?), and they are senderID and message, in that order. We define the variables to be used, then reference them in print().

    Edited:

    Your final fixed code may end up looking like this:

    function get()
        print("How long would you like to wait for a message?")
        timeout = read()
        rednet.open("back")
        local senderID, message = rednet.receive(timeout)
        print("Message came from computer ["..senderID.."] with the message ["..message.."]")
        subend()
    end
    

    Edited:

    Please note that I do not have any knowledge of rednet, but can simply assist you by my knowledge of Lua. If you do have any errors that continue to be produced, dump them here and I or someone else will eventually assist.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Friendly Friendly x 1 (list)

  10. Post #10
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    Long Post
    Thank you really much, I will update the code with yours as once I get on my computer, since I'm on my phone at the the time (Using the FacePunch mobile app).

    Edited:

    Adding to my last reply since editing is not implented on facepunch mobile.

    To give you an idea of what I was handling with, here is this.

    RedNet is a preinstalled API that comes with the ComputerCraft mod to allow the transfer of text to
    A. Simply text
    B. Make a live game
    C. Send programs/files

    ComputerCraft is a programable computer using the Lua API to create programs to...well anything. From games to handling large scale factories to practical devices.

    My program's main objective is to send and receive messages from one computer using the RedNet API.
    The thing about the RedNet API is the fact that if you sent a message using the private function with provided computer ID, it will only get to that computer with the same ID that the message was set to send to.

  11. Post #11
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    Ok, my computer is going down into Indy because I left my computer in my dads car, so I guess I have to wait for my computer to come back to I can fix that code.
    Reply With Quote Edit / Delete Reply Mac United States Show Events Funny Funny x 2Friendly Friendly x 1 (list)

  12. Post #12
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    Been lazy updating that code...Fuck my laziness.
    You know what, I will update it now.
    (Also an excuse to bumping this so I can switch computers to update it.)

    Edited:

    Updating code...

    Edited:

    Updated!
    Running

    Edited:

    Problem with sending command, debugging.

    Edited:

    Code was outdated, updated

    Edited:

    Problem, the send command
    function private()
    print("What computer? (Computer ID?)")
    id = read()
    print("Message?")
    pmesg = read()
    rednet.open("back")
    rednet.send((id), (pmesg))
    print("Sent ["..(pmesg).."] to computer ["..(id).."]")
    subend()
    end
    
    Is saying that it was expecting a positive number. What can I do to make the read() command to be interpreted as a number?

    Edited:

    OK, tested the receive command with the broadcast.
    IT WORKED!
    Still need that private messager fix.

    Edited:

    Hmm...next step, servers. So multiple messages can be processed.

  13. Post #13
    Gold Member
    subenji99's Avatar
    May 2005
    3,572 Posts
    id = tonumber(read())

    Note that this is not ideal, you should be doing some sort of sanity checking to make sure that the received id is valid - but anyways that function will convert a string to a number.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Agree Agree x 1 (list)

  14. Post #14
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    id = tonumber(read())

    Note that this is not ideal, you should be doing some sort of sanity checking to make sure that the received id is valid - but anyways that function will convert a string to a number.
    Thanks

  15. Post #15
    ─ ─ ─ ─ ─▀ ▀ ▀ ▌ ─ ─ ─ ▄ ▄ █ █ ▌ █ ░ ♥ ░ HONK HONK- ITS A TRUCK ░░ ♥ ░░ ░ ░ ▐ ▄ ▄ ▄ ▌ ▐ █ █ ▌ █ ░ ♥ ░ FULL OF DICKS ░░░ ░ ♥
    Jarrod's Avatar
    December 2010
    1,082 Posts
    works like it should, the script is now completely debug and functioning with out problems (Unless you do something that you logically shouldn't do). Time to update the CC-Get code.

    Edited:

    Get the program here!
    http://cc-get.djranger.com/scripts/view/49