Jump to content




Call a function before its defined


  • You cannot reply to this topic
3 replies to this topic

#1 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 25 August 2015 - 05:50 PM

I DON'T mean this:
test()

function test()
  print("test")
end



I mean something more like this:
(The code in question uses my Screen API
local function buttonAction()
  --# Other stuff
  calcSize()  --# Need to call 'calcSize'
end

local function calcSize()
  addButton( "Button1" , buttonAction )  --# Need to pass the 'buttonAction' function
end
This isn't how it actually looks, but this is the general idea.

Assume 'buttonAction' is also called further down in the code by something else


So how would I go about doing this?

Edited by HPWebcamAble, 25 August 2015 - 05:50 PM.


#2 Lignum

  • Members
  • 558 posts

Posted 25 August 2015 - 05:57 PM

Forward declare the function.

local calcSize = nil

local function buttonAction()
   calcSize()
end

calcSize = function()
   addButton("Button1", buttonAction)
end

Though you still have to make sure that calcSize is defined before buttonAction is called.

#3 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 25 August 2015 - 06:06 PM

View PostLignum, on 25 August 2015 - 05:57 PM, said:

Forward declare the function.

Awesome, it works, thanks :)

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 August 2015 - 11:53 PM

Note that the "= nil" business is optional. Just "local calcSize" will suffice.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users