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.
Nesting troubles
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Nesting troubles
What have I gotten myself into?
Re: Nesting troubles
A couple of ideas (sorry if these are naive! )
- 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! ), and running this once (in love.load, if you're using LÖVE) before using anything that depends on it.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Nesting troubles
How about something like this:
The table will be automatically initialised the first time approximateSine is called.
Code: Select all
function math.sine.approximateSine(x)
if not ApproximateSineTable then
generate_approximate_sine_table()
end
return ApproximateSineTable[x]
end
Help us help you: attach a .love.
Re: Nesting troubles
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.
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)
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.
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?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Nesting troubles
Yes.AaronV wrote:about the love.load() bit, can i just make a bunch of .lua files and require them from inside love.load?
No.AaronV wrote: and they have to be called inside love.load if i want to use them at all?
Nothing would happen.AaronV wrote:what kind of thing would happen if i didn't call them there?
Depends.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)
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
Help us help you: attach a .love.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Nesting troubles
Why hasn't netultimate1 been banned from the forum yet? It snuck in and is now running amuck! Four whole posts all ready!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests