I have a question about how lua works in general

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
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

I have a question about how lua works in general

Post 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 35 times
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I have a question about how lua works in general

Post by dusoft »

And your question is?
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: I have a question about how lua works in general

Post 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
User avatar
togFox
Party member
Posts: 780
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: I have a question about how lua works in general

Post by togFox »

Main.lua:

Code: Select all

Require 'coin'
Coin.myfunction()

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)
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: I have a question about how lua works in general

Post by MaxGamz »

togFox wrote: Sun Oct 08, 2023 12:05 am Main.lua:

Code: Select all

Require 'coin'
Coin.myfunction()

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
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: I have a question about how lua works in general

Post 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
test.love
(884.2 KiB) Downloaded 62 times
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I have a question about how lua works in general

Post by dusoft »

https://www.lua.org/pil/16.html

See the explanation about colon functions here.
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: I have a question about how lua works in general

Post by MaxGamz »

dusoft wrote: Sun Oct 08, 2023 7:57 am https://www.lua.org/pil/16.html

See the explanation about colon functions here.
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?
MrFariator
Party member
Posts: 515
Joined: Wed Oct 05, 2016 11:53 am

Re: I have a question about how lua works in general

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

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 29 guests