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

OpenComputers for ComputerCraft users

Recommended Posts

 

Hello.

So you know how to make a program in ComputerCraft and you want to switch to OpenComputers or just want to know OC better, this tutorial shows you how to use OpenComputers.

0. Some concepts you have to know

  • You can think of Components like Peripherals in CC. They are basically the same
  • require is a function in Lua that returns a module. It can be compared to ComputerCrafts os.loadAPI. But require doen't load an API global like loadAPI, instead it returns the API and it doesn't return the _G table that the api defines. It returns what the loaded File returns like a function. More examples to that later in that Tutorial
  • A computer from OC has no OS and no BIOS. So a computer can't start or load something without an Lua EEPROM and a typical OS, with a fancy Text terminal and some programs, is also not available without installing OpenOS first. That makes setting up a OC Computer a bit more complicated but it allows you to make a real OS for your Computer and not just a fancy UI or a Overlay to the predefined Operating System.
  • An Address is used instead of the peripheral name and the ID in ComputerCraft. So your computer won't get the name "computer_0" or the id 0. The Address is a random UUID string and not a number that increments for every computer. So a Address looks more like "cd6c4e39-bbb6-491c-826d-48112912f44d". That can be a problem to remember, but components are usually uniquely identifiable by the first 3 characters of the UUID and later we will learn some other tricks to copy them.
  • You can't shutdown a Computer with Ctrl+S or restart it with Ctrl+R. You have to type "reboot" or "shutdown" in the console or press the green button in the UI for the Computer Case.
  • To interrupt a program in OpenComputers you press Ctrl+C or to forcefully kill it you can press Ctrl+Alt+C. In ComputerCraft you would press Ctrl+T and there is no way to forcefully kill a program in ComputerCraft other than restarting the computer.
  • A Computer doesn't restart when you leave the world or the chunk unloads. Instead they keep the memory and the program running on it won't notice that the computer stopped for a moment.
  • Some features are only available with cards. Like the Internet Card for HTTP and TCP (also Pastebin and Wget), Redstone Card for sending and receiving Redstone signals or a graphics card for connecting to a screen.
  • Memory is limited and the amount you can use is determined by the Memory Items you put into the Case
  • Your computer will use Power (If you didn't disable it in the config). Cases can convert RF and other electricity types into OC's own power system. There is also a Adapter that can convert power faster. Creative Cases generate their own power.

1. How to build a Computer

If you play with ComputerCraft you just place one block and done. In OpenComputers however you need more.

  1. What you have to craft:
    1. ComputerCase .... Better Tier means that you can have more components and also components with better tier.
    2. CPU or APU ... The Tier of the CPU determines the amount of components that can be connected to your computer. The APU has the same features like a CPU but it can replace the Graphics Cards. OpenComputers allows you to have different Languages by default Lua 5.3 is selected. You can change that by shift+rightclicking the CPU or APU in your hand.
    3. Memory ... The memory determines the amount of RAM your program can use.
    4. Graphics Card (if you didn't craft a APU) .... This will determine the resolution and the color your screen can have. A Tier 1 Card can only display black and white (like a basic CC Computer) a Tier 2 can display colors (like an advanced Computer) and Tier 3 can display more colors.
    5. Screen ... The screen determines the resolution and color like a graphics card. The setup will only be as good as the weakest part. So if you have a Tier 1 Card and a Tier 2 screen you can't display colors and the resolution will only match a Tier 1 setup. So both GPU and Screen should have the same Tier. A Tier 2 or 3 Screen can also handle touch events like an Advanced Computer from ComputerCraft.
    6. Keyboard ... The keyboard doesn't really do anything other than enabling the GUI of the screen to type something. Without a keyboard you can only look at the screen and use it as a touchpad (if you have Tier 2 Screen or higher).
    7. Disk Drive ... A disk drive is similar to the disk drive in ComputerCraft. A tier 3 cases and a creative cases have a builtin Disk Drive, so you don't need one if you selected one of these cases.
    8. OpenOS Floppy ... This item contains the OpenOS ready for installation. This floppy won't get consumed so you only need one and as long as you don't lose it you never have to craft a new one.
    9. Hard Disk Drive ... This allows you to store data. The tier determines the amount of storage you have.
    10. (Lua BIOS) EEPROM ... This is the BIOS that loads our OS. It is required for the startup of our computer. If you have crafted one, you can copy the EEPROM by combining the Lua BIOS EEPROM in a crafting Table with an empty one.
  2. The setup:

5RMQvUP.png

SXkGMKA.png

As you can see on the first image, the power cable can be connected directly to the case (no need for the adapter in this setup). The keyboard has to be a neighbor of the screen, but it doesn't have the be placed on the screen. So if you want to build a fancy table in from of the screen with the keyboard on top of it, you can do that. The Disk Drive and the screen is placed directly next to the case, you can also place the case somewere else and connect the screen and the drive via a cable, like in ComputerCraft. (except you don't need modems)

In the second picture you see, how you have to put the components into the UI of the case. The tier of the component has to be the same or lower than the number shown in the slot. OpenComputers helps you by highlighting possible slots and items in your inventory and also in JEI if you have it installed, you just have to hover over the item to see possible slots or hover over the slot to see possible items. In the second picture you can also see, that items get an address. That would be the address of the component if the items acts as a component. Like a redstone card would give you the redstone component.

If you have TOP (The One Probe) or WAILA installed the address of a component is also visible in there. You can see them on Cases, Screens, Redstone IO blocks, ... . If you don't have WAILA or TOP installed you can use an analyser from OpenComputers to get the address. If you hold Ctrl while rightclicking a block with the analyer you can copy the address of that component block.

2. Install OpenOS

As I already mentioned, the OS is not preinstalled when you build the Computer. You can write your own OS, but that's not what we want to do here. So now its time to put the OpenOS disk into the Disk Drive. You can open the GUI and put it in the slot or you can shift rightclick the disk drive (doens't work on the integrated disk drive in Tier 3 cases). The disk drive block will show you the inserted disk.

If you now start the computer by pressing the green button inside the gui or shift rightclicking the case with an empty hand, the light of the computercase will light up with a green color, and you should hear some floppy noise. 

ZfrKURv.png 

You should see something like this if you open the UI of the screen. Sadly we are not done yet. 

If your try to type in " edit test.lua " you will see the error message "file system is read only", that's because the OpenOS disk doesn't store anything on your computer it is a fixed storage, if you craft an empty floppy you would be able to store files on it. But in our case we already installed a Hard Disk Drive we just have to boot from it. To do that we have to copy OpenOS onto it. To do that you simply type  " install ". The program asks you if you want to install OpenOS. Now you have to type " y " and enter to start the process. This will take some time. After it is done the program will ask you if you want to reboot. So you can type "y" again. There shouldn't be much floppy sound and more sound from the hard drive. You can now take the floppy disk out and try " edit test.lua " again. It should now open the text editor. In the text editor you can press Ctrl + S to save and Ctrl + W to exit.

3. Troubleshooting

So chapter 2. didn't work and your computer won't turn on. If the green light is on but you don't see anything on your screen. It's likely that the screen is not connected or you forgot the graphics card. 

If there is only a red blinking light you should see a error message in your chat, if that's not the case build an analyser and shift + rightclick the case. You should see the "Last Error". These error messages should tell you what went wrong.

Somtimes you also get a blue screen with an error message. If you see the message "Too many components connected" it can means 2 things. One is that you exceeded the maximum amount of components that are allowed for your CPU Tier or you forgot the CPU. 

4. Programming

This is not a full Lua tutorial I will show you some basic programs and show you the difference to ComputerCraft.

First we have to create the file. Simply type "edit test.lua" to open the editor and yes all lua Programms should end with ".lua".

Since I want to work with redstone, I put a tier 1 redstone card in the second slot of my case. Since it is the only redstone component it is also the primary component. The primary component should be the first connected component of a type. In this example the redstone card is a "redstone" component. You will see why this is important in a second.

 

--First thing we have to do is to load the component library.
--Some libraries are loaded by default but "component" we have to load manualy
--Thats the biggest difference from ComputerCraft, that APIs are not loaded from the beginning.
local component = require("component")

--OpenComputers defines sides as numbers and not as a string like ComputerCraft
--But we don`t want to learn every number for every side so we can load a library that does that for us.
local sides = require("sides")

--Now we see what primary component means. OpenOS defines the primary component as a variable with the name of the type in component.
--So we can access component.redstone and will get the redstone card. Later I will show you how to connect to multiple components of
--the same type.
local redstone = component.redstone

while true do
  --Set the redstone output to 15 (maximum strength). The first argument defines the side. We use the sides api here to help us out
  --You can also put 3 instead of sides.front but as I sayed we don`t have to remember every number
  redstone.setOutput(sides.front, 15)
  --"os" is a library thats loaded by default. We use the sleep function to sleep for 500 milliseconds
  os.sleep(0.5)
  redstone.setOutput(sides.front, 0)
  os.sleep(0.5)
end

 

(Sorry it seems like the coloring is a bit off)

INFO: You can paste ingame by pressing your mouseweel in the UI of the OpenComputers screen.

This program has no exit condition so the only way to stop it is with Ctrl+Alt+C (or reboot the computer).

So how can you load multiple components with the same type?

We first have to get the Address. You can copy the address into the clipboard by holding Ctrl while clicking with the analyser on a component block. As I mentioned in most cases the first 3 characters in the address should be enough. Lets do the same thing with with a redstone component.

--First thing we have to do is to load the component library.
--Some libraries are loaded by default but "component" we have to load manualy
--Thats the biggest difference from ComputerCraft, that APIs are not loaded from the beginning.
local component = require("component")

--OpenComputers defines sides as numbers and not as a string like ComputerCraft
--But we don`t want to learn every number for every side so we can load a library that does that for us.
local sides = require("sides")

--This line now contains 2 functions. The proxy function is the same as peripheral.wrap in ComputerCraft but it needs an address and it doesn't work with
--sides. To get the full address out of the 3 Characters we have to search for it. The component api has a builtin function called "get" for this purpose.
--If you copy the full address with an analyser you can get rid of the component.get call and give the address directly to to proxy function
--You should replace "a37" with the address of your redstone card or redstone IO Block
local redstone = component.proxy(component.get("a37"))

while true do
  --Set the redstone output to 15 (maximum strength). The first argument defines the side. We use the sides api here to help us out
  --You can also put 3 instead of sides.front but as I sayed we don`t have to remember every number
  redstone.setOutput(sides.front, 15)
  --"os" is a library thats loaded by default. We use the sleep function to sleep for 500 milliseconds
  os.sleep(0.5)
  redstone.setOutput(sides.front, 0)
  os.sleep(0.5)
end

You can find more information about the apis here: https://ocdoc.cil.li/api

In the section "Standard libraries" you can find the libraries that are loaded by default and you don't have to require them

In the section "Custom libraries" you can find the libraries that are offered by OpenOS and you have to call "require" in order to load them

More information about the components can be found here: https://ocdoc.cil.li/component

 

Many APIs that are built into ComputerCraft are moved to components in OpenComputers like "HTTP", "Redstone", "Rednet", "Disk", "Fs" (partially), "Turtle" (Robot in OC).

Fs and Disk are mostly moved to the "filesystem" component, which allows you to get the empty space, rename the disk and basic File access. The "filesystem" api is a more abstract, which allows you to work with file "objects" and it has some more features.

HTTP is the internet component in OpenComputers and it is provided by the internet card. The internet component also allows you to make raw TCP requests.

The Rednet API in ComputerCraft is basically another way to access the modem peripheral in CC so you can find it in the modem component from OC, which is available if you add a network card or a wireless network card to your setup.

 

Events work similar to the events from ComputerCraft but they have their own Api in OpenComputers called "event". You can find a list of signals (other name for event) here: https://ocdoc.cil.li/component:signals

So lets make a example for a touch event: 

local event = require("event")

while true do
	--This will wait an infinite amount of time for any event
	local ev, arg1, arg2, arg3, arg4, arg5 = event.pull()
	
	--"interrupted" event is fired when you press Ctrl+C
	if ev == "interrupted" then
		return
	--The "touch" event is fired by the screen and gives us: The Screen Address (1), X & Y - Coordinate (2 & 3), The Mouse Button (4) and the Player (5)
	elseif ev == "touch" then
		print("Player " .. arg5 .. " touched screen " .. arg1 .. " at X="..arg2.." Y="..arg3)
	end
end

You can also filter for one event / signal or give the "pull" function a timeout. If you want to filter for multiple events you can use event.pullMultiple(). More information here: https://ocdoc.cil.li/api:event

If you want to handle Ctrl+Alt+C in your program you have to handle errors thrown by event.pull(). You can do that with pcall() but I won't go into details here (this is a normal lua function, so you can search it online).

If you want to handle an unknown amount of return arguments from any lua function you can also do " local ev = { event.pull() } "  this will create a array with all the arguments and you can access them with ev[1], ev[2], .... 

5. Filesystem

OpenComputers OpenOS has a more complex filesystem than the OS from ComputerCraft and it resembles the UNIX filesystem. If you bootup your computer you will start in /home. This is where you should write your own programs and store data. 

/bin ... Programs installed by OpenOS

/lib ... Libraries installed by OpenOS ( This is one of the paths where "require" searches for libraries

/usr/bin ... Programms installed by the user. Usually via a package manager like OPPM

/usr/lib ... Libraries installed by the user. Usually via a package manager like OPPM

/usr/man ... Manual pages for programms. You can type " man <command> " to open them

/usr/misc ... Some miscellaneous data for some programms

/etc ... Configs for programs.

/boot ... Scripts that run during bootup

/mnt ... This is where you can access other storage devices like floppy disks. OpenOS will create a directory inside mnt with the first letters taken from the address of the disk

/tmp ... Temporary storage. Cleaded after a reboot.

/init.lua ... File that gets executed by the BIOS

/autorun.lua ... This File doesn't exist after a fresh installation. But you can create it to execute a program after startup. (Like the "startup" file in ComputerCraft)

INFO: Not all of these directories exist after you install OpenOS. Some of them are created when they are needed by some program.

 

I hope this tutorial helps you to learn how to use OpenComptuers.

Maybe I will make a second part about the differences between a CC Turtle and a OC Robot.

Edited by feldim2425
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

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