Jump to content




[Hard to say..] script variable for arguments


12 replies to this topic

#1 CCTech

  • Members
  • 40 posts
  • LocationGermany

Posted 05 April 2018 - 08: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..

#2 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 06 April 2018 - 07: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 by Jummit, 06 April 2018 - 01:38 PM.


#3 CCTech

  • Members
  • 40 posts
  • LocationGermany

Posted 06 April 2018 - 12: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

#4 RR_DEADPOOL

  • Members
  • 15 posts

Posted 06 April 2018 - 12: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

#5 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 06 April 2018 - 01:21 PM

View PostRR_DEADPOOL, on 06 April 2018 - 12:14 PM, said:

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 by Jummit, 06 April 2018 - 01:37 PM.


#6 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 06 April 2018 - 05:38 PM

View PostOneTech, on 05 April 2018 - 08:21 PM, said:

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 by EveryOS, 06 April 2018 - 05:46 PM.


#7 CCTech

  • Members
  • 40 posts
  • LocationGermany

Posted 07 April 2018 - 01: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 by OneTech, 07 April 2018 - 01:29 PM.


#8 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 07 April 2018 - 03:18 PM

What would be handy is the full code or the error message/result you get from it.

#9 CCTech

  • Members
  • 40 posts
  • LocationGermany

Posted 08 April 2018 - 09:53 AM

View PostJummit, on 07 April 2018 - 03:18 PM, said:

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..

#10 RR_DEADPOOL

  • Members
  • 15 posts

Posted 09 April 2018 - 06:30 AM

View PostJummit, on 06 April 2018 - 01:21 PM, said:

View PostRR_DEADPOOL, on 06 April 2018 - 12:14 PM, said:

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.

#11 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 09 April 2018 - 06:51 AM

View PostRR_DEADPOOL, on 09 April 2018 - 06:30 AM, said:

View PostJummit, on 06 April 2018 - 01:21 PM, said:

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 by Jummit, 09 April 2018 - 06:53 AM.


#12 RR_DEADPOOL

  • Members
  • 15 posts

Posted 09 April 2018 - 07:06 AM

View PostJummit, on 09 April 2018 - 06:51 AM, said:

View PostRR_DEADPOOL, on 09 April 2018 - 06:30 AM, said:

View PostJummit, on 06 April 2018 - 01:21 PM, said:

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.

#13 CCTech

  • Members
  • 40 posts
  • LocationGermany

Posted 09 April 2018 - 03: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..





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users