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

Harkole

Members
  • Content Count

    12
  • Joined

  • Last visited

  • Days Won

    2

Posts posted by Harkole

  1. The only thing I can suggest at the moment is to extend your sleep time, it's not exactly accurate and short sleep times maybe missed, try increasing it to 10 or something longer than 3 at any rate and see if it works then.

  2. Hey Ash,

     

    The nil result suggests the robot failed to move for some reason, the explanation of move from the Robot API reads:

     

    Returns: true if the robot successfully moved, nil otherwise. If movement fails a secondary result will be returned describing why it failed, which will either be 'impossible move', 'not enough energy' or the description of the obstacle as robot.detect would return.
    The result 'not enough energy' is rarely returned as being low on energy usually causes the robot to shut down beforehand.
    The result 'impossible move' is kind of a fall-back result and will be returned for example if the robot tries to move into an area of the world that is currently not loaded.

     

     

    I've not done anything with Robots yet so Molinko maybe a better person to help you, but in an attempt to keep you moving forward with your problem, I think it's returning "nil" because it can't move, it maybe worth catching the second returned value and checking if it has a value?

    -- catch movement errors
    success, reason = robot.back()
    
    -- debug output
    if(success) then
      print("I moved back")
    else
      print("I can't move because "..reason)
    end
    

    Hope that helps move you forward so to speak!

  3. You have a couple of problems here, the first isn't a major but worth noting:

     

    1. Without cheating this will only work in creative (debug card is not available to craft so you can't get the world co-ordinates, although I'd love to be corrected on this)

     

    2.

     

    You are referring to the ability to perform path finding, the most common is A* Pathfinding (and is possible in Lua). The problem however is that you need to move the robot, detect which direction to go next, detect if it can go in that direction then move and repeat until you find a situation you are in where you can only move back. As this is extremely time consuming if you're planning on having the robot do this from anywhere in the world, it may find it's self stuck in a cave dead end for a very long time before it works it's way back. Additionally you'd need to keep the chunk loaded while it worked out the route.

     

    I've done a quick Google and there are a lot of examples of "Lua A* Pathfinding" which should get you going however  :) I've not tried this in Lua but based on a the Google results there is nothing stopping this from working with some success.

     

    To fix your code you've put the closing bracket on the function in the wrong place, set up a function like this:

    function move()
      -- place code here to do when calling move()
    end
    
    -- Call move
    move()
    

    Also I'd recommend changing the way you handle your variables for home to something like, it allows you to give new home co-ordinates by manipulating the x, y and z values, whilst not needed for your example code above it may prove useful in A*:

    local x = ##
    local y = ##
    local z = ##
    
    function move(x, y, z)
      -- do movement stuff
    end
    
    move(x, y, z)
    
  4. It's all about scope, or better memory management. If you are only doing a small program it doesn't matter but good practice is important.

    Lua code runs in blocks, local variables are only available in the current block, a global variable is always available. What this means in terms of memory is a global variable, once created will sit around in memory until the program ends or you set the variable to nil.

    The Lua manual explains this with examples but I am posting from my phone so don't have access to the link at the moment.

    Here is the link to what I am explaining: http://www.lua.org/pil/4.2.html

  5. Sleep is a wonderful thing! - The answer to your question is Concatantaion, in Lua it is very simple to do here is an example:

    local word = "a string of text"
    local num = 5
    
    print(word..num)
    
    -- to have fixed text just do
    print("This is always the same text, with the number stored in num "..num)
    

    Because when I first saw this post I was half asleep and not paying attention below this line, in the spoiler, is a semi solution. To see my original post with a full example of what I'd try to do for you issue click the spoiler button - if you want the challenge of doing it your self (always more rewarding) then don't!  :D
     


    Not sure what the question is but based on the Wiki you forgot to open the port to broadcast on:
     

    -- Hook components
    -- (the Wiki has the event too but that is for the message receive part so i dropped it)
    local component = require("component")
    local m = component.modem -- get primary modem component
    
    local time = 5 -- time to wait between ping messages
    local count = 1 -- intialise count to 1
    
    -- Open the port on the network and print out if it is open (should print true or this wont work)
    m.open(123)
    print(m.isOpen(123)) -- true
    
    -- Do a ping test (sort of)
    
    while count < 5 do
        m.broadcast(321, "ping "..count)
        count = count + 1
        os.sleep(time)
    end
    
    -- close the network port
    m.close()
    

    Your receiving server will just simply listen with the event system... I don't really know why you'd want to do this but it's what you asked.
     

    -- Hook the Events library
    local event = require ("event")
    
    -- Wait for a message from another network card.
    local _, _, from, port, _, message = event.pull("modem_message")
    local data = tostring(message)
    
    -- check that the received event was a ping message, like ping 1 and do something
    if(data == "ping 1") then
        print "I received a ping, it was the first ping!"
    end
    

    Not tested this and it's currently 01:15 local time so I'm heading to bed, but hope this helps.

  6. OK done some testing (OC 1.5.6), nothing wrong with your program but two things I learned from this:

     

    Firstly, don't call your Lua script "redstone" it won't work and will give you instructions for using OC's redstone API  :P (ended up calling mine testRed.lua)

     

    Secondly, frequency 0 doesn't work, but anything else appears to be fine. The "os.sleep(int)" command needs to be longer as the computer isn't guaranteed to be processed at fixed intervals which can lengthen or shorten the live period. I set it to 5 and, baring Frequency 0, never had an issue.

     

    You must have a Redstone Tier 2 card installed in the computer and you don't need a wireless transmitter (the tier 2 card is this item in effect). A Redstone I/O block doesn't work.

  7. You need an IF statement around the sg,openIris() call, this statement needs to check the data you are sending in the sgMessageReceived validate it and if true open the iris.

     

    I'm not at home to test this code but based on the Wiki my limited experience with OC and LUA I can suggest the below to help you move forward.

     

    Without the broadcast code I've no idea what you've broadcast the "12345" text as, but if you read the wiki here: http://ocdoc.cil.li/component:modem you can see that it tells us your string will be one of the parameters sent at the end of the standard list. Simply check the parameter against what you want it to be or assign it to a variable called password for example I will assume the Wiki's example but with your required password:

     

    Example Broadcast code (direct from the Wiki page and altered to send your password string 12345)

    -- Hook components
    -- (the Wiki has the event too but that's for the message receive part so i dropped it)
    local component = require("component")
    local m = component.modem -- get primary modem component
    
    -- Open the port on the network and print out if it is open (should print true or this wont work)
    m.open(123)
    print(m.isOpen(123)) -- true
    
    -- Send the password string, this could be a variable with your user input but for
    -- now use a hard coded value, this has the weakness of if someone gets your device
    -- they can use it to open your gate!
    m.broadcast(321, "12345")
    

    The receiving computer would have this to be able to get the message:

    -- Hook the Events library
    local event = require ("event")
    
    -- Wait for a message from another network card.
    local _, _, from, port, _, message = event.pull("modem_message")
    password = tostring(message)
    

    Once you've got that you'd simple check that the password matched the value you want and at that point open the Iris with something like:

    IF(password == "12345")THEN
        sg.openIris()
        -- Maybe send back confirmation the iris is open?
    END
    

    Just food for thought, you'd probably want some way to know if the Iris has opened or not so you should send a message back to inform the user if they are safe or not as getting the password wrong would not open the iris and would most likely upset the travelers day!  :lol:

  8. Hi All,

     

    Hopefully this is in the right place and going to be helpful to someone! I've come from an older version of Computer Craft and Minecraft to 1.7.10 and OC and I've been brushing off the rust and trying to get things going. Having done the tutorials and messed around I thought I'd try doing a simple(!) tank reading program so I can check the fuel levels of my generator.

     

    Turns out that either my Google-foo is weak or that not many people have tried/done it. So below is how I did it with some explanations of techniques I used to work things out (so hopefully you can tell me a better way of doing it or I can save some blood/sweat/tears of someone following).

     

    First things first, there is a good tutorial on here and the Wiki about the computer so I'm skipping straight to the "connection of the tank" and code.

     

    The Adapter block must be connected to a valve, it also needs to have the Tank Controller Upgrade installed for the example to work.

     

    I did notice that you can actually see a RailCraft tank with no upgrade but it threw errors if I tried to access any of it's methods, if I work out where the log files that it said to check are I can forward them as bug reports...

     

    So, with the Adapter in place and attached by either cable or directly to the computer and OpenOS installed/loaded, we can use the following code in the LUA shell (type lua and press enter if you are at the start up prompt). Note the = proceeding the command is needed to see the results:

    -- list all components, with the unique address of said component (for specific attachment)
    -- and the name you can use as a component name (this may not be unique and the system will use the first it finds)
    =component.list()
    

    This, as the comments say will list all attached components of the computer, if there are a lot you may see "..." at the bottom, in which case use the Analyzer to get the unique address for the part in question (you can use the address or name, it's just easier to use a name for readability)

     

    Our Adapter with the Tank Upgrade becomes known as tank_controller, with this information we can call:

    -- List all methods/functions of the component
    -- component.<component name> 
    =component.tank_controller
    

    This will tell us what we can do with the tank_controller (or other component name), with this information we can actually use LUA to see what this information would be by calling it and also tell what format the data is coming back in...

    -- Example Connecting to a single tank on the south of the Adapter with a fluid upgrade in it
    -- this stores the result in a table called tInfo (south side connection)
    =component.tank_controller.getFluidInTank(3)
    
    -- would return a table with the key values:
    --		amount		->	current volume of fluid
    --		capacity	->	total storage available for fuilds in this tank
    --		hasTag		->	?
    --		label		->	In game name value of fluid
    --		name		->	??? Possibly the forge fluid name ???
    

    We can tell it's a table because of the {{}} symbols around the bulk of the data and that it's data pairs (someone with more LUA skill will explain this better I'm sure).

     

    So with all this science out of the way we can now create a simple LUA script to display just the information we want in a tidy way, below is the entire code commented to explain what we are doing. There are some key differences between using the LUA shell and a LUA script regarding talking to the component, but it's still quite basic:

    -- ********************************************************
    -- Example of simple tank reader (using Adapter with Tank Controller Upgrade)
    -- ********************************************************
    
    -- Hook the component
    component = require("component")
    tank = component.tank_controller
    
    -- Get the tank information, the side is the Forge value for sides (int), results are in table format
    tInfo = tank.getFluidInTank(1) -- Tank is above the Adapter
    
    -- Access the key values of the table (LUA is base 1 not 0 for first index values)
    -- We don't actually have to do this, but it helps if the code gets big to be clear, we could of actually
    -- just used the values straight in the print command.
    amount = tInfo[1].amount
    capacity = tInfo[1].capacity
    label = tInfo[1].label
    
    -- get the tanks fill value as a percentage
    -- this is another reason to assign things to variables, as this alone returns a value of up to 5 decimals places...
    percent = (tInfo[1].amount / tInfo[1].capacity) * 100
    
    -- ... but as we can pass the variable about to do things we can use other methods/functions to clean it up
    -- while still keeping the code readable
    -- format the percentage to a nice number
    math.floor(percent + 0.5)
    
    -- Output as you want... a very simple way:
    print("Fuel Type: "..label)
    print("Amount   : "..amount)
    print("Tank Cap.: "..capacity)
    print("% Full   : "..percent)
    

    Whilst it's not perfect, it'll hopefully serve as a bridging point in to the world of components not native to OC. There are other things we could of done like make a function of the check and passed it to a function for displaying then looped through the code so it updates the screen constantly but that's all for you guys to think about, this was just a brief example of where to start!

×
×
  • Create New...

Important Information

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