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

Strange Results from program.

Started by grand_mind1, 04 July 2014 - 04:42 AM
grand_mind1 #1
Posted 04 July 2014 - 06:42 AM
I recently decided to once again pick up my button API. While working out a few test programs to relearn how to use it, I ran into a couple interesting bugs that I couldn't seem to find the source of.
(I tried explaining the bug purely with words multiple times, and failed, so I've put together a few images, demonstrating it.)
http://imgur.com/a/rY3dq

Test Program:
http://pastebin.com/jr38vzpa

Button API:
http://pastebin.com/TmCR7bWX

As you can see, the program acts very strangely. I should also note that any code may be put inside any function being run by the api with the same results.
I spent quite a while trying to figure this one out and just can't.
Help is appreciated!
Thanks! :D/>
Grim Reaper #2
Posted 04 July 2014 - 07:16 AM
My first question is as to why in the code that you've run, the print statement should print "Hello," but it prints "hello" instead. Also, I don't understand why you say that you rerun the program: In my mind, this implies that you haven't rerun the program after editing it. Do you mean that you rerun it for a second time? If I understand correctly, you're saying that you run it, get the expected output, close and edit, rerun, get unexpected output, close and rerun, finally getting the expected output?
Bomb Bloke #3
Posted 04 July 2014 - 07:20 AM
It's a matter of pointers and scope.

When you define a function, the actual function goes off in memory somewhere, and a variable ("test" in your case) gets assigned a pointer to that function (like a shortcut).

When any variable is defined (string/number/function pointer/whatever), Lua makes it accessible within a certain "scope". By default, that scope is "global", or "everywhere" - ComputerCraft limits that to "just the computer you're working on", but the point is, non-localised variables are available to the whole environment. They stayed loaded when your script ends, and other scripts can even access and modify them.

So with that in mind:

First run: button is assigned function pointed to by variable "test". That doesn't point anywhere yet, so the button has no function. Then you define "test" and set it to say "hello" (though this doesn't change the fact that you failed to assign the button a function). Pressing the button crashes out with an attempt to call nil.

Second run: "test" is set to point to a function that prints "hello", so that pointer is given to the button. "test" then gets a pointer to a function that prints "test" (though this doesn't change the button's pointer to the old function). Pushing the button prints "hello".

Third run: button gets the pointer to the "test" printing function. "test" variable gets a pointer to a new "test" printing function.

And so on. Bear in mind the global variables you define only get cleared if you either reboot your CC computer, or manually set them back to nil. Local variables clear as soon as their scope ends - eg, define a local variable in an "if" block, and it'll auto-wipe when that block completes. Local variables that aren't in a "block" last as long as your script does.
grand_mind1 #4
Posted 04 July 2014 - 07:21 AM
Sorry, I see that this may be slightly confusing. Let me try to explain. :)/>
First, I run the code with it outputting "Hello"
I then change the code to output "Test"
I run the code, and it incorrectly outputs "Hello"
I then rerun the same code and it now correctly outputs "Test"

Edit: Oops, while typing this Bomb Bloke responded.
Edited on 04 July 2014 - 05:22 AM
grand_mind1 #5
Posted 04 July 2014 - 07:31 AM
Ok, I think I understand, but please forgive me if I am wrong.

x = 2 --global variable. Clears when the computer is rebooted

local i = 1 --local variable outside a block. Clears when the script ends

if i == 1 then
  local p = 2 --local variable inside a block. Clears after the block is done
end

So my problem lies with the fact that the pointer is not changed? Is this just the way things work or am I doing something wrong that could be fixed?
Edited on 04 July 2014 - 05:35 AM
Bomb Bloke #6
Posted 04 July 2014 - 07:42 AM
Your main error is defining the button before you define the function you wish to make said button out of.
grand_mind1 #7
Posted 04 July 2014 - 07:47 AM
Oh my goodness, yes, that is it! I remember I encountered this when first creating the API. Putting the functions before fixed it but I didn't understand why. Thank you very much for explaining this to me.
Bomb Bloke #8
Posted 04 July 2014 - 07:53 AM
Not a worry. Another thing, though: do remember to localise your variables! For starters, the problem here would've likely been more obvious to you if you'd done so. Then there's the matter of cluttering up RAM with variables you're no longer using - it's all round a good practise.