8 posts
Posted 27 February 2013 - 09:49 AM
I was looking for a nice code for a elevator powered by Computercraft and Redpower 2 Frame Motors. Since i couldn't find a good one, I decided to make my own.
The elevator works together with a GPS system to determine the position of the elevator.
The main computer sends signals to 2 seperate sub-computers via rednet. These computers control the up and down signals for the Elevator.
The Problem I have right now is that the code worked just fine, then I changed something (can't remember what it was, sry) and now the code does nothing.
I saved the program under the name "newel" and whenever I start the program "newel" it does nothing. The blinking cursor disappears, the monitor keeps the text "newel" on it and nothing happens.
Even when I put print("something") at the top of my code nothing happens.
The code is not completely finished I think (wanna add a secret floor option and stuuf :P/>)
Oh and yeah, the code is probably really messy and not very clever, but that is all I can manage :P/>
Here is the code so far:
http://pastebin.com/sP7N2sQqHope somebody can help me there :D/>
8543 posts
Posted 27 February 2013 - 02:16 PM
Split into new topic.
1511 posts
Location
Pennsylvania
Posted 27 February 2013 - 04:38 PM
Can definately be cleaner.. But we aren't here to criticize now are we? Looking through all your variables and functions, pretty confusing (As you will notice, looking at someone else's code is hard since THEY coded it to their likings) but if I find anything wrong, I will give you a shout. Unless someone else helps you before I see the problem.
2005 posts
Posted 28 February 2013 - 03:28 AM
Don't call functions from within themselves unless you have an extremely good reason, as in something that cannot be achieved other than by recursion. Don't use it for simple looping.
Likewise, don't call a function from a function that is called by the first function. This is basically the same thing, but harder to spot and therefore harder to debug.
You've basically made your code so recursive that the loadstring hangs when attempting to parse it. That is somewhat impressive, but it means you need to can it and start again from scratch. The "small change" you made was probably just adding one more recursive function call, a sort of straw breaking the camel's back thing. But the recursion is deadly wrong and has to go, you have no excuse for using a single recursive function here, let along multiple functions that call one another recursively.
8 posts
Posted 28 February 2013 - 03:30 AM
Well, what you are seeing here is a code from a guy who didn't know anything bout codinh before he typed in the first letter :P/>
It is kinda a "learning on the way" code.
I am definitely interested in any feedback regarding making it cleaner, less confusing and more efficient. Am thankful for every single advice I can get :)/>
Cheers for looking into it :D/>
-Bassy
8 posts
Posted 28 February 2013 - 03:35 AM
So what I understand from this is:
I am simply stressing the program to debug it to a point where it can't handle it anymore, right?
Therefore I need to replace the function looping with a different looping technique?
7508 posts
Location
Australia
Posted 28 February 2013 - 03:38 AM
Yes very impressive to cause a hang. normally recursive calls end with the result of stack overflow.
simply use
while true do
-- code here
end
There are heaps of other loops … more can be read on control structures in lua, in the PIL,
here
8 posts
Posted 28 February 2013 - 03:52 AM
Haha, if my code is not impressive than I need to do some other impressive stuff, right? :P/>
Thanks for your feedback so far. Gonna mess with the code a bit and maybe come back to you guys after I encounter another problem (I am sure I will run into a few more :D/> )
8 posts
Posted 28 February 2013 - 05:44 AM
Is there a way to use a table in such a way, that if you input the value it returns the value and the key for the input-value as variables?
The key and the value need to be editable tho. So that the keys could be the z–coordinates that you need to edit depending on your floor levels and the value would be the name of the level.
I understand that I can use ipairs for it, although this doesn't really work, coz it doesn't continue iterating after the first integer key absent from the table.
Is there any way to make it continue iterating until it found the right value?
Nevermind about that part, found out about pairs() :D/>
Edited on 28 February 2013 - 05:31 AM
8 posts
Posted 28 February 2013 - 07:13 AM
K, after messing around a bit more I made this code:
local table = {[1]="Test1",[3]="Test2",[10]="Test3"}
for i,v in pairs(table) do
local input = read()
print(i,table[input])
end
My question now is:
How can I assign the (i,table[input]) part to a variable?
1511 posts
Location
Pennsylvania
Posted 28 February 2013 - 09:16 AM
K, after messing around a bit more I made this code:
local table = {[1]="Test1",[3]="Test2",[10]="Test3"}
for i,v in pairs(table) do
local input = read()
print(i,table[input])
end
My question now is:
How can I assign the (i,table[input]) part to a variable?
This should work…
local someVariable = (i,table[input])
May I also ask why you are indenting print?
8 posts
Posted 28 February 2013 - 11:29 AM
It was simply the only way it worked for me :D/>
I don't want to use the print there at all.
I tried to replace the print(derp) command with
somevariable = (i,table[input])
but I didn't define the variable as local, I did define it globally. That should normally work as well, right?
It didn't for me tho, the program always gave me the "expecting index. Got number" error.
2005 posts
Posted 28 February 2013 - 12:39 PM
…
What exactly are you trying to do here?
First off, don't name a table "table", that will hide your table functions (which is bad, even just doing it locally). Second, your table has only number indexes, you're trying to print the value of a string index (which will be nil). When you try assignment of (i,table[input])…well, you've seen what happens.
Are you trying to use the value from read to get a value out of the table, or are you trying to put a value into the table?
8 posts
Posted 28 February 2013 - 10:38 PM
I only named it table for demonstration. It is going to be named something else in the finished code. But thx for telling me, didn't know that.
I quite don't understand what you mean by:
" Second, your table has only number indexes, you're trying to print the value of a string index (which will be nil)."
Because the code works just fine for me in its current state.
I had two tables in my previous code (that one that hangs itself). One table where I stored the Level names (called floors{} ) and one where I stored the z-coordinates of the Levels (called currentz{} ).
Now I am trying to merge them together in such a way that the keys are the z-coordinates (hence only number index) and the values are the Level names. This way if you want to add, remove or change Levels you only need to change the keys and values inside that table and you don't have to look through the whole code and change bits here and there.
Let's say you input "Test2" (which needs to match a value inside the table ofc) then I want the code to assign the key/index (in this case "3") of that input to a variable and the value/input to a second variable.
Let's say we have the variables
n (for the names) and l (for the level coordinates),
then the output after the code should be:
n = "Test2"
l = "3"
I think that I could make the code much smaller this ways. If you look into my previous code than you can see the part where it says
–Level 1
Somecode
–Level 2
Somecode
.
.
.
and so on
I think that I can get rid of most of this with the method I described above.
Dunno if this actually works, that's why I am asking you guys here :D/>/>
Hope you understand what I mean by all this.
Thx for your patience and help so far, I guess my questions must be really noobish :P/>/>
2005 posts
Posted 01 March 2013 - 12:23 PM
What do you mean by "the code works just fine for me in its current state"? Do you mean that it does something useful, or do you mean that it doesn't error? Because print() handles nil values just fine, but there is no point.
"{[1]="Test1",[3]="Test2",[10]="Test3"}" defines a table with three strings indexed by (non-sequential) numbers. If you use the numbers 1, 3, or 10 to index this table, you get one of the strings. If you use any other numbers, or any non-number values, then you will find a nil value. The function read() will always return a string.
If you're trying to put a value into the table, then you can assign a value to the new index. If you're trying to get a value out of the table, then you need to use an existing index.
Tables can contain functions and other tables, along with the more conventional value types. There are no limits on what you can put into a table in Lua, if it can't be done using a table, then it simply cannot be done at all.
In the particular case of using a table to handle a series of conditionals with similar form, that should be possible. I don't know exactly what you're trying to do in the section of code you mention, the fact that the program as a whole cannot be parsed means that nothing in it actually does anything and that makes it hard to establish intention.