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

I am trying to create an operating system

Started by Too_Quataz, 02 June 2013 - 09:54 PM
Too_Quataz #1
Posted 02 June 2013 - 11:54 PM
I am trying to create an operating system for a computer in my house. I was creating a calculator API to use in it, and it seems to be disregarding any conditional statements I use. Tried to fix it for about a full hour, and have made no progress. This is the script, but I don't think it's a scripting error. I know it could be more efficient.
http://pastebin.com/NBJdJJme
I should also mention this is still a work in progress. I intend to add more to it.
blipman17 #2
Posted 03 June 2013 - 08:14 AM
I am trying to create an operating system for a computer in my house. I was creating a calculator API to use in it, and it seems to be disregarding any conditional statements I use. Tried to fix it for about a full hour, and have made no progress. This is the script, but I don't think it's a scripting error. I know it could be more efficient.
http://pastebin.com/NBJdJJme
I should also mention this is still a work in progress. I intend to add more to it.

you made a fully working calculator if i look at it, altho i think you mean that it doesn't let you do anything. i got it working by writing the following underneath it.
calculator()
that runs the function you just made, you made a completely working function exept that you didn't runned it.

if you create a function like the following

function dostuff(stuff1, stuff2)
return stuff1-stuff2
end

you have a good working function, exept the fact that you never used it. to use it simply do the following.
dostuff(234, 3245)

making your own functions is usefull if you have to keep your code clean or want to use that part multiple times.
CoderPuppy #3
Posted 03 June 2013 - 08:50 AM
You need to use op == "add" or op == "subtract" instead of op == "add" or "subtract". And it would just be extremely difficult to make it work that way. So if op == "add" then it would be true otherwise it would return "subtract"
GravityScore #4
Posted 03 June 2013 - 10:26 AM
Expanding on what CoderPuppy said:

In an if statement, you have to imagine every condition in it separately. To visualise this:

if (op == "add") or ("subtract") or ("multiply") or ("divide") then

Lua will evaluate each of the brackets to either true or false. If any of the brackets are true, the if statement will continue (because of the ors).

Notice how the "subtract" and "multiply" and "divide" aren't compared to anything? When you don't compare a value to something (such as in this case), Lua will automatically compare the value to nil. So the above effectively becomes:

if (op == "add") or ("subtract" ~= nil) or ("multiply" ~= nil) or ("divide" ~= nil) then
--  Might be true       Is true                Is true                Is true

Of couse, the last 3 conditions are all true. "subtract" is not nil, nor is "multiply", or "divide", so your if statement is going to continue. What you want is this:


if (op == "add") or (op == "subtract") or (op == "multiply") or (op == "divide") then -- Note: you don't need the brackets - that's just for visualisation
Too_Quataz #5
Posted 03 June 2013 - 10:39 AM
I just woke up. Thank you guys or the help. Also didnt notice that I had an end statement in the wrong spot, but the if statement needed fixed. It works now. Thank you.
theoriginalbit #6
Posted 03 June 2013 - 10:58 AM
Also I should expand on GravityScores statement and tell you, that if you are ever passing a nil variable into a statement you must do a comparison for nil, in Lua nil evaluates to false, so that means that part of the conditional will result in false.

Not working code

local var = nil
if var then
  print('This will never print')
else
  print('This always prints')
end

working code


local var = nil
if var == nil then
  print('This will always print')
else
  print('This never prints')
end
or alternatively, since nil results in false, we can also use the not operator


local var = nil
if not var then
  print('This will always print')
else
  print('This never prints')
end