Page 1 of 1

How do I clean up this code?

PostPosted: Sun Feb 22, 2015 5:26 am
by 80bit
:idea: I came up with a way that lets me have fading layers and sprites of the same type setup across the different layers and have my mouse properly tell me which one is active, limited by which one is currently visible.

CAPX @ : http://d.pr/f/uTA7/2HMUdXCi

My question is... am I doing this in a bad way? Is there a better way I can do it? Is there a cleaner way I can do it? This is just a test version to show you how im doing it, but if I am going to go ahead with this in my actual code, it's going to be a LOT of garbagey stuff I feel like there must be a way more efficient way to get the same results?

Would madly appreciate any help, if you have any time to check.

Thank you!
Nate

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 5:36 am
by DUTOIT
Use functions. With return value. That way you only code once and can call/use that function(code) all over the place.

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 5:45 am
by 80bit
DUTOIT wrote:Use functions. With return value. That way you only code once and can call/use that function(code) all over the place.


I thought the problem with functions in this case is they don't continually run. so where i want the layers to fade into one another, a function cant do that (am i wrong) because it just 'starts' to set the opacity of the layer, then stops after the first tick.

Hoping for some genius wisdom to cause a paradigm shift in my thinking tho. Ive tried functions and just couldnt get it working as i need here.

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 6:25 am
by blackhornet
Functions are used for repetitive code. You've got three sets of events doing exactly the same thing except the name of the layer being checked. Convert one of those blocks to be inside a function, using Function.Param(0) as the name to be checked, and replace the current blocks with a function call.

(You can't use Trigger once in this case, but that's a small loss, and not critical in this case.)

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 6:38 am
by 80bit
You did it. you blew my mind. i never once thought i could/should call a function every tick like that. seems dumb now that i realize it... its doing what i was already doing every tick...

thanks man.

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 6:59 am
by codah
I'm sure you've used FunctŃ–ons in your code before? :shock: Anyway the repetitive checking of 'activeLayer' had me twitching, checking the same thing 4 times in a row, so here's a slight mod of blackhornet's mod. Also, why set the red/blue/green in events instead of in properties? I guess because it's just a sample capx, but anyway I changed that too. I'm not a fan of using UID directly like that.
HoverOnLayers_BHT195.capx

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 7:49 am
by 80bit
Yeah i use functions all over the place, but it was always for triggered stuff like.... hard to explain... like 'doing stuff on an action' type stuff. i didnt really think to use them in situations where they're checked every tick.. for some reason i thought that would be inefficient haha. im a lil embarrassed by it, of course. ;)

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 8:21 am
by DUTOIT
80bit wrote:I thought the problem with functions in this case is they don't continually run. so where i want the layers to fade into one another, a function cant do that (am i wrong) because it just 'starts' to set the opacity of the layer, then stops after the first tick.

Hoping for some genius wisdom to cause a paradigm shift in my thinking tho. Ive tried functions and just couldnt get it working as i need here.


Wow, see you got some great stuff from the others ;)

Yes, functions can continually run. As long as you give it a reason to run.
Call it everytick works or you can give it a reason to run
Just as example
run function = 1
if run fuction = 1
and x<100
do this or that
else
run function = 0 x =99
or whatever floats your boat.

Remember every tick is reading your eventsheets from top to bottom and if a condition isn't met the function/code will run until that condition is met.

Re: How do I clean up this code?

PostPosted: Sun Feb 22, 2015 6:08 pm
by 80bit
time to clean up haha