Old 20th September 2009, 13:11   #1
Michgelsen
Major Dude
 
Michgelsen's Avatar
 
Join Date: Oct 2003
Posts: 1,416
Question Is it possible to redefine a function inside a script?

Dear modern skinners,

It's been a long while since I did any skinning, but I'd like to improve one of the skins I made.
I'd like to be able to choose between two algorithms for my VU meters, but without an 'if{}' inside the 'vutimer.onTimer(){}' checking which algorithm to use every 20 milliseconds.
It would be possible to do this when there's a function inside onTimer() that can be redefined when the user chooses the other algorithm.
(Wrong) Example:
PHP Code:
Function govu();

Vutimer.onTimer(){
    
govu();
}

Vumeter.onDataChanged(){
    
algorithm stringToInteger(vumeter.getData());
    if(
algorithm==1){
        function 
govu(){algorithm1}
    }
    if(
algorithm==0){
        function 
govu(){algorithm0}
    }


How would I correctly do the above? In other words, how do you redefine a function when the script is already running?
Michgelsen is offline   Reply With Quote
Old 20th September 2009, 19:44   #2
SLoB
Major Dude
 
SLoB's Avatar
 
Join Date: Jul 2003
Location: UK
Posts: 1,316
You've done the right thing by wanting to move it outside of the timer call, totally unnecessary.

There are a few ways, simplest is to create 2 functions 2 timers and stop the first timer when the user points to use the other and vice versa.

Is the algorithm totally different, can you not put it inside a variable and set the variable on change?
SLoB is offline   Reply With Quote
Old 20th September 2009, 21:48   #3
Michgelsen
Major Dude
 
Michgelsen's Avatar
 
Join Date: Oct 2003
Posts: 1,416
Thank you for that! Why didn't I think of that, it sounds simple enough...

The algorithm is different, because the decay of the VU is calculated in a different way. One uses a linear decay where the VU falls back one piece at a time, while the other one divides the difference between the previous (higher) signal and the new (lower) one by a fixed number, with the result that the decay is faster when the difference is bigger. This makes the VU more 'bouncy' when the music has a lot of dynamics or strong beats. On the other hand, when the level is more even, the VU seems more relaxed and less 'flashy'. This is more suited for analog-style VUs, but looks good on led-style VUs as well. Right now I have two versions of the same skin (Studiosus in my sig) and sometimes I just can't choose, so I wanted to integrate them. Now I can. Thanks again!
Michgelsen is offline   Reply With Quote
Old 21st September 2009, 00:29   #4
SLoB
Major Dude
 
SLoB's Avatar
 
Join Date: Jul 2003
Location: UK
Posts: 1,316
Hmm, for that kind of thing you might want to look at using a list, then use variables to change which value is pushed onto the list, then pop them off, so you use the list like a stack and push, pop.
Check out the rotational viz script (Azenis skin) I did a while back using a list, it's damn quick!
SLoB is offline   Reply With Quote
Old 21st September 2009, 00:50   #5
Michgelsen
Major Dude
 
Michgelsen's Avatar
 
Join Date: Oct 2003
Posts: 1,416
Well, that turned out to be a little more complex than I thought. I came to the conclusion that I had actually already used your solution for something else. I use one script for driving two VUs: the one in normal OR the one in shade mode. Whenever you switch mode, one of the two VUs (and its timer) is activated and the other stopped to save resources.
Thus, I needed four timers and a whole bunch of ifs and elses. Luckily they can all be outside of onTimer(), so I'm good now. It works. Still thanks for putting me on the right track.

Edit: I hadn't seen your reply yet, about the list. It's interesting. I have one list in use to control the 'progress bar' style sliders of my eq. I think a list would indeed make it possible to use only one timer for all the VUs. I don't know whether this would be the more efficient way, because it would mean that every VU, even invisible ones, are being driven. Do I understand this correctly?
Edit2: I apparently didn't. I now understand what you mean (I think): from the outside of onTimer, where we want as little stuff as possible, you control which VUs are on the list. On every tick of the timer, only the VUs that are on the list are being updated. Clever!
Michgelsen is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Skinning and Design > Modern Skins

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump