Lua conventions

From OpenComputers: Rebooted
Revision as of 08:08, 24 August 2026 by imported>OCDoc Import (Imported from legacy OpenComputers documentation at ocdoc.cil.li)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lua Code Conventions

  • Be consistent.
  • Indent using spaces, two wide. Do indent properly.
  • Try to limit width of the code to 80 chars.
  • Don't add spaces between braces/brackets and what's in them.
  • Do use brackets in functions calls even if it's not necessary.
  • Name variables for what they are, don't include type markers in them, i.e. do not use Hungarian notation.
  • Nice to have: sort your requires alphabetically (OCD!)
  • Feel free to comment your code. Any additional memory needed to load your file due to heavy use of comments is released soon after loading is complete.
  • If you have to validate arguments, use the built-in checkArg method. Homogenous error messages are a good thing! Its usage is checkArg(n, value, type1, ...), where n is the number of the argument, value is the value of the argument and type1 and so on are the allowed types for the argument, as retrieved via type(value). The number is used in the error message like so: "bad argument #n (type1 expected, got type(value))". So for example, to require the first argument to be a number you'd do checkArg(1, arg, "number").

Bad Code:

function f(sArg1 , ... )
   assert(type(sArg1)== "string", "me wants a strign!")
    if sArg1 then
        local nResult = 1
        --do some more stuff
        return nResult
  end
end
if  f ( "a" )  ==1 then
print"asd"
end

Good Code:

function f(name, ...)
  checkArg(1, name, "string")
  if name then
    local result = 1
    -- We extrapolate the b-spline of the non-euclidean space to
    -- determine the fraction of potential failures encountered.
    return result
  end
end
if f("a") == 1 then
  print("asd")
end

Contents

⚠ We are currently in the process of unifying the separate block/item pages and component API pages together for ease of navigation. If you find the dedicated component API page is missing, check the item page. Thank you for your patience during this transition. ⚠


Links
OpenComputers
General Install and Use OpenOS - Crossmod Interoperation - Lua Conventions - Computer Users
Blocks
Computers Computer Case - Server Rack - Microcontrollers
Display Hologram Projector - Keyboard - Screen
Power and Networking Capacitor - Net Splitter - Power Converter - Power Distributor - Relay
Extensions Adapter - Cable - Disassembler - Disk Drive - Geolyzer - Motion Sensor - Redstone I/O - Transposer - 3D Printer
Robotics Assembler - Charger - Drone - Robot - Waypoint
Items
Components Data Card - Debug Card - Graphics Card - Internet Card - Linked Card - Network Card - Redstone Card - Wireless Network Card
Storage Devices Floppy Disk - EEPROM - Hard Disk Drive - Memory
Upgrades Angel Upgrade - Battery Upgrade - Chunkloader Upgrade - Database Upgrade - Experience Upgrade - Generator Upgrade - Hover Upgrade - Inventory Controller Upgrade - Inventory Upgrade - Leash Upgrade - Navigation Upgrade - Piston Upgrade - Sign I/O Upgrade - Solar Generator Upgrade - Tank Upgrade - Tank Controller Upgrade - Tractor Beam Upgrade
Containers Card Container - Upgrade Container - Disk Drive
Others Analyzer - Texture Picker - Component Bus - CPU - APU - Drone Case - Manual - Microcontroller Case - Nanomachines - Remote Terminal - Terminal Server - Server - Tablet Case - Tablet - Crafting Materials - Ink Cartridge - Scrench - Hover Boots - Grog - Loot Disks
APIs
OpenOS buffer - colors - component - computer - event - filesystem - uuid - internet - keyboard - note - process - rc - robot - serialization - shell - sides - term - text - thread - transforms - unicode
Lua Libraries coroutine - package - io - os
Components
Core Components Access Point - Computer - Drone - Drive - EEPROM - Filesystem - GPU - Microcontroller - Modem - Redstone - Robot - Transposer
Others Component Access - Signals
Cross-Mod Integration Applied Energistics
Addon Mods
Addons Computronics
Supporting OpenComputers Thut's Concrete - HBM's Nuclear Tech Mod
Tutorials
Mod Specific Basic Computer - Writing Code - Hard Drives - Autorun and Startup scripts
Modding Custom Architectures - IMC Messages
Programs OPPM - install
Others Custom Operating Systems
Other Languages Page
Simplified Chinese 简体中文(Simplified Chinese)