This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LeDark Lua's profile picture

Script type API?

Started by LeDark Lua, 21 July 2015 - 03:29 PM
LeDark Lua #1
Posted 21 July 2015 - 05:29 PM
Well I explained it wrong mabie but ill give it a shot. This is what I have in mind:

I initialize a script like so:

myScript=script.init()

Then i can run it with arguments if there is like: myName=argument[1]

and then:

myScript("LeDarkLua")

and that script has:

myName=argument[1]
print(myName)

Thanks in advance!
Edited on 21 July 2015 - 03:32 PM
KingofGamesYami #2
Posted 21 July 2015 - 05:47 PM
I think you're looking for …, like this:

local function foo( ... )
  local tArgs = {...}
  print( tArgs[ 1 ] )
end

foo( "Hello World" )
LeDark Lua #3
Posted 21 July 2015 - 05:49 PM
yeah something like that, but just how to establish it or create this. Like I said in the thread?

Someone did a class system like java and they did something like: class "name" { } or something like that.
Edited on 21 July 2015 - 03:52 PM
KingofGamesYami #4
Posted 21 July 2015 - 05:53 PM

local script = {}
script.init = function( ... )
  return function( ... ) print( ({...})[1] ) end
end

myScript = script.init()
myScript( "Hello World" )

?
LeDark Lua #5
Posted 21 July 2015 - 05:54 PM
so now if i want to nest some code in there i just call: function myScript code here end ???
KingofGamesYami #6
Posted 21 July 2015 - 05:57 PM
Sorry, I'm not sure what you're trying to accomplish here.

What I did was make a function that returns another function. Which is pretty much pointless. Are you trying to make objects?
LeDark Lua #7
Posted 21 July 2015 - 06:00 PM
No, im trying to make something so that I can run and pass arguments. I dont know if I explained that correctly…
MKlegoman357 #8
Posted 21 July 2015 - 06:03 PM
I think he's trying to do the 'user script' part of this.

So the scripts would be placed in separate files? In this situation you'll either have to support Lua 5.1, Lua 5.2 or both. I'd suggest to support both. But note that CC is already started to slowly upgrade to Lua 5.2.

Anyway, this thing will need to work with function environments. You'd basically load the file with the custom environment containing your custom variables.
flaghacker #9
Posted 21 July 2015 - 06:04 PM
No, im trying to make something so that I can run and pass arguments. I dont know if I explained that correctly…

You're describing a simple function there, maybe try again ;)/>
LeDark Lua #10
Posted 21 July 2015 - 06:06 PM
Ok then :D/>, well its like a function but a bit simplier or something. Well look at GameMaker making scripts and you will see what I mean. Im baaaad at explaining :|
flaghacker #11
Posted 21 July 2015 - 06:13 PM
I never actually used Gamemaker, so I read this page, and I get the feeling scripts and functions are the same thing: they have a name, they get arguments, they run code and they return stuff. Am I failing to see something here? Could you perhaps try to explain the diffrence between scripts and functions?
Edited on 21 July 2015 - 04:13 PM
LeDark Lua #12
Posted 21 July 2015 - 06:15 PM
So ok, I want to run a file like so: myScript = script.init(path)
And then: myScript( … )
MKlegoman357 #13
Posted 21 July 2015 - 06:23 PM
I think he's trying to do the 'user script' part of this.

So the scripts would be placed in separate files? In this situation you'll either have to support Lua 5.1, Lua 5.2 or both. I'd suggest to support both. But note that CC is already started to slowly upgrade to Lua 5.2.

Anyway, this thing will need to work with function environments. You'd basically load the file with the custom environment containing your custom variables.

So ok, I want to run a file like so: myScript = script.init(path)
And then: myScript( … )

And …? You don't know …? You cannot figure a way to do …?
LeDark Lua #14
Posted 21 July 2015 - 06:27 PM
coroutines and loadstring…. or shell.run or os.run :D/>

Lemme introduce myself: Im a dummy and I can sometimes ask dumb questions.
Edited on 21 July 2015 - 04:27 PM
LeDark Lua #15
Posted 21 July 2015 - 06:37 PM
Ok i got it…. Im so dumb, sorry 'bout this

local function scriptAdd( ... )
return function( ... ) os.run({ argument={ ... } }, "/test") end
end

myScript = scriptAdd()

myScript("LeDarkLua")