Easy-To-Use ComputerCraft Support?

Discussion in 'Mod Discussion' started by Reika, Feb 26, 2014.

  1. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    I am adding ComputerCraft support to my machines, and I have a set of functions already callable on them. However, the calling and the received data...their formatting leaves something to be desired. Is there a better way to do this?
    [​IMG]
     
    1SDAN, Eyamaz and Pokefenn like this.
  2. casilleroatr

    casilleroatr New Member

    Excited about the computercraft support for your mods!

    Not sure exactly what you mean with your question so I am making an assumption. Feel free to ignore if the assumption turns out wrong (in which case what is wrong with the formatting?)

    You can always wrap your peripherals like so (assuming its on the right hand side of the computer for now). Makes it easier to call peripheral functions.

    Code:
    local rotaryMachine = peripheral.wrap("right")
    
    local slotData = rotaryMachine.getSlot(0)
    --is the return a table or a string?
    for i,v in pairs(slotData) do print(i..": "..v) end
    
    That might not work properly inside the Lua shell (putting it in a do end block might help there though)

    [/assumption]
     
  3. EnzymeA113

    EnzymeA113 New Member

    Your mods are getting crazier and crazier. Keep it up.
     
  4. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    It returns a table. In the case of getSlot, it is [itemID][metadata][stackSize][displayName]. My concern is, how will a program actually parse and use this?
     
    Last edited: Feb 26, 2014
  5. casilleroatr

    casilleroatr New Member

    I'm not yet familiar enough with your mods to know what sorts of programs one might make for rotarycraft blocks. But the stackSize and displayName variables will most likely be very useful. So long as the keys in the table have decent, consistent string names it will probably be really easy to use. I have used similar openperipherals methods that return a very similar table to your getSlots method, and I have had no problem using those and some of the programs I made were quite useful, so I expect there will be plenty of uses for your methods.
     
  6. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    Keys in the tables...? Tables are basically arrays, correct?
     
  7. casilleroatr

    casilleroatr New Member

    Yes. Something ideal for the table returned by getSlot written out in Lua would look like this

    Code:
    slotReturn = {
      ["id"] = id,
      ["metadata"] = metadata,
      ["stackSize"] = stackSize,
      ["itemName"] = itemName,
      }
    
    Parsing that would be fine.
     
  8. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    How do I do that? All the CC API asks me for is to return an array of objects, in this case three integers and a string.
     
  9. casilleroatr

    casilleroatr New Member

    I'm sorry I don't know I haven't made a mod, just someone who likes to play. Maybe someone from the open peripherals team could help because some of their table returns have string keys.
     
  10. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    This is though CC directly, but OK.
     
  11. casilleroatr

    casilleroatr New Member

    Sorry, do you mean you want to use the Lua in computercraft to look through the table in a computercraft program? If so then it is done this way. Just make a new program and do something like this

    Code:
    local  rotaryCraftMachine = peripheral.wrap("right")
    local slotData =  rotaryCraftMachine.getSlot(0)
    
    --print the whole table out by key and value using generic for loop. Key is stored in i and value in v
    for i,v in pairs(slotData) do
      print(i..": "..v) -- .. is concatenate in Lua. Will print out something that looks like this, itemName: steel, but for each key value pair in the table
    end
    
    --store the different parts of the table as variables (assuming key names are what I put in the previous post. They can obviously be corrected.) Keys go inside square brackets
    
    local slotID = slotData["id"]
    local slotMeta = slotData["metadata"]
    local slotStack = slotData["stackSize"]
    local slotItem = slotData["itemName"]
    
    --You can do whatever you want with those variables.
    
    I thought you were asking for about the api for modders to add peripherals to computercraft which I know nothing about
     
  12. kittle

    kittle New Member

    If your just returning an array of objects.. return a 2 column array
    {
    [ "itemID", 30761],
    [ "metadata", 42],
    ["stackSize", 64],
    ["displayName", "Yeast"]
    }

    This has the nice feature to be (almost) infinitely extensible so you dont have to revamp your API if you want to return more or less items
     
  13. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    Now to see if CC accepts this as a valid return.

    Answer: No.
    Code:
    computercraft: Could not convert object of type [Ljava.lang.Object; to LuaValue
    
     
    Last edited: Feb 27, 2014
  14. CaseyRobinson

    CaseyRobinson New Member

  15. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    Again, this is CC directly. Also, the javadoc says this:
    Code:
    Returns:
    An array of objects, representing values you wish to return to the lua program.
    Integers, Doubles, Floats, Strings, Booleans and null be converted to their corresponding lua type.
    All other types will be converted to nil.
    You may return null to indicate no values should be returned.
    
    
     
  16. Niels Henriksen

    Niels Henriksen New Member

    Is it your mod that need to return data to CC so the user can read it? Then you just make an array and in each field in the array you put the object. Your object can contain all datatypes that CC accepts. You dont need to use Lua in your mod.
     
  17. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    Yes, I know that - that is what I am doing. The problem is one of usability of the data. A pile of unlabeled numbers is not user-friendly.
     
  18. Niels Henriksen

    Niels Henriksen New Member

    Then you need to make one more property in your object that tells what this is (like Name or something like that). An API is not supposed to be user-friendly becuase its only data.
     
  19. Reika

    Reika RotaryCraft Dev FTB Mod Dev

    ??
    Are you suggesting alternating the data with Strings that label them? That is no more user-friendly.
     
  20. Niels Henriksen

    Niels Henriksen New Member

    Im not so experinced in Java yet and are only now learning to make MC mods but there is what I will do (psodu code and I will expect it to be like this)

    a = new array()
    b = new class_of_object()
    b.itemID = 30761
    b.metadata = 42
    b.stackSize = 64
    b.displayName = "Yeast"

    a.append(b)

    Then you only need to return a (the array) to CC. CC will then in Lua return

    {
    [ "itemID", 30761],
    [ "metadata", 42],
    ["stackSize", 64],
    ["displayName", "Yeast"]
    }
     

Share This Page