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

More complicated if statement

Started by Crowdy199, 15 January 2013 - 10:20 AM
Crowdy199 #1
Posted 15 January 2013 - 11:20 AM
I am making and os where and there is a tool bar at the bottom i am using the mouse in the os now i no how to do that by using if x == whatever number and y == whatevet number so what i dony get is how to do this i want them to be able to do whatever they want unless they click whatever spot then run this code so plz tell me how to do that thank u
theoriginalbit #2
Posted 15 January 2013 - 11:37 AM
This is what I posted in a topic yesterday… :)/>

Conditionals are definitely a very important part of programming that everyone needs to know… that being said here goes…

Explaination of if's and boolean logic:
SpoilerIf statements
An if statement is a way of performing particular instructions given a certain condition… these conditions can be a single boolean (true/false) variable or a set of statements that produce a boolean… basic Lua syntax of an if statement is:

if <condition> then
  -- instructions to perform when condition is true
end

Boolean Logic
An if statement's conditional can then be furthered using boolean logic ( not, and, or ) using not, and, or all can effect the condition.
code example

if ( active and running ) or not waiting then
end
where active, running and waiting are all boolean variables

A not effects in this way

not true = false
not false = true

An and effects in this way

true and true = true
true and false = false
false and true = false
false and false = false

An or effects in this way

true or true = true
true or false = true
false or true  = true
false or false = false

As shown in the coding example you can also use brackets to make sure that those statements are evaluated first. For examples using

if not ( running and listening ) then
would only do the actions if running AND listening are false

Ifelse statements

Ifelse statements work in the exact same way as with the if statements except they provide one advantage, if we wished to do something based on a condition, without ifelse we would do this

if <condition> then
  -- what to do when condition is true
end

if not <condition> then
  -- what to do when condition is not true
end

however with ifelse statements we can do the following


if <condition> then
  -- what to do when condition is true
else
  -- what to do when condition is false
end

now ifelse statements can also be extended when dealing with variable equality as follows

if input == "this" then

elseif input == "that" then

elseif input == "the other" then

end
we can also end the above code with an aditional else, just incase it doesn't match anything



if input == "this" then

elseif input == "that" then

elseif input == "the other" then

else

end

Nested ifs
We can also have if and ifelse statements inside another if or ifelse statement. the basic syntax is as follows

if <first condition> then
  if <second condition> then
  else
  end
end
This code will only reach the second condition IF the first condition is true…

Pro tip:
SpoilerLets say that we want to assign a variable x to 10 if a boolean is true or 20 if it is false, normally people would use an if statement as follows

local x
if someBoolean then
  x = 10
else
  x = 20
end
However we can use a (what is referred to in other languages) ternary operator…
A ternary operator works in this way

<condition> and <value if true> or <value if false>
so for the example above we would do the following

x = running and 10 or 20

now lets say we want to assign to the variable x the value 20 ONLY if the variable y has no value (is nil) we can do the following

x = y or 20

we can also assign a boolean variable like so

var = <value 1>  and <value 2>
This will only assign true to var if BOTH value 1 and value 2 are not false or nil

We can also use


var = <value 1>  or <value 2>
This will assign true to var if either value 1 or value 2 is not false or nil
Obviously this can be extended into any combination not, and, or and brackets to gain order… but beware, the longer it is the more complicated it gets

var = not ( <value 1> and <value 2> or <value 3> ) and <value 4> or not <value 5>

Off the top of my head I think I have covered everything, I hope I'm not forgetting anything and that it all makes sense, it made sense in my head ;)/>
Edited on 15 January 2013 - 10:47 AM
sonmica #3
Posted 15 January 2013 - 06:02 PM
I am making and os where and there is a tool bar at the bottom i am using the mouse in the os now i no how to do that by using if x == whatever number and y == whatevet number so what i dony get is how to do this i want them to be able to do whatever they want unless they click whatever spot then run this code so plz tell me how to do that thank u

You will need to define a rectangle for where you want your toolbar to be in, and then when there is a mouse click event, it runs a check to see if the mouse has been clicked inside the rectangle.

Define your rectangle with boundaries, say:
toolbar_left – x coordinate
toolbar_right – x coordinate
toolbar_top – y coordinate
toolbar_bottom – y coordinate

Your conditional checks whenever you receive a mouse click event would be this (assuming x=0 is at the left of the screen and y=0 is at the top of the screen):


if ( mouseclick_x > toolbar_left ) and ( mouseclick_x < toolbar_right ) and ( mouseclick_y > toolbar_top ) and ( mouseclick_y < toolbar_bottom ) then
-- toolbar is clicked, do toolbar stuff
end

hope this helps.
ChunLing #4
Posted 15 January 2013 - 06:43 PM
The real problem is making it so that the user "able to do whatever they want unless they click". This involves either having an os.pullEvent() loop that handles all other kinds of events as well as mouse clicks or making use of the parallel api functions.