I am confused about the file structure in Love2D/Lua. Perfect outcome from this post would be a live chat, however perfect is not always possible, right?
Anyway, here's the question: I have player.lua, map.lua, main.lua so far, and I'm going to have more .lua files later on. How should I go about drawing the player? So far I have had to put declare the classes globally and put say player.draw() into main.lua. I saw other projects which didn't do the same, they simply put the drawing code to function player.draw(this, x, y) in player.lua (just an example) and that's it - the object was drawn to the screen.
So what's up, how do I handle multiple-file structure? Is there some hidden tutorial about this somewhere? I don't get how to sort it out...
P.S. Perhaps an example project with one (excluding main.lua) file that has code for an image and it's movement, loads, draws and updates it?
File structure
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
File structure
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! I don't learn very well from references, etc....
Re: File structure
You're talking about object orientation in some parts. Do a search or check the wiki, there's tons of examples. To use multiple files you just need to require them.
Code: Select all
require "player" --loads player.lua
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Re: File structure
I know that I have to include it, it is included. I'm attaching the.love file, so you would understand my question. I'm not good at explaining, I guess.
EDIT: Ideally I would like to keep code separate in files, which means no player.draw() in main.lua, in this case.
EDIT: Ideally I would like to keep code separate in files, which means no player.draw() in main.lua, in this case.
- Attachments
-
- game.love
- (3.72 KiB) Downloaded 91 times
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! I don't learn very well from references, etc....
Re: File structure
Code: Select all
--main.lua--
require 'player'
function love.load()
loadPlayer()
end
function love.update(dt)
updatePlayer(dt)
end
function love.draw()
drawPlayer()
end
The reason that nothing is happening in the .love you provided is because you're defining functions in player.lua and not calling them in main.lua, which is your main game logic.
Re: File structure
I've tried it and it still doesn't work.SudoCode wrote:Where loadPlayer(), updatePlayer(), and drawPlayer() are defined in player.lua.
The reason that nothing is happening in the .love you provided is because you're defining functions in player.lua and not calling them in main.lua, which is your main game logic.
I also don't think that it's the right way to go about it - there were other games which didn't have those functions repeated in main.lua... Also for the player.load() function, it is executed twice, which means double-loading, which doesn't make sense performance-wise in the first place.
In short, I don't think it's the best way to do it and it still doesn't work.
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! I don't learn very well from references, etc....
Re: File structure
You'll have to provided an updated .love with the new code. Also, which games, specifically? I'm not aware of any way to pass functions without calling them in main.lua.
player.load() shouldn't be executed twice unless you're calling it twice.
player.load() shouldn't be executed twice unless you're calling it twice.
Re: File structure
For example, this one, if I'm not mistaken: viewtopic.php?f=5&t=10635SudoCode wrote:You'll have to provided an updated .love with the new code. Also, which games, specifically? I'm not aware of any way to pass functions without calling them in main.lua.
player.load() shouldn't be executed twice unless you're calling it twice.
In fact, the code in that one seems rather advanced, but odd - he uses a lot of underscores...
- Attachments
-
- game.love
- (3.73 KiB) Downloaded 65 times
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! I don't learn very well from references, etc....
Re: File structure
It appears as if he just calls a function in love.load and then just runs everything through that. I'm sure you could get everything to run that way if you really wanted to, but I'm not entirely sure why you'd do such a thing or if it would be optimal. Maybe someone with more experience than I could speak to that.
The first problem with your code though is that you're calling a function (player.load) in love.load, but aren't passing any arguments to it. All of the code that happens in player.load relies on arguments that should be passed (in this case, "this"). Love won't define this.x or this.y to mean anything unless you tell it what you want the variable to be. So when you call player.load, it should be called as
which would set variable.width = 32 or variable.x = px. Same thing goes with player.update. When you call it in main.lua, you aren't telling it what "this" is to update.
Also, rename your functions. player.load will not work. It will think you're referring to the table player. Try loadPlayer.
The first problem with your code though is that you're calling a function (player.load) in love.load, but aren't passing any arguments to it. All of the code that happens in player.load relies on arguments that should be passed (in this case, "this"). Love won't define this.x or this.y to mean anything unless you tell it what you want the variable to be. So when you call player.load, it should be called as
Code: Select all
love.load()
player.load(variable, px, py)
end
Also, rename your functions. player.load will not work. It will think you're referring to the table player. Try loadPlayer.
Re: File structure
Still doesn't work.SudoCode wrote:It appears as if he just calls a function in love.load and then just runs everything through that. I'm sure you could get everything to run that way if you really wanted to, but I'm not entirely sure why you'd do such a thing or if it would be optimal. Maybe someone with more experience than I could speak to that.
The first problem with your code though is that you're calling a function (player.load) in love.load, but aren't passing any arguments to it. All of the code that happens in player.load relies on arguments that should be passed (in this case, "this"). Love won't define this.x or this.y to mean anything unless you tell it what you want the variable to be. So when you call player.load, it should be called aswhich would set variable.width = 32 or variable.x = px. Same thing goes with player.update. When you call it in main.lua, you aren't telling it what "this" is to update.Code: Select all
love.load() player.load(variable, px, py) end
Also, rename your functions. player.load will not work. It will think you're referring to the table player. Try loadPlayer.
Why wouldn't it work if it was player? I've renamed them, but I'm curious.
- Attachments
-
- game.love
- (3.76 KiB) Downloaded 59 times
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! I don't learn very well from references, etc....
Re: File structure
Here's a version that works. There's a bit of bloat still though.
It won't work because player.load refers to the table "player" with the key "load". You could store the function in the table if you wanted to though.
It won't work because player.load refers to the table "player" with the key "load". You could store the function in the table if you wanted to though.
- Attachments
-
- editedgame.love
- (3.71 KiB) Downloaded 83 times
Who is online
Users browsing this forum: No registered users and 39 guests