1220 posts
Location
Earth orbit
Posted 30 March 2014 - 06:22 PM
Greetings, all :)/>
In the program I'm working on I have a function (drawCLI) that is called from both above and below so it can't be localized. Due to the structure of the program there is no practical way to place all the calling functions below drawCLI.
This being the only function I couldn't localize (and it being responsible for determining which elements to draw on screen) I added a 'stub' function below drawCLI (callDrawCLI) that allows me to localize drawCLI and only have the stub be non-localized. The only two functions calling the stub (callDrawCLI) are the two that are above drawCLI. This seems to work fine, so my questions are:
Is this worth it? Do I gain any advantage by doing this or is it just as well to leave drawCLI non-localized and ditch callDrawCLI()?
Edited on 30 March 2014 - 05:58 PM
350 posts
Posted 30 March 2014 - 06:34 PM
For me it makes no diference. But i am using the 1.5.2 minecraft version, wich may be outdated.
1281 posts
Posted 30 March 2014 - 06:35 PM
There's 2 ways to deal with this, personally i use my own local environment, saves me from having to deal with this at all. However, you could just define the variable that points to the function as local from the get go.
local test
local function iWillCallTest()
test()
end
local test = function()
print"test has been called"
end
iWillCallTest()
It dosen't matter what the variable points to at the time, only thing that matters is wether it's local or not. Your original solution would gain you nothing, as you'd still end up with a global function.
For me it makes no diference. But i am using the 1.5.2 minecraft version, wich may be outdated.
This goes for all versions of ComputerCraft because it's a Lua thing, regardless of what version.
Edited on 30 March 2014 - 04:37 PM
350 posts
Posted 30 March 2014 - 06:50 PM
There's 2 ways to deal with this, personally i use my own local environment, saves me from having to deal with this at all. However, you could just define the variable that points to the function as local from the get go.
local test
local function iWillCallTest()
test()
end
local test = function()
print"test has been called"
end
iWillCallTest()
It dosen't matter what the variable points to at the time, only thing that matters is wether it's local or not. Your original solution would gain you nothing, as you'd still end up with a global function.
For me it makes no diference. But i am using the 1.5.2 minecraft version, wich may be outdated.
This goes for all versions of ComputerCraft because it's a Lua thing, regardless of what version.
Right. Thanks :)/>
1220 posts
Location
Earth orbit
Posted 30 March 2014 - 07:15 PM
So, in my case I did this and it isn't working - I still run into 'attempt to call a nil'
local drawCLI
local function callDrawCLI()
drawCLI()
end
... other functions ...
local drawCLI = function()
<drawCLI function here>
All calls still go directly to drawCLI() with the exception of the two functions that call callDrawCLI() - those two calls to callDrawCLI() still don't work. Unfortunately I don't see what I'm doing wrong and this syntax is unfamiliar to me.
Edited on 30 March 2014 - 05:34 PM
350 posts
Posted 30 March 2014 - 07:37 PM
So, in my case I did this and it isn't working - I still run into 'attempt to call a nil'
local drawCLI
local function callDrawCLI()
drawCLI()
end
... other functions ...
local drawCLI = function()
<drawCLI function here>
All calls still go directly to drawCLI() with the exception of the two functions that call callDrawCLI() - those two calls to callDrawCLI() still don't work. Unfortunately I don't see what I'm doing wrong and this syntax is unfamiliar to me.
Why do you have a function just for calling other function?
1281 posts
Posted 30 March 2014 - 07:45 PM
You define it as a local variable once
local drawCLI
Then you redefine it as another local variable here
local drawCLI = function() --ditch the local here
<drawCLI function here>
1220 posts
Location
Earth orbit
Posted 30 March 2014 - 07:52 PM
You define it as a local variable once
local drawCLI
Then you redefine it as another local variable here
local drawCLI = function() --ditch the local here
<drawCLI function here>
Funny you should point out that exact thing - I suspected that, removed the 'local' in front of drawCLI = function() and it works - I just wasn't sure if that was the correct fix. As RoD pointed out, It looks like I don't even need the callDrawCLI() stub either.
Thanks, CometWolf and RoD!
Edited on 30 March 2014 - 05:58 PM
7508 posts
Location
Australia
Posted 30 March 2014 - 11:35 PM
However, you could just define the variable that points to the function as local from the get go.
The programming term for this is defining a forward declaration ;)/>
1220 posts
Location
Earth orbit
Posted 31 March 2014 - 12:31 AM
However, you could just define the variable that points to the function as local from the get go.
The programming term for this is defining a forward declaration ;)/>
Knowledge is power. Thanks for making me more powerful :)/>