Global is easier to grasp for beginners, but later on you'll want to do everything local. Otherwise you will make programming mistakes (variable gets overwritten in some completely unrelated function without you realizing it).
Local is actually pretty easy once you know how to structure your code and data. You need to know how to divide your program into different "components". with good organization, everything falls into place.
When it comes to levels, sure, place those into separate .lua files, but basic game handling code should remain in a single place and not withing level-specific modules.
A sensible system would be something like:
*Series of .lua files that handle general game logic such as object updating, collisions, etc.*
*Set of .lua files for the code pertaining to each object in your game*
*Set of .lua files that describe level data, possibly referencing objects from other .lua files but not containing any of their code*
Relationship-wise, it would look like:
Game handling -> object code -> object/level data
You may want to have .lua files that have level-specific code. However, your game loop should not "live" inside this code. Instead, this code should provide setup routines and possibly some special behaviors that your base code calls into. Additionally, this code may reference some of your objects from other .lua files.
A good class or entity/component system will help a lot. By using "require" to pull in your .lua files, code only gets loaded once and then can be accessed any time - so you can simply "require" the .lua file for some class you have and then instantiate your game objects from there.
Let me know if this is too vague/confusing and I'll break things down for you.
Stuck with local variables
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Stuck with local variables
Dear DaedalusYoung and Grubby,
The others are right. Lua programmers are not a "cliquish bunch", what they're saying to you is accepted wisdom in the whole field of programming.
To put it simply, the trouble you have isn't because of locals, it's the way you organise your code. Don't worry, everyone starts out that way. Let me show you a diagram:
The others are right. Lua programmers are not a "cliquish bunch", what they're saying to you is accepted wisdom in the whole field of programming.
To put it simply, the trouble you have isn't because of locals, it's the way you organise your code. Don't worry, everyone starts out that way. Let me show you a diagram:
Help us help you: attach a .love.
Re: Stuck with local variables
In case anyone's wondering what a big project looks like (because many people seem to think that a dozen files and a few thousand lines is quite big) the project I am currently working on for my dayjob (not Lua, but PHP) currently clocks in at over 20.000 files of code (including libraries and the framework).
The reason you don't see the point in using locals and organising things neatly is generaly because you've never seen what a really big project that requires these things looks like There's a big difference between a guy making a little game for himself and a team building professional software, and a lot of the advice you'll see on the internet is geared to prepare you for the latter and might not make sense if you've never had to build something really big before.
But trust me, if you grow further in software development, you'll start to understand just how important these things are and you'll be really happy that you practiced what sounded like silly advice when you first heard it.
The reason you don't see the point in using locals and organising things neatly is generaly because you've never seen what a really big project that requires these things looks like There's a big difference between a guy making a little game for himself and a team building professional software, and a lot of the advice you'll see on the internet is geared to prepare you for the latter and might not make sense if you've never had to build something really big before.
But trust me, if you grow further in software development, you'll start to understand just how important these things are and you'll be really happy that you practiced what sounded like silly advice when you first heard it.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Stuck with local variables
Alright, but then the question is, how do I organise my code so I do't run into this problem?
If I have one file that loads in a level, then how does another file that handles collisions access that level's data? I pass arguments to the collision file, of course, but what arguments must I pass then? All level data, or do I need to write code so only the relevant tileX, tileY, tileWidth and tileHeight get passed?
--
I was trying to organise my code, but I suppose the way I did that was not compatible with the use of locals. For example, I tried putting all player related data in the table player, I'd have all images in a table img. Then the collision file can just see if player.x collides with tile.x and each gamestate's own draw() function (menu.draw(), level.draw(), endboss.draw(), congraturation.draw(), etc.) could just grab whatever img.var it needed.
But if menu.lua, level.lua, etc. can not access table img, then somewhere there's a problem. If the problem is not that the table is local to an unrelated scope, then surely the problem is that I've got a nice, neat list of images, and they need to be broken up into an... unholy mess of various tables holding images only specific images to their gamestate?
If I have one file that loads in a level, then how does another file that handles collisions access that level's data? I pass arguments to the collision file, of course, but what arguments must I pass then? All level data, or do I need to write code so only the relevant tileX, tileY, tileWidth and tileHeight get passed?
--
I was trying to organise my code, but I suppose the way I did that was not compatible with the use of locals. For example, I tried putting all player related data in the table player, I'd have all images in a table img. Then the collision file can just see if player.x collides with tile.x and each gamestate's own draw() function (menu.draw(), level.draw(), endboss.draw(), congraturation.draw(), etc.) could just grab whatever img.var it needed.
But if menu.lua, level.lua, etc. can not access table img, then somewhere there's a problem. If the problem is not that the table is local to an unrelated scope, then surely the problem is that I've got a nice, neat list of images, and they need to be broken up into an... unholy mess of various tables holding images only specific images to their gamestate?
Re: Stuck with local variables
Well that's the hard part really, and the one that requires the practice
A good start would be to organise your level into a table and then passing that into the collision handler.
Also, a player's images should probably go into the player table. And the player table probably needs a draw method to draw itself onto the screen. You might even split those up to a deeper level by having a player table that includes an input table (that handles keyboard input), a draw table (that handles drawing the player) and a collision table (that handles movement) along with whatever else you need.
The idea is to make small chunks of functionality that have access only to the data they need to run and nothing else. So if your draw function needs an image, chug that image into that function table.
It intuitively makes sense to sort data by its type (all images in a list, all enemies in a list, etc) but that's only useful if you're a data storage engine. You're building a program, so you need to sort data by purpose. If your draw function needs access to the player's data and the player's images, pass those to it instead of the full list of images and entities. It'll be much more adaptable that way.
Good luck.
A good start would be to organise your level into a table and then passing that into the collision handler.
Also, a player's images should probably go into the player table. And the player table probably needs a draw method to draw itself onto the screen. You might even split those up to a deeper level by having a player table that includes an input table (that handles keyboard input), a draw table (that handles drawing the player) and a collision table (that handles movement) along with whatever else you need.
The idea is to make small chunks of functionality that have access only to the data they need to run and nothing else. So if your draw function needs an image, chug that image into that function table.
It intuitively makes sense to sort data by its type (all images in a list, all enemies in a list, etc) but that's only useful if you're a data storage engine. You're building a program, so you need to sort data by purpose. If your draw function needs access to the player's data and the player's images, pass those to it instead of the full list of images and entities. It'll be much more adaptable that way.
Good luck.
Re: Stuck with local variables
Plu is right,
images make more sense where you need them, so for the player put them into the player table and so on.
overall I already send you a link with my game code, things like that are all there.
It's not perfect but you should get a good idea on how to do stuff.
(Github)
Good luck
images make more sense where you need them, so for the player put them into the player table and so on.
overall I already send you a link with my game code, things like that are all there.
It's not perfect but you should get a good idea on how to do stuff.
(Github)
Good luck
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Stuck with local variables
And if you don't mind posting your source (or at least contact me inbox) I can provide some advises, with respect to my humble experience.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Stuck with local variables
Ok, i see. So instead of img.player and img.enemy I should use player.img and enemy.img. I can get with that.
But then there's still things, like the player shoots a bullet that hits an enemy. So the bullet has to know the location of the player so it knows where to spawn and it needs to know the location of the enemy, so it knows when it hit it. (Or the enemy needs to know the location of the bullet) So, do I pass the player and enemy data on to the bullet? But there are no links, my main.lua does not know anything about the player's local variables, and the bullet does not know anything about the main local variables, and the enemy does not know anything.
And even if it did, I can only imagine it being something like this:
And then just return the entire player table from player.update() and the enemy table from enemy.update(). Which seems cumbersome and counterintuitive. I would/should only ever pass dt into an update function, but then I don't know how to get data from one scope to another.
But then there's still things, like the player shoots a bullet that hits an enemy. So the bullet has to know the location of the player so it knows where to spawn and it needs to know the location of the enemy, so it knows when it hit it. (Or the enemy needs to know the location of the bullet) So, do I pass the player and enemy data on to the bullet? But there are no links, my main.lua does not know anything about the player's local variables, and the bullet does not know anything about the main local variables, and the enemy does not know anything.
And even if it did, I can only imagine it being something like this:
Code: Select all
function love.update(dt)
local tempplayer = player.update(dt)
local tempenemy = enemy.update(dt)
bullet.update(dt, tempplayer, tempenemy)
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Stuck with local variables
I'd do something like this:
main.lua:
bullet.lua:
main.lua:
Code: Select all
local player = require 'player'
local enemy = require 'enemy'
local bullet = require 'bullet'
[...]
function love.update(dt)
player.update(dt)
enemy.update(dt)
bullet.update(dt)
end
Code: Select all
local player = require 'player'
local enemy = require 'enemy'
local bullet = {}
[...]
function bullet.update(dt)
-- use player and enemy here
end
[...]
return bullet
Help us help you: attach a .love.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Stuck with local variables
But does that update then? I have an initial player.x value in player.lua, but it likely changes in player.update(). Can bullet.lua see that change?
Who is online
Users browsing this forum: No registered users and 4 guests