Advanced function replacement
#21
Posted 17 August 2012 - 04:20 PM
#22
Posted 17 August 2012 - 04:21 PM
local os=os --making a local table that you can now modify os.pullEvent=os.pullEventRaw
and that should do it
#23
Posted 17 August 2012 - 04:27 PM
#24
Posted 17 August 2012 - 07:11 PM
immibis, on 17 August 2012 - 08:08 AM, said:
function foo() print("Hi!") endand
foo = function() print("Hi!") endare exactly equivalent (in fact, the first gets translated into the second when Lua compiles it)
ElvishJerricco, on 01 August 2012 - 12:11 PM, said:
ElvishJerricco, on 01 August 2012 - 12:11 PM, said:
function myPrint(msg) -- calculate condition condition = conditionFindingFunction() -- compare to see if printing is allowed if condition then --call the backup of print() that get's made before this function is called printBackup(msg) end end -- backup print to printBackup so that we don't lose the original print printBackup = print -- replace print with myPrint print = myPrint print("Testing!")
ElvishJerricco, on 01 August 2012 - 12:11 PM, said:
Wrong. Because functions are just values, you can get a function from a table the same way you get any other element in the table. window.print(msg) works fine, if the window object is programmed to work that way. However, in Lua it's very common for functions in tables to have the table itself as the first parameter, as there's no implicit "this" parameter like in Java.
window:print("hi") is just a shortcut for writing window.print(window, msg).
KaoS, on 16 August 2012 - 08:07 AM, said:
local os.pullEvent = os.pullEventRawNot only will that not work, it's a syntax error.
Yes after i wrote this tutorial i discovered why the colon is required for some things. I'll go ahead and update it. And
local os.pullEvent = os.pullEventRawis actually a syntax error. The new local os table had never been declared as a table so you can't index it with .pullEvent.
Also, i understand that functions are first class objects, but that's just a fancy term for object with a pointer, which is a reference to a spot in memory that holds the actual object. My wording was less technical, but that was on purpose because not everyone is a programming pro.
#25
Posted 17 August 2012 - 08:18 PM
KaoS, on 17 August 2012 - 04:21 PM, said:
local os=os --making a local table that you can now modify os.pullEvent=os.pullEventRaw
and that should do it
Except that won't work. You'll be creating a local called os that is a reference to the real os - meaning changes that happen to the local happen to the main table.
#26
Posted 17 August 2012 - 08:58 PM
, then change temp os will remain the same as it is not equal to temp. it is the other way around...
in variables this is correct but in tables if you do the following
tableone={"01","02","03","04"} tabletwo=tableone tabletwo[3]=nilthen tableone[3] becomes nil as well.... all I have to say is wtf... the only way to change this is manually going through each value
local temp={} for k in pairs(os) do temp[k]=os[k] end local os=temp temp=nilthen finally all changes made to os during the function are local only
I have no explanation for the first observation /> but atleast have finally worked out how to do it /> I tested it so I know it works
#27
Posted 18 August 2012 - 12:02 PM
KaoS, on 17 August 2012 - 03:56 PM, said:
KaoS, on 17 August 2012 - 04:21 PM, said:
local os=os --making a local table that you can now modify os.pullEvent=os.pullEventRaw
and that should do it
That makes "os" local, so if you do os=something then the change is not visible outside your program.
#28
Posted 18 August 2012 - 07:03 PM
immibis, on 18 August 2012 - 12:02 PM, said:
KaoS, on 17 August 2012 - 03:56 PM, said:
KaoS, on 17 August 2012 - 04:21 PM, said:
local os=os --making a local table that you can now modify os.pullEvent=os.pullEventRaw
and that should do it
That makes "os" local, so if you do os=something then the change is not visible outside your program.
You are not correct on either account. The reason it's a syntax error is because the local table "os" hasn't been declared as a table yet, so you can't use dot notation yet. And local os = os will not make os local. It'll give you a reference to the real os stored on the system. It doesn't copy it or anything. so local os = os; os.pullEvent = os.pullEventRaw will work EXACTLY as if you were using the regular table. Tables variables are references to objects in memory, so to set one table to another is to provide a reference. For example:
t = {"one", "two", "three"} t2 = t print(t2[1])
This will output "one", because t2 is now a reference to the same object as t. Similarly,
t = {"one", "two", "three"} t2 = t t2[4] = "four" print(t[4])
should output "four" because you're using t2 to set the table's 4 value to "four", then referencing the same table to get the value of 4
#29
Posted 19 August 2012 - 12:06 PM
ElvishJerricco, on 18 August 2012 - 07:03 PM, said:
I think I know how pointers/references work. "local os = os" makes a local variable called os that points to the same table as os did before, and changing its contents will change the contents of the global os table. I simply pointed out that "os=5" would not set the global os table to 5 after that.
And the reason "local os.pullEvent = os.pullEventRaw" is a syntax error is because os.pullEvent is not a valid identifier (or Name, as the reference manual defines it)
Syntax of a local declaration: stat ::= local namelist ['=' explist]
Definition of a namelist (about halfway through the section): namelist ::= Name {',' Name}
Definition of a Name
"os.pullEvent" is not a valid name, so "os.pullEvent" is not a valid namelist, so "local os.pullEvent = os.pullEventRaw" is not a valid local declaration (noting that "os.pullEventRaw" is a valid explist)
#30
Posted 24 August 2012 - 11:12 AM
KaoS, on 17 August 2012 - 08:58 PM, said:
, then change temp os will remain the same as it is not equal to temp. it is the other way around...
in variables this is correct but in tables if you do the following
tableone={"01","02","03","04"} tabletwo=tableone tabletwo[3]=nilthen tableone[3] becomes nil as well.... all I have to say is wtf... the only way to change this is manually going through each value
local temp={} for k in pairs(os) do temp[k]=os[k] end local os=temp temp=nilthen finally all changes made to os during the function are local only
I have no explanation for the first observation but atleast have finally worked out how to do it I tested it so I know it works
#31
Posted 24 August 2012 - 06:53 PM
immibis, on 19 August 2012 - 12:06 PM, said:
ElvishJerricco, on 18 August 2012 - 07:03 PM, said:
I think I know how pointers/references work. "local os = os" makes a local variable called os that points to the same table as os did before, and changing its contents will change the contents of the global os table. I simply pointed out that "os=5" would not set the global os table to 5 after that.
And the reason "local os.pullEvent = os.pullEventRaw" is a syntax error is because os.pullEvent is not a valid identifier (or Name, as the reference manual defines it)
Syntax of a local declaration: stat ::= local namelist ['=' explist]
Definition of a namelist (about halfway through the section): namelist ::= Name {',' Name}
Definition of a Name
"os.pullEvent" is not a valid name, so "os.pullEvent" is not a valid namelist, so "local os.pullEvent = os.pullEventRaw" is not a valid local declaration (noting that "os.pullEventRaw" is a valid explist)
You said it was because it had a dot in it. You can see where confusion might be drawn from that.
#32
Posted 25 August 2012 - 06:25 AM
ElvishJerricco, on 24 August 2012 - 06:53 PM, said:
immibis, on 19 August 2012 - 12:06 PM, said:
ElvishJerricco, on 18 August 2012 - 07:03 PM, said:
I think I know how pointers/references work. "local os = os" makes a local variable called os that points to the same table as os did before, and changing its contents will change the contents of the global os table. I simply pointed out that "os=5" would not set the global os table to 5 after that.
And the reason "local os.pullEvent = os.pullEventRaw" is a syntax error is because os.pullEvent is not a valid identifier (or Name, as the reference manual defines it)
Syntax of a local declaration: stat ::= local namelist ['=' explist]
Definition of a namelist (about halfway through the section): namelist ::= Name {',' Name}
Definition of a Name
"os.pullEvent" is not a valid name, so "os.pullEvent" is not a valid namelist, so "local os.pullEvent = os.pullEventRaw" is not a valid local declaration (noting that "os.pullEventRaw" is a valid explist)
You said it was because it had a dot in it. You can see where confusion might be drawn from that.
#33
Posted 25 August 2012 - 06:34 AM
local os=non-local-tableas this still makes a reference to the same table object and makes global changes, in order to do this we have to make a new, identical table that is used in the place of the os table locally by doing this
local temp={} for k in pairs(os) do temp[k]=os[k] end --this is creating a copy table called temp local os=temp temp=nil
#34
Posted 25 August 2012 - 10:44 PM
immibis, on 25 August 2012 - 06:25 AM, said:
ElvishJerricco, on 24 August 2012 - 06:53 PM, said:
immibis, on 19 August 2012 - 12:06 PM, said:
ElvishJerricco, on 18 August 2012 - 07:03 PM, said:
I think I know how pointers/references work. "local os = os" makes a local variable called os that points to the same table as os did before, and changing its contents will change the contents of the global os table. I simply pointed out that "os=5" would not set the global os table to 5 after that.
And the reason "local os.pullEvent = os.pullEventRaw" is a syntax error is because os.pullEvent is not a valid identifier (or Name, as the reference manual defines it)
Syntax of a local declaration: stat ::= local namelist ['=' explist]
Definition of a namelist (about halfway through the section): namelist ::= Name {',' Name}
Definition of a Name
"os.pullEvent" is not a valid name, so "os.pullEvent" is not a valid namelist, so "local os.pullEvent = os.pullEventRaw" is not a valid local declaration (noting that "os.pullEventRaw" is a valid explist)
You said it was because it had a dot in it. You can see where confusion might be drawn from that.
Sorta. The dot makes lua try to interpret the os identifier as a table. Then when it finds out that it's not a table it throws that "attempt to index ?" error.
#35
Posted 26 August 2012 - 05:38 AM
ElvishJerricco, on 25 August 2012 - 10:44 PM, said:
immibis, on 25 August 2012 - 06:25 AM, said:
ElvishJerricco, on 24 August 2012 - 06:53 PM, said:
immibis, on 19 August 2012 - 12:06 PM, said:
ElvishJerricco, on 18 August 2012 - 07:03 PM, said:
I think I know how pointers/references work. "local os = os" makes a local variable called os that points to the same table as os did before, and changing its contents will change the contents of the global os table. I simply pointed out that "os=5" would not set the global os table to 5 after that.
And the reason "local os.pullEvent = os.pullEventRaw" is a syntax error is because os.pullEvent is not a valid identifier (or Name, as the reference manual defines it)
Syntax of a local declaration: stat ::= local namelist ['=' explist]
Definition of a namelist (about halfway through the section): namelist ::= Name {',' Name}
Definition of a Name
"os.pullEvent" is not a valid name, so "os.pullEvent" is not a valid namelist, so "local os.pullEvent = os.pullEventRaw" is not a valid local declaration (noting that "os.pullEventRaw" is a valid explist)
You said it was because it had a dot in it. You can see where confusion might be drawn from that.
Sorta. The dot makes lua try to interpret the os identifier as a table. Then when it finds out that it's not a table it throws that "attempt to index ?" error.
#36
Posted 18 September 2012 - 12:06 AM
I replaced shell.run with:
local loggedIn = false local run = shell.run function shell.run(prog, ...) if loggedIn or prog=="startup" then run(prog,...) elseif prog==login then pass = ... loggedIn = pass==cpass if loggedIn then print("Logged in!") else print("Access denied.") end else print("You have to log in!") end end
I set up startup to just open my door, and could ctrl+t out and login with "login <password>" to do anything else />
#37
Posted 18 September 2012 - 05:06 PM
#38
Posted 18 September 2012 - 05:30 PM
unstoppable
--grab command-line params local args={...} --create the coroutine local routine=coroutine.create(function() shell.run(unpack(args)) end) local e,p1,p2,p3,p4,p5 --run once to get it started local _,waitingFor=coroutine.resume(routine,e,p1,p2,p3,p4,p5) while coroutine.status(routine)~="dead" --wait for an event e,p1,p2,p3,p4,p5=os.pullEventRaw() --if it's the right event... if waitingFor==nil or e==waitingFor then --resume routine passing in event and params _,waitingFor=coroutine.resume(routine,e,p1,p2,p3,p4,p5) end end
usage (at command line):
> unstoppable myprogram <any parameters here>
if the program you call exits or errors out, so will unstoppable, and ctrl-t will be back to normal.
#39
Posted 18 September 2012 - 06:15 PM
Quote
#40
Posted 18 September 2012 - 06:52 PM
Jajnick, on 18 September 2012 - 06:15 PM, said:
Quote
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users