repeating script across multiple objects

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Terryfic
Posts: 19
Joined: Sun May 05, 2013 3:50 pm

repeating script across multiple objects

Post by Terryfic » Sat May 25, 2013 3:54 pm

Hi All

I have the following script for a custom slider that sets its thumb position and displays the value in a label (Qtr). The slider and label are in a group.

on mouseMove
put the long id of graphic "tline" of me into CurrentLine
put the long id of graphic "thumb1" of me into Currentthumb
put the height of currentline into lheight
put item 2 of the loc of Currentthumb into tloc
put the top of currentline into ltop
put lheight-(tloc-ltop) into tunits
if the value of tunits >200 then put 200 into tunits
if the value of tunits <0 then put 0 into tunits
put the value of tunits into field "Qtr" of me
if item 2 of the mouseLoc > 250 then exit mousemove
if item 2 of the mouseLoc < 50 then exit mousemove
if intersect(Currentthumb,Currentline) and the mouse is down then set the loc of Currentthumb to item 1 of the loc of Currentline & "," & item 2 of the mouseLoc
end mouseMove

There are literally dozens of sliders and, although each slider operates independently, exactly the same code is repeated in every one.

Is there any way to share this script (or even the calculations to get the value for tunits) across all the sliders without having to copy and paste the whole script into each group? I'm thinking along the lines of a calling a subroutine in VB, but I can't seem to find anything similar in Livecode.

Regards

Terry

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: repeating script across multiple objects

Post by Dixie » Sat May 25, 2013 4:19 pm

have a look at 'behvior' in the dictionary...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: repeating script across multiple objects

Post by dunbarx » Sat May 25, 2013 4:39 pm

What Dixie said.

But read up on "the target", which is an easier way of having LC determine which object is being used, and common code can then be applied to many such objects.

Please do this. Make a new mainstack. Put three sliders and one field in it. Name the sliders "S1, "S2 and "S3"". In the card script:

on scrollbardrag
put the thumbPosition of the target && the short name of the target into fld 1
end scrollbardrag

Slide anywhere, anytime. This requires a little thought on the structure of the message hierarchy, and it is high time you experimented with this sort of thing.

Craig

Terryfic
Posts: 19
Joined: Sun May 05, 2013 3:50 pm

Re: repeating script across multiple objects

Post by Terryfic » Sat May 25, 2013 5:42 pm

Thanks Dixie. Finally got it to work.

Regards

Terry

Post Reply