I had a function that completely relied on keeping a float variable as a cooldown. I know that they never used to save actual variables, but they used to save list variables, which is what I used. The newest update completely changed that and now that doesn't work, so are there any ways to work around this or am I gonna have to change my system completely?
#Any way to save variables in functions?
1 messages · Page 1 of 1 (latest)
The problem with variables in a function is, functions call this pre-made circuit and all of its contents in whatever state they are in inside the function definition. So if an int variable is set to 5 by default, it will always read 5 at first when you call the function. You can still do some useful things with this but yes, it means you cannot make variables' values persist through multiple executions of the same function, unfortunately.
Luckily, when you have a function with exec ports, every output acts like a variable sort of; you activate it once and then all the outputs will stay how they were after that execution, until the next time you execute that call function, and it doesn't cost you any extra chips! There are some tricks you can pull off with this to substitute persistent (not sure if that's the best word but hopefully you know what i mean) variables and "variable changed" event receivers.
To substitute persistent variables what you can do instead is make an identical input and output that on the outside of the function loops back into itself, and you can use an if value on the inside to decide if you want to keep looping the old value or switch to a new one.
To substitute "variable changed" event receivers you can send a value out of the function, then loop back into itself to get what the value was the previous frame. Using an equals or not equals you can compare the current and previous value to tell if it has only JUST changed and send the execution down a unique line of chips just for that one frame!
@rugged egret