Skip to content

Power Section Documentation

Overview

The Power section in the cee API provides functions for interacting with and managing power sources within a ComputerCraft/CC:Tweaked environment. It includes methods to check if a computer has power, determine the current power level, and manage external power sources such as batteries or power cells.

This section is essential for developers working on energy-sensitive applications or systems that must react to changes in available power. The functions provided allow for fine-grained control over how programs behave depending on whether sufficient power is present.


Calling Conventions / Prefixes

All methods within the Power section are accessed through the global cee namespace, meaning they can be invoked directly as:

local hasPower = cee.hasPower()
local currentPower = cee.getPower()

These functions do not require any object instantiation and operate on the current computer's power state. They should not be confused with methods that are part of other sections like speaker, disk, or graphics, which may have their own prefixes or objects.


API Reference Table

Signature Arguments Description
cee.hasPower() None Returns true if the computer currently has power; otherwise false.
cee.getPower() None Returns the current power level as a number (in RF or equivalent).
cee.setPowerLevel(level) level (number) Sets the computer's internal power level to the specified value.
cee.isPowered() None Alias for hasPower(). Returns true if powered, otherwise false.

Lua Code Examples

Example 1: Check if Computer Has Power

if cee.hasPower() then
    print("Computer is powered.")
else
    print("No power available.")
end

Example 2: Get Current Power Level

local powerLevel = cee.getPower()
print("Current power level:", powerLevel)

Example 3: Set Internal Power Level (for simulation or testing)

⚠️ This function may not work in all environments or versions. Use with caution.

cee.setPowerLevel(1000) -- Sets internal power to 1000 RF units

Example 4: Monitor Power Status in a Loop

while true do
    if cee.hasPower() then
        print("System operational.")
    else
        print("Low power! Shutting down...")
        break
    end
    os.sleep(1)
end

Example 5: Use isPowered() as an Alias

if cee.isPowered() then
    print("Power is on!")
else
    print("Power is off.")
end

Notes

  • The cee.hasPower() and cee.isPowered() functions are equivalent.
  • The setPowerLevel() function may be used for testing or simulating power states in controlled environments but might not reflect real hardware behavior.
  • Power-related functions are useful when building energy-efficient programs, especially those involving long-running tasks or multiple peripherals that consume significant amounts of power.