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

[solved] CraftOS 1.5 to 1.7 making code not work?

Started by Nhorr, 27 August 2015 - 03:32 PM
Nhorr #1
Posted 27 August 2015 - 05:32 PM
[RESOLVED - Thanks to all that replied!]

Hey, just wondering what changed that made this code that works in 1.5 not work in the emulator that runs 1.7, as well as how I could fix it.
In the emulator whenever a menu is clicked the list instantly minimizes again. (I'm assuming it's because of version 1.7, and not the emulator itself.)

http://pastebin.com/3CVX5cPE

It's from my personal UI project that I'm just doing for fun, but I'd like to know at what version this issue would have started as well as what I need to change to fix it.
Any tips on how this code could maybe be written to be simpler would be great, but that's not the main focus of the topic.

If this has been discussed in a previous topic, feel free to just direct me to it and lock the thread I guess.

Thanks in advance!
Edited on 11 February 2016 - 02:05 PM
KingofGamesYami #2
Posted 27 August 2015 - 07:08 PM
Everything that works for 1.5 should work for 1.7 exactly the same, with the exception of a few term functions and possibly rednet.
MKlegoman357 #3
Posted 27 August 2015 - 07:21 PM
The problem here is that you never properly check for an event before checking it's parameters. Try pressing some buttons on the keyboard, that might also crash the code. After you fix that problem I'll tell you why the menu closes. Don't want to spoil it too soon, or you might misunderstand how to fix your code.
Nhorr #4
Posted 27 August 2015 - 08:28 PM
The problem here is that you never properly check for an event before checking it's parameters. Try pressing some buttons on the keyboard, that might also crash the code. After you fix that problem I'll tell you why the menu closes. Don't want to spoil it too soon, or you might misunderstand how to fix your code.

Yeah, that's been an issue before… never understood why but I think I do now. I'll check the code once I catch a break and see how I could fix that first.
Thanks for cluing me into the reason for it crashing with keys being pressed, though! Obviously kinda new to lua and computercraft (I'm sure it shows), so any tips/help's appreciated.
Nhorr #5
Posted 02 September 2015 - 06:21 PM
The problem here is that you never properly check for an event before checking it's parameters. Try pressing some buttons on the keyboard, that might also crash the code. After you fix that problem I'll tell you why the menu closes. Don't want to spoil it too soon, or you might misunderstand how to fix your code.

I've been trying to work out how to check for an event before checking its parameters… I know you probably don't want to directly tell me how, but could I possibly get some hints? Or at least key words?

Sorry, I'm not trying to cheat on your help offer, I'm just really confused. :(/>
Edited on 02 September 2015 - 04:25 PM
MKlegoman357 #6
Posted 02 September 2015 - 07:04 PM
You simply check the first return value of os.pullEvent():


while true do
  local event, b, x, y = os.pullEvent()

  if event == "key" then
    if b == keys.enter then
      ...
    elseif
      ...
  elseif event == "mouse_click" then
    if b == 1 and x == 1 and y == 1 then
      ...
  end
end
Nhorr #7
Posted 03 September 2015 - 02:45 AM
You simply check the first return value of os.pullEvent():


while true do
  local event, b, x, y = os.pullEvent()

  if event == "key" then
	if b == keys.enter then
	  ...
	elseif
	  ...
  elseif event == "mouse_click" then
	if b == 1 and x == 1 and y == 1 then
	  ...
  end
end

Thanks for the tip.

In the segment

if event == "key" then
	if b == keys.enter then
	  ...
	elseif
	  ...
what exactly is that "if b == keys.enter then" part for? So I can integrate key usage as well?

Since I wasn't exactly sure of what it did, I didn't include it, but I did make that part of the code

if event == "key" then
  err=1
to simply create a non-used variable when the if statement is called.
(On a side-note, what would be used to make it so if an if statement were true, it did nothing at all?)

Other than that the code seems to work now and won't mess up when a key is pressed. Changed the code of a few other sections too to make the UI itself a bit improved.

EDIT: It also seems the issues I was having with the menus disappearing in the emulator is no longer an issue. I guess the topic's resolved, but I'd like to know the answer to the above questions first before calling the topic completely off…
Edited on 03 September 2015 - 01:11 AM
HPWebcamAble #8
Posted 03 September 2015 - 04:45 AM
You ONLY want to check for 'mouse_click' events, correct?
So leave out the

if event == "key" then
  err = 1
and change it to

if event == "mouse_click" then
  --# Existing code here

If, in the future, you'd like to integrate keybindings, just add an

elseif event == "key" then
after the if-statement for the mouse_click event.
(or back where it is now. Doesn't really matter)



if event == "key" then
  err=1
to simply create a non-used variable when the if statement is called.
(On a side-note, what would be used to make it so if an if statement were true, it did nothing at all?)
If you never use that variable, it is still useless
Its not like it will ERROR necessarily, since that's a completely valid thing to do in Lua, it just wastes memory
Since its a single variable its a tiny amount of memory, though its not good practice in the long run :)/>
Edited on 03 September 2015 - 02:45 AM
Nhorr #9
Posted 03 September 2015 - 05:33 AM
You ONLY want to check for 'mouse_click' events, correct?
So leave out the

if event == "key" then
  err = 1
and change it to

if event == "mouse_click" then
  --# Existing code here
That's the thing, though - that's what my code used to be, but it would crash every time a key was pressed. You can see this by running my older v0.2a build: http://pastebin.com/cEgrT1Qs - when you open a menu and press a button, it throws the program off its loop.
Now, by implementing the dead end "if event == "key" then" part of the code, the mentality is that it throws all key presses to the side and makes them do nothing. Now when keys are pressed, they just set the dead end variable, 'err' (for now, till I implement click and key press interactions).

If, in the future, you'd like to integrate keybindings, just add an

elseif event == "key" then
after the if-statement for the mouse_click event.
(or back where it is now. Doesn't really matter)
Gotcha :)/>/>/>


if event == "key" then
  err=1
to simply create a non-used variable when the if statement is called.
(On a side-note, what would be used to make it so if an if statement were true, it did nothing at all?)
If you never use that variable, it is still useless
Its not like it will ERROR necessarily, since that's a completely valid thing to do in Lua, it just wastes memory
Since its a single variable its a tiny amount of memory, though its not good practice in the long run :)/>
Oh, yeah, no I didn't think it'd ERROR, but just figured I'd make that the variable representing any non-allowed actions. :P/>
And yeah, I really don't like leaving un-used variables… is there anything I could do that would simply make the if statement "if event == "key" then" do nothing at all? I'm considering simply making the code just set the cursor pos to (1,1). In fact, I'll go ahead and edit my code. Tomorrow I'll tidy it up a bit more, but I'm on mobile and need sleep. :)/>

Thanks for the response :D/>
Edited on 03 September 2015 - 03:51 AM
Bomb Bloke #10
Posted 03 September 2015 - 05:52 AM
If you don't want your code to respond to a certain case, then simply don't write code that responds to that case.

Your original code responded to ALL event types, because you told it to:

function runDesktop()
while true do
  local event, button, x, y = os.pullEventRaw()
  if slc == 0 then
    if event == "mouse_click" then
      -- blah blah blah
    end
  elseif x>=1 and x<=11 and y==2 and button==1 then slc = 0  -- The checks here aren't conditional on the event type - they're performed whenever slc is not 0!

So let's say a key event occurs. "event" is set to "key", "button" is set to a number representing which key, and x and y are nil.

That last line in the snippet is therefore going to try to compare nil against some numbers. That's not possible, hence the crash.

You're thinking the answer is to try and define instructions for every single event type - instead, it's to define instructions only for the event types you're interested in. Eg:

function runDesktop()
while true do
  local event, button, x, y = os.pullEventRaw()
  if event == "mouse_click" then
    if slc == 0 then
      -- blah blah blah
    elseif x>=1 and x<=11 and y==2 and button==1 then slc = 0  -- The checks here can now only be performed against mouse click events!
Nhorr #11
Posted 03 September 2015 - 06:09 AM
If you don't want your code to respond to a certain case, then simply don't write code that responds to that case.

Your original code responded to ALL event types, because you told it to:

function runDesktop()
while true do
  local event, button, x, y = os.pullEventRaw()
  if slc == 0 then
	if event == "mouse_click" then
	  -- blah blah blah
	end
  elseif x>=1 and x<=11 and y==2 and button==1 then slc = 0  -- The checks here aren't conditional on the event type - they're performed whenever slc is not 0!

So let's say a key event occurs. "event" is set to "key", "button" is set to a number representing which key, and x and y are nil.

That last line in the snippet is therefore going to try to compare nil against some numbers. That's not possible, hence the crash.

You're thinking the answer is to try and define instructions for every single event type - instead, it's to define instructions only for the event types you're interested in. Eg:

function runDesktop()
while true do
  local event, button, x, y = os.pullEventRaw()
  if event == "mouse_click" then
	if slc == 0 then
	  -- blah blah blah
	elseif x>=1 and x<=11 and y==2 and button==1 then slc = 0  -- The checks here can now only be performed against mouse click events!
Figure I'd refresh one last time before going to bed and alas, you've caught me.

NOW I see what the issue there was… Such a stupid, easily fixable problem caused by carelessness on my part.

Like I said, I'll get back to fixing the code tomorrow for further review, but I think by tomorrow morning this should be ironed out.

Thanks for the response!

EDIT: Couldn't fall asleep quite yet. Fixed the code and everything is working as it should. Consider the thread as a resolved issue. Thanks to everyone that helped! :)/>
Edited on 03 September 2015 - 05:00 AM