Jump to content




I am trying to create an operating system


  • You cannot reply to this topic
5 replies to this topic

#1 Too_Quataz

  • Members
  • 3 posts

Posted 02 June 2013 - 10: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.

#2 blipman17

  • Members
  • 92 posts

Posted 03 June 2013 - 07:14 AM

View PostToo_Quataz, on 02 June 2013 - 10:54 PM, said:

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.

#3 CoderPuppy

  • Members
  • 121 posts

Posted 03 June 2013 - 07: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"

#4 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 03 June 2013 - 09: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


#5 Too_Quataz

  • Members
  • 3 posts

Posted 03 June 2013 - 09: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.

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 09: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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users