Home › Forums › General › Programming › local function?
Tagged: local
- This topic has 3 replies, 2 voices, and was last updated 5 years, 11 months ago by human fly.
-
AuthorPosts
-
April 8, 2018 at 10:29 am #83424
when you use a (sub-)function in a method, when do you
make it ‘local’? ie:
local function pselect()
it seems to work without the ‘local’ – so what happens
in either case?
– – –
as a side issue, i tried extracting sections of a method
to a ‘library’ method, leaving just the function calls in
the original method – and with some variables initialised
as ‘global’, so the functions could interact –and this seemed to be running ok – until i restarted Ctrlr
and my panel, and then nothing seems to work anymore: a key
subfunction returns ‘nil’. i checked everything but it all
seems ok.so now i’ve had to put back the functions in the original,
and it works again. – so what’s going on there? was it because
i was making those variables global, and they were still in memory?April 8, 2018 at 6:20 pm #83431I never used local functions so I cannot help you on that. But I can explain you what “local” means on variables: The var will only be available for the actual method. All other methods won’t see this var. If you don’t declare the var as local it will be automatically a global variable. This is not to be confounded with the Ctrlr global vars. Unfortunately they share the same name but this is something completely different.
April 8, 2018 at 6:38 pm #83432can’t tell if there’s a difference if i make a
‘nested’ function ‘local’ or not. either seems to
work with this.i suppose it keeps the function name local, so you
could have two functions with the same name, as
long as they are ‘local’ to the method.that’s actually what i’ve got at the moment, re-ordering
the mess i’ve made.
– – –
update on the other thing, not being able to call external
functions from a Library method: that was user error, partly.
i had commented out a couple of lines because i forgot what
they were for – turned out they were needed.
however: it seems that some of my (remote?) functions work
from the Library, and others i have to keep within the method
for now: i can run them straight as part of the main script,
or break it down into little local functions (not much point
unless it’s called more than once?)
i’ll probably find out why eventually. i had merged several
operations sharing the same iteration – seemed like a good
idea at the time, but now it means having to separate it
again.April 9, 2018 at 10:21 am #83438got it working. so my lengthy thing now looks like:
function padSelect(mod,value) -- This variable stops index issues during panel bootup if panel:getRestoreState() == true or panel:getProgramState() == true then return end -- ------------------------------------------------------ local function pselect() --[ needs (mod,value) in main function ]-- psub=nil --[ pad number ]-- local padName= L(mod:getName()) psub=tonumber(string.sub(padName,4)) end -- ------------------------------------------------------ pselect() --[ get psub pad# ]-- -- ------------------------------------------------------ findkit() --[ get kitselct kit# ]-- -- ------------------------------------------------------ kgetcolour() --[ get kit colours for psetcolour ]-- -- ------------------------------------------------------ findpad() --[ get padselct pad# ]-- -- ------------------------------------------------------ psetcolour() --[ set pad colour ]-- -- ------------------------------------------------------ keynumber() --[ get keyselct key# ]-- -- ------------------------------------------------------ --[ optional: get pad letter and display pad data]-- -- displaypad() -- ------------------------------------------------------ findVoice() -- ------------------------------------------------------ updateVoiceName() --[ get voice name and number ]-- -- --[ retrieve values ]--------------------------------- -- fetchVoiceTable() --[ restore voice params ]-- -- ------------------------------------------------------ -- fetchKeyTable() --[ restore keyassgn params ]-- -- ------------------------------------------------------ end
and the other part that works with it:
function kitSelect(mod,value) -- This variable stops index issues during panel bootup if panel:getRestoreState() == true or panel:getProgramState() == true then return end -- ------------------------------------------------------------ local function kselect() --[ needs (mod,value) in main function ]-- ksub=nil --[ kit number ]-- local kitName= L(mod:getName()) ksub=tonumber(string.sub(kitName,-1)+1) console(String("ksub="..ksub-1)) end -- ----------------------------- kselect() --[ get ksub ]-- -- ----------------------------- findkit() --[ get kitselct ]-- -- ----------------------------- ksetcolour() -- ----------------------------- findpad() --[ get padselct ]-- -- ----------------------------- pgetcolour() -- ----------------------------- keynumber() --[ get keyselct ]-- -- ------------------------------------------------------------ --[ lcd display ]-- --[ optional ]-- --[ get pad letter ]-- --panel:getComponent("tempdisplay"):setPropertyString("uiLabelText","key="..keysel) -- panel:getComponent("tempdisplay2"):setProperty("uiLabelText","pad:"..(tbl_padalpha[pselect]).."/kit:"..(ksub-1).."/key:"..keysel,false) -- ------------------------------------------------------------ --[ new library stuff ]-- findVoice() -- ----------------------------- updateVoiceName() --[ get voice name and number ]-- -- ----------------------------- -- fetchVoiceTable() --[ restore voice params ]-- -- ----------------------------- -- fetchKeyTable() --[ restore keyassgn params ]-- -- ----------------------------- end
- This reply was modified 5 years, 11 months ago by human fly. Reason: addendum
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.