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.
I have a question about how lua works in general
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: I have a question about how lua works in general
And your question is?
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: I have a question about how lua works in general
Main.lua:
Coin.lua:
I'm not sure if that answers the question. (Im On mobile)
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)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Re: I have a question about how lua works in general
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 playertogFox 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)
Re: I have a question about how lua works in general
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: I have a question about how lua works in general
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?dusoft wrote: ↑Sun Oct 08, 2023 7:57 am https://www.lua.org/pil/16.html
See the explanation about colon functions here.
-
- Party member
- Posts: 563
- Joined: Wed Oct 05, 2016 11:53 am
Re: I have a question about how lua works in general
Well, as the error warns you, you can not index a function. To illustrate it, consider this minimum example:
The same goes for situations, like trying to index a boolean or an undefined variable.
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.
Code: Select all
local function myFunction() end -- an empty function that does nothing
myFunction() -- ok
myFunction.someValue() -- error, can not index a function
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
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.
Who is online
Users browsing this forum: Bing [Bot] and 3 guests