OpenComputers

OpenComputers

49M Downloads

Loop in Function not Looping

TheMagikPeanut opened this issue ยท 3 comments

commented

I have yet another issue. I'm writing a program for controlling (soon automatic control) a motor-generator (Mechanical Multiblock from Immersive Wires). Pertinent mods are Immersive Wires, Zetta Industries, Immersive Engineering, ProjectRed (for the bundled cables), an of course OC. Attached is the program.

OC: 1.7.5.192
MC: 1.12.2

The issue I'm having is this: Several functions have loops, that do/would run a number of times, run indefinitely (until key press), or run until a specific condition is met. All functions work except one: genStart(). There is no loop that will not break out after the first run. For loops aren't exactly applicable, according to my current knowledge, the while loop is preferred, as it runs a constant check for the specified condition, and then there's the sloppy goto method - least preferred, but IDC how it works, so long as it does what I need to have happen there.

Already checked to make sure variables are getting values, and values are valid. You'll note where it will print the value. Value checks out.

I repeat again, NO LOOP will loop in this function. Everywhere else they run fine. genMonitorSpeed() does exactly what it is supposed to, literally running until a key press breaks it out.

gencontrol.lua.txt

EDIT: I should also add that all the loops I mention I have tried, except the for loop, for reasons stated above.

commented

verify that speed1 and speed2 are what you think they need to be. verify that you're passing the correct arguments to rs.getBundledInput().

it's a good idea to make your genRun and genGetSpeed and cli functions local. you may have fewer strange issues.

a while true do cli() end rather than return cli() at the end of every function is more conventional.

in cli(), you can just do if not k then return end and avoid checking if (k and k == "bla") every time.

tl;dr this more than likely isn't an OC issue, but is rather an issue in some part of your setup.

commented

genStart calls genSetPower which calls cli and thus the "main loop which is not a loop but acts like a loop" starts over again. Thus, the end of the function genStart is never reached.

@TheMagikPeanut in addition to @Ocawesome101 's great advice, I recommend you isolate the purpose of each function with clear pre and post conditions, instead of using tail call optimizations as a replacement for looping

commented

@payonel, can you explain? I'm still learning, and don't know what all that means.

Edit: I do see how genSetPower is interfering, as the end is never reached. As for the functions being local, I'll try that again, but I changed them from local because something wasn't working, as something was I guess out of scope. I kept getting context errors regarding a variable not existing or something.

Also, what was said about cli(), the point of testing k every time is to ensure that the entered command matches what's defined. Is there a better way of doing that?