Accum in codebox

    Jun 09 2021 | 12:56 pm
    Hii, im wondering if its possible to accum x +1, if (y == 0) else x -1 if (y != 0)? I know its possible to "history = x" but im mostly just throwing things around and im interested to see some working code or maybe someone can point out my mistakes?Thanks

    • Jun 09 2021 | 3:21 pm
      just to add to what Gregory Taylor wrote before, one mistake you're making is when you use history in your codebox, you need to declare it with a capital 'H', so instead of the line in your codebox that reads, "history test;" i would write "History test(0);" (<-declares 'test' as a history object and initializes it to 0)
      i'd also not declare the history object in object form outside the codebox with the same exact name. finally, in your outer patch, it might be best to see what you're doing more specifically by adding a number box to the output of 'random' so while you're testing you know what the exact input is.
      beyond that i wasn't entirely clear on what you're trying to do(in particular, if you want to accum x, what do you intend the '5' input to act as?)... here's the patch rewritten if you wanted '5' to act as part of the increment/decrement but constantly accumulate/decumulate from there:
      that's probably not what you wanted, but hope that helps whittle away some of the mistakes you were asking about and give you some working code to help you further study how codebox can work with history and functions, etc. 🍻
    • Jun 09 2021 | 11:05 pm
      Hi πŸ‘½R βˆ†j βˆ†πŸ‘½ thanks, for some context '5' needs to accumulate but also will be variable, im about to take a look your example.
    • Jun 09 2021 | 11:44 pm
      Very cool, it helps so much to have examples, i switched 'test' in the if statement with 'val1' param and worked perfectly.Thanks again πŸ‘½R βˆ†j βˆ†πŸ‘½ always appreciate the insight.
    • Jun 09 2021 | 11:52 pm
      hey Brodie, no prob, was thinking in case you want it to accumulate by 1 every time, not by 5, in that case, inputting 5 to trigger the calculation each time is unnecessary, so by leaving the 'History' object outside the function(like you tried originally), will allow it to be exposed as an attribute/message to the outside... here i've named that history object 'init'(instead of 'test') since it's basically an initialization state, and that allows you to input 5 just once, initially(you can send a 'loadmess 5' to it from the outside if you always want to start from 5 when the patch is loaded), and then the rest can just evaluate according to a single/input 'y' value:
      hope that helps make more sense of it all 🍻
      edit: oops, sorry, didn't see your previous post.