Nesting troubles

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Nesting troubles

Post 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.
What have I gotten myself into?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Nesting troubles

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Nesting troubles

Post 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.
Help us help you: attach a .love.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Re: Nesting troubles

Post 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)
What have I gotten myself into?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Nesting troubles

Post 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?
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Nesting troubles

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests