Page 1 of 1
I have a question about how lua works in general
Posted: Sat Oct 07, 2023 4:41 pm
by MaxGamz
When I stared learning how to make games in Lua, I liked how versatile it was, especially with the ability to create different files for each object. The only problem I am having however, is when I make an array object. Recently I created a coin systems and it is working just fine, but I don't want all the information about my project be under the main.lua file. I want my project to be more organized. And the coin system isn't like the player object at all since I spawn an instance of the coin object for each tile that has an area reserved for a coin and it updates each coin accordingly. I would like to create a separate coin file like I did with the player one to make the code more clear and also use elements of the coin file for the enemies in my game.
- gam.love
- (883.99 KiB) Downloaded 63 times
Re: I have a question about how lua works in general
Posted: Sat Oct 07, 2023 7:08 pm
by dusoft
And your question is?
Re: I have a question about how lua works in general
Posted: Sat Oct 07, 2023 8:53 pm
by MaxGamz
dusoft wrote: ↑Sat Oct 07, 2023 7:08 pm
And your question is?
I wan't to create a seperate coin file and call the functions that are responcible for spawning the coins in the main file. I tried it before but I got a lot of errors and I wasn't sure why it was happening
Re: I have a question about how lua works in general
Posted: Sun Oct 08, 2023 12:05 am
by togFox
Main.lua:
Coin.lua:
Code: Select all
Coin = {}
Function Coin.myfunction()
-- do things
End
Return coin
I'm not sure if that answers the question. (Im On mobile)
Re: I have a question about how lua works in general
Posted: Sun Oct 08, 2023 1:22 am
by MaxGamz
togFox wrote: ↑Sun Oct 08, 2023 12:05 am
Main.lua:
Coin.lua:
Code: Select all
Coin = {}
Function Coin.myfunction()
-- do things
End
Return coin
I'm not sure if that answers the question. (Im On mobile)
What would be the difference between something like Coin.myfunction() and Coin:myFunction()? When I tried to do the second one it wasn't working eventhough I used the same method for the player
Re: I have a question about how lua works in general
Posted: Sun Oct 08, 2023 1:36 am
by MaxGamz
dusoft wrote: ↑Sat Oct 07, 2023 7:08 pm
And your question is?
To make it more clear here is my last attempt. I commented out the coin spawing functions in the main.lua file and tried to import them from a coin.lua file
Re: I have a question about how lua works in general
Posted: Sun Oct 08, 2023 7:57 am
by dusoft
https://www.lua.org/pil/16.html
See the explanation about colon functions here.
Re: I have a question about how lua works in general
Posted: Sun Oct 08, 2023 2:01 pm
by MaxGamz
Do the functions get indexed as well as the values? I keep getting an "attempting to index function value" error and I'm confused if I should make another table within the coin one but I feel like it would add necessary complexity?
Re: I have a question about how lua works in general
Posted: Sun Oct 08, 2023 4:01 pm
by MrFariator
Well, as the error warns you, you can not index a function. To illustrate it, consider this minimum example:
Code: Select all
local function myFunction() end -- an empty function that does nothing
myFunction() -- ok
myFunction.someValue() -- error, can not index a function
The same goes for situations, like trying to index a boolean or an undefined variable.
Code: Select all
local myBool = false
myBool.someValue = 1 -- error, can't index a boolean
local myTable = {innerTable1={1,2,3}}
myTable.innerTable1[1] = 2 -- ok
myTable.innerTable2[1] = 2 -- error, innerTable2 is undefined (nil). you can not index a nil value
So, consider your code carefully. If you receive an error in this manner, it is telling you exactly what the code is doing, giving you a place to start debugging. If you do not know how to solve your problem, you need to share your code (and possibly error traceback) for us to see.
But basically: you create a table, which contains possibly multiple functions (among other things). You access those functions via the table, with either the dot (.) or colon (:) syntax, depending on situation. togFox's example in his post above illustrates how to store functions within a table, and how to access said functions later in another file.