Code Improvement: Propeller-based Tamagotchi V2.0
I like to go through each step of my thought process just in case someone else is coming across the same issues. At this point, my biggest issue is spaghetti code. So the latest suggestions from Roy and Whisker were that I needed to get rid of the stack overflow issues.
Essentially with my last code, it was sent out from one function to another to another, without ever coming back to step one. So you'd end up with a queue of functions - like premain, main, standby, hunger, maintwo, etcetc. And eventually the program would stop working.
So I moved around a few items:
I renamed Premain to Initialize. And called it once in the Main function.
I also realized that I was repeating the age analysis with every hunger/poo/standby pub - so I decided to just have the program set that variable in the Main function. This made the action pubs much shorter. Here's an example of how those look now:
So you'll notice that I did a few more things.
First I took out the debug display and made a new function out of it - then called it from the Main function (as Display).
Then I took out all of the MainSecond/MainThirds etc. and moved the comparisons (if > x, then do this) to the main function. This was contingent on learning that when code gets to the end of that function, it goes back to where it was in the code that called it.
You'll also notice that instead of having the code write out (via text.str) action specifics, I took advantage of the uniformity of the Dat section. For example. If a pet was hungry:
Instead of setting Function as @VYHungry - Main Pub's already set Function as @VY way in the beginning. So instead of depicting VYHungry and needing to change that variable anytime it was sleepy or pooing, I used @VY as a starting point. And fortunately, I had already standardized the spacing and number of responses by age in the DAT section.
So I'd know that from the first line of @VY or @Y or @A:
+ 0 = Standby face 1
+ 9 = Standby face 2
+ 18 = Hungry face 1
+ 27 = Hungry face 2
And so on. So I made the action pubs all go off of the first DAT line of the correct age. I was able to shorten up the code for the button push monitoring as well with the same DAT section monkeying!
@atdiy/@tymkrs











