Closed Tacodiva closed 4 years ago
You should be declaring your variables as local. Doing type = ...
on its own, without declaring a variable will pollute the global environment, hence the crash. The following code should work:
local modem = require("component").modem
local event = require("event")
modem.open(20)
local type = "Hello World"
event = event.pull("key")
That's because type
is a keyword in Lua and when you change it before the event.pull() it overwrites the global one.
The solution @SquidDev suggested might work, but you'd be better off using a different variable name.
@SquidDev @LizzyTrickster Thanks (I'm new to lua and don't really know the conventions) - this is still a bug though right? It doesn't seem like it should basically brick the computer if you do this and the fact that it only crashes when the script is called on startup seems weird.
It's not a bug, the process.lua lib is one of the low-level libs of OpenOS. If you overwrite keywords of course it's going to crash out the computer.
So long as you local
any variables, you shouldn't have issues with stuff outside of your script because the local keyword (so long as it's defined after the require()
lines) keeps it local to your script and nothing more.
you've put a broken script in your shell start-up, so you're going to get repeated calls to an unhappy program. this is like saying openos has a bug because someone wrote this:
echo reboot > /home/.shrc && reboot
And wabam -- endless reboot loop. openos bug? nah
Also, i would never recommend you change globals, and more importantly, don't use standard lua tables and method names for your own identifiers
this code would be better
local modem = require("component").modem
local event = require("event")
modem.open(20)
local txt = "Hello World"
local event_pull_response = event.pull("key")
and event.pull
has a variable arg return, so I would pack it:
local event_pull_response = table.pack(event.pull("key"))
see https://www.lua.org/manual/5.3/manual.html#6.6 for more info
Description When you call event.pull from a script run by .shrc when a variable named "type" is defined the computer blue screens with error
/lib/process.lua:112: attempt to call global 'type' (a string value)
. As the error suggests if you change the name of the variable to something other than "type" it works as intended.Steps to reproduce
/home/test.lua
:test
to/home/.shrc