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

[Hard to say..] script variable for arguments

Started by CCTech, 05 April 2018 - 07:21 PM
CCTech #1
Posted 05 April 2018 - 09:21 PM
Hello dear Community,

I am making right now a new OS.. I‘ve just started.

So I would like to know hoe I can make it if the Player right clicks an Icon and the OS opens the editor in the right file.. I tried something like this to test if it works.. (using input read in console):

local Input = read()
local arg = read()
local edit = "edit“

while input == edit and arg then
Shell.run("edit" arg)
else
print("This is a Test")
end
end

But it doesn‘t works..
I can‘t give the error message because my laptop broke..
Jummit #2
Posted 06 April 2018 - 08:30 AM
I think you want to make an if clause, but you are using the keyword while instead. Here is the modified code:

local Input = read()
local arg = read()
local edit = "edit"
--# if instead of while
if input == edit and arg then
   Shell.run("edit" arg)
else
   print("This is a Test")
end
--# removed end, don't know why it was there :D/>/>

Here is the lua wiki article about if/then/else.
Edited on 06 April 2018 - 12:38 PM
CCTech #3
Posted 06 April 2018 - 01:04 PM
I used while because I wanted it to loop so when I enter "help“ didn‘t added it yet so I can again use it because if I use "if“ it ends the program when I type a command
RR_DEADPOOL #4
Posted 06 April 2018 - 01:14 PM
to make it work on if a player clicks a button on their mouse you have to use an os event. i can help you with it if you would like

i cant exactly help right now so tell me when youre on
Jummit #5
Posted 06 April 2018 - 02:21 PM
to make it work on if a player clicks a button on their mouse you have to use an os event. i can help you with it if you would like

i cant exactly help right now so tell me when youre on
If you want to get the x and y coordinate of the mouse when clicked, you use the os.pullEvent function:

--# make a new button
local button = {
  x = 10, y = 10, w = 10, h = 5, func = function()
    --# we call this function when the button is clicked
	print("button clicked")
  end
}

--# waits until mouse is pressed and gets data about the click
local event, mouseButton, mouseX, mouseY = os.pullEvent("mouse_click")

--# check if our button is clicked
if mouseX>=button.x and mouseY>=button.y then
  # mouse was clicked right and below the button
  if mouseX<button.x+button.w and mouseY<button.y+button.h then
    --# mouse was clicked left and above the bottom right corner of the button

    --# run the button function
	button.func()
  end
end

Here is more information about os.pullEvent and which events are there.

Later you can make a function to make a new button:

--# function to make a new button, takes x, y, w, h and func and returns button
local function newButton(x, y, w, h, func)
  return {
	x=x,y=y,w=w,h=h,func=func,
	draw = function(self)
	  --# use the paintutils api to draw a box
	  paintutils.drawFilledBox(self.x, self.y, self.x+self.w-1, self.y+self.h-1, colors.lightBlue)
	end,
	update = function(self, mouseX, mouseY)
	  if mouseX>=self.x and mouseY>=self.y and mouseX<self.x+self.w and mouseY<self.y+self.h then
		self.func()
	  end
	end
end
--# make a new button
local button = newButton(10, 10, 10, 5, function()
  print("pressed")
end)
--# draw button
button:draw()
--# get events once again
local event, mouseButton, mouseX, mouseYos.pullEvent("mouse_click")
button:update(mouseX, mouseY)
Edited on 06 April 2018 - 12:37 PM
EveryOS #6
Posted 06 April 2018 - 06:38 PM
Hello dear Community,

I am making right now a new OS.. I‘ve just started.

So I would like to know hoe I can make it if the Player right clicks an Icon and the OS opens the editor in the right file.. I tried something like this to test if it works.. (using input read in console):

local Input = read()
local arg = read()
local edit = "edit“

while input == edit and arg then
Shell.run("edit" arg)
else
print("This is a Test")
end
end

But it doesn‘t works..
I can‘t give the error message because my laptop broke..


local input = read()
local arg = read()
local edit = "edit"

if (input == edit) and arg then
shell.run("edit" arg)
else
print("This is a Test")
end
Changes:
  • The ending quote for "edit" should be written with just a US keyboard, but it was written with a USI keyboard
  • the "while" should be if (or you should place a "break" before the "end", in which case you would keep the while, but same difference
  • shell should not be capitalized
  • removed extra end
  • fixed case-senstivity isue for the input variable
Note that the bullet point about the quoting style is more important than it looks.
Here is an example using the forums code marking feature and it's auto-coloring (Like a syntax highlighter)

Look at this (US keyboard):

"Test"
This shall not be green

VS. this (USI keyboard):

"Test“
This shall not be green
Edited on 06 April 2018 - 04:46 PM
CCTech #7
Posted 07 April 2018 - 02:18 PM
Thanks to everyone here. I‘ve got it to work :)/>/>/> I know how I can make the button. But.. I need help making a menu which shows a button which should shutdown it won‘t shutdown.. here is the code:

— variables

running = true
_dt = paintutils.loadImage(".system32/backdrops/1.png")
button = 1

— Booleans 

_ms = 0

— Functions

drawBar = function()
term.setCursorPos(1, 1)
term.setBackgroundColor(colors.black)
term.clearLine()
term.setCursorPos(1, 1)
print("Settings")
end

Menu1 = function()
term.setCursorPos(2, 1)
term.setBackgroundColor(colors.gray)
print("Shutdown")
end

Shut = function()
running = false
term.clear()
shell.run("shutdown")
end

runtime = function()
if _ms == 0 and button == 1 and y == 1 and x < 8 then
Menu1()
_ms = 1
button = 2
elseif _ms == 1 and button == 2 y == 1 and x < 8 then
Shut()
end
end
end

Init = function()
drawBar()
runtime()
end

— Main Stuff
init()

So there aren‘t all booleans and variables and functions but thoose which are needed for the menu.. so and everything works fine I don‘t get an error but it won‘t shutdown.. I did it like in the tutorial and it worked there.. it would be nice if you anyone could help me here..

Edit: just corrected some wrong code.. like missing "x" etc..
Edited on 07 April 2018 - 12:29 PM
Jummit #8
Posted 07 April 2018 - 04:18 PM
What would be handy is the full code or the error message/result you get from it.
CCTech #9
Posted 08 April 2018 - 10:53 AM
What would be handy is the full code or the error message/result you get from it.

I just didn‘t wrote the the code for drawing the background.. I don‘t get any error message if I press shutdown it doesn‘t just stays like lt is.. no error message..
RR_DEADPOOL #10
Posted 09 April 2018 - 07:30 AM
to make it work on if a player clicks a button on their mouse you have to use an os event. i can help you with it if you would like

i cant exactly help right now so tell me when youre on
If you want to get the x and y coordinate of the mouse when clicked, you use the os.pullEvent function:

--# make a new button
local button = {
  x = 10, y = 10, w = 10, h = 5, func = function()
	--# we call this function when the button is clicked
	print("button clicked")
  end
}

--# waits until mouse is pressed and gets data about the click
local event, mouseButton, mouseX, mouseY = os.pullEvent("mouse_click")

--# check if our button is clicked
if mouseX>=button.x and mouseY>=button.y then
  # mouse was clicked right and below the button
  if mouseX<button.x+button.w and mouseY<button.y+button.h then
	--# mouse was clicked left and above the bottom right corner of the button

	--# run the button function
	button.func()
  end
end

Here is more information about os.pullEvent and which events are there.

Later you can make a function to make a new button:

--# function to make a new button, takes x, y, w, h and func and returns button
local function newButton(x, y, w, h, func)
  return {
	x=x,y=y,w=w,h=h,func=func,
	draw = function(self)
	  --# use the paintutils api to draw a box
	  paintutils.drawFilledBox(self.x, self.y, self.x+self.w-1, self.y+self.h-1, colors.lightBlue)
	end,
	update = function(self, mouseX, mouseY)
	  if mouseX>=self.x and mouseY>=self.y and mouseX<self.x+self.w and mouseY<self.y+self.h then
		self.func()
	  end
	end
end
--# make a new button
local button = newButton(10, 10, 10, 5, function()
  print("pressed")
end)
--# draw button
button:draw()
--# get events once again
local event, mouseButton, mouseX, mouseYos.pullEvent("mouse_click")
button:update(mouseX, mouseY)

that is overly complicated… you can make one function to make several buttons.
Jummit #11
Posted 09 April 2018 - 07:51 AM
Later you can make a function to make a new button:

--# function to make a new button, takes x, y, w, h and func and returns button
local function newButton(x, y, w, h, func)
  return {
	x=x,y=y,w=w,h=h,func=func,
	draw = function(self)
	  --# use the paintutils api to draw a box
	  paintutils.drawFilledBox(self.x, self.y, self.x+self.w-1, self.y+self.h-1, colors.lightBlue)
	end,
	update = function(self, mouseX, mouseY)
	  if mouseX>=self.x and mouseY>=self.y and mouseX<self.x+self.w and mouseY<self.y+self.h then
		self.func()
	  end
	end
end
--# make a new button
local button = newButton(10, 10, 10, 5, function()
  print("pressed")
end)
--# draw button
button:draw()
--# get events once again
local event, mouseButton, mouseX, mouseYos.pullEvent("mouse_click")
button:update(mouseX, mouseY)

that is overly complicated… you can make one function to make several buttons.
Wait, I thought I did exactly that? With the newButton function you can make as many buttons as you want. Or what are you trying to say?
Edited on 09 April 2018 - 05:53 AM
RR_DEADPOOL #12
Posted 09 April 2018 - 08:06 AM
Later you can make a function to make a new button:

--# function to make a new button, takes x, y, w, h and func and returns button
local function newButton(x, y, w, h, func)
  return {
	x=x,y=y,w=w,h=h,func=func,
	draw = function(self)
	  --# use the paintutils api to draw a box
	  paintutils.drawFilledBox(self.x, self.y, self.x+self.w-1, self.y+self.h-1, colors.lightBlue)
	end,
	update = function(self, mouseX, mouseY)
	  if mouseX>=self.x and mouseY>=self.y and mouseX<self.x+self.w and mouseY<self.y+self.h then
		self.func()
	  end
	end
end
--# make a new button
local button = newButton(10, 10, 10, 5, function()
  print("pressed")
end)
--# draw button
button:draw()
--# get events once again
local event, mouseButton, mouseX, mouseYos.pullEvent("mouse_click")
button:update(mouseX, mouseY)

that is overly complicated… you can make one function to make several buttons.
Wait, I thought I did exactly that? With the newButton function you can make as many buttons as you want. Or what are you trying to say?

I think I'm confusing myself with the buttons function but you can make a simple button using just one function within about 4 lines of code.
CCTech #13
Posted 09 April 2018 - 04:45 PM
Well thanks for all the help! I found what I did wrong. I don‘t had to add the buttons variable.. and the y coordinate was wrong..