Page 1 of 1

Nesting troubles

Posted: Tue Jul 17, 2012 6:27 am
by AaronV
I'm pretty sure I don't know what I'm doing.

I'm trying to construct a math namespace, so I can call math.section.function().

inside my math namespace is the sine section.
so i can call math.sine.function().

my function is approximateSine().
so like math.sine.approximateSine().

THIS FUNCTION IS DONE SO I DON'T NEED TEH CODEZ.

so right now I have a table math = {},
and inside that table is a table sine = {},
and inside that table is a table that approximates sine, and a function that uses that table to more closely approximate sine.

so like, approximateSine() uses a SineApproxBase = {}.

but in order to calculate the table I need to loop through a different function, one that i only need to run once.

so i was gonna tack it on at the end but for variable scope i'd like to understand -
how can i set it up so that i run some construction code -
for loops and function calls and other things -
inside of a table.

because i have to construct SineApproxBase using a for loop.
but 90% of the variable should exist within the math.sine namespace.

i just would like to be able to run code inside of a table, but just once to construct/initialize it.

so am i just doing this wrong? is this even possible in this language?
the tutorials really don't seem to help much. not much on nesting.
read a bunch of class mimicing metatable like tutorials, but if they would've helped me i didn't get it.

not that it's so important right now, but I'm going to be running around willy nilly doing this to all of my code.

Re: Nesting troubles

Posted: Tue Jul 17, 2012 6:51 am
by Santos
A couple of ideas (sorry if these are naive! :D)
  • Precomputing the SineApproxBase table. To do this, you could make the SineApproxBase as per usual, but then saving it to a file, and then using this precomputed version.
  • Creating a function to initialize SineApproxBase (math.sine.initialize() perhaps! :D), and running this once (in love.load, if you're using LÖVE) before using anything that depends on it.

Re: Nesting troubles

Posted: Tue Jul 17, 2012 7:06 am
by Robin
How about something like this:

Code: Select all

function math.sine.approximateSine(x)
    if not ApproximateSineTable then
        generate_approximate_sine_table()
    end
    return ApproximateSineTable[x]
end
The table will be automatically initialised the first time approximateSine is called.

Re: Nesting troubles

Posted: Wed Jul 18, 2012 5:38 pm
by AaronV
both of those are what i figured might be the case.
it just frustrates me that i either have to avoid calling the function or call it by its full name math.sine.approximateSine() outside of a place that i want it in.
i want to use the table's variables math.sine.samples within the table math.sine outside of a function. more like in a do/end block or something, that would be ideal. i want all of this to occur when i require math.lua on it's own accord, basically. i just want "local" to be the table lol.
it's okay though, i'll just stick some function calls around and sort everything all crazy like.

thanks santos and robin. :crazy:



more important questions if you feel like a follow up:
about the love.load() bit, can i just make a bunch of .lua files and require them from inside love.load? and they have to be called inside love.load if i want to use them at all? what kind of thing would happen if i didn't call them there?
llua question: can i call something like math.sine.initialize() and have the function occur later on in the file, or does the computer need to plan for it's use like a variable sometimes does (like if you tried to do math with nil)

Re: Nesting troubles

Posted: Wed Jul 18, 2012 6:07 pm
by Robin
AaronV wrote:about the love.load() bit, can i just make a bunch of .lua files and require them from inside love.load?
Yes.
AaronV wrote: and they have to be called inside love.load if i want to use them at all?
No.
AaronV wrote:what kind of thing would happen if i didn't call them there?
Nothing would happen.
AaronV wrote:llua question: can i call something like math.sine.initialize() and have the function occur later on in the file, or does the computer need to plan for it's use like a variable sometimes does (like if you tried to do math with nil)
Depends.

When you call a function, it must exist.

Examples:

Code: Select all

--this works
function foo()
   bar()
end

function bar()
   -- hi
end

foo()

Code: Select all

--this doesn't work
function foo()
   bar()
end

foo()

function bar()
   -- hi
end
Do you understand the difference?

Re: Nesting troubles

Posted: Sat Jul 21, 2012 8:06 am
by Jasoco
Why hasn't netultimate1 been banned from the forum yet? It snuck in and is now running amuck! Four whole posts all ready!