That being said, I have another question. When I was programming in AS3, I could create a separate player entity and add it to the field like "add(new Player)" This would add that player to the game as well as all of the properties that are contained in it. In this case the "Player Class" Would hold all data pertaining to the player itself, Including hitbox, health, controls, and even collisions. This made it especially easy to work with since you could then add a "player" to the level and manipulate the "Player" itself as if it were an object.I was wondering if I could do something like this:
You can do better if you use this other code:Ryne wrote: player = {
image = love.graphics.newImage("MYIMAGELOCATION"),
hp = 15,
strength = 20,
magic = 25
}
[/code]
So I was curious how I could then add a "player" to the grid and assign those table attributes to it.This way you will separate resource loading (the image) from data structure (the player). This structure will also allow you to create multiple entities that share the same image (for example, enemies)Code: Select all
player_image = love.graphics.newImage("MYIMAGELOCATION") player = { image = player_image hp = 15, strength = 20, magic = 25 }
Löve does very little automatically.The way it translates in my head is that I could be able to type something to allow me to add "player" to the field, including its image and it would automatically have all of those attributes. Though that doesn't seem to be the case.
The code above, literally, just means: "create a lua table called player with the folowing fields inside: hp is the integer 25, strength is the integer 10, image is this image loaded from this location, and magic is the integer 25).
The only "automatic" thing that this gives you is the opportunity to change that table. You will be able to do, for example:That just means that you changed the value of one of the fields of the table "player". Tables are important since they allow you to structure data in a meaningful way (to you, the programmer). But that's pretty much all about them. In other words, the code above is very similar to this other code:Code: Select all
player.hp = 26
On this case, I just created some variables and assigned them some values, instead of creating one variable "player" and assigning it a table. Generally using tables is better - if anything, it forces some consistency (you can have PLAYER_HEALTH and PlayerHP and player_magic, but with a table everything begins with "player").Code: Select all
player_image = image player_hp = 15 player_strength = 20 player_magic = 25 }
What I mean is that will not "automatically draw" anything. Tables, like variables, are a "mental thing", not a "visual thing". They exist only on the computer memory, but will not appear on the screen.
You have to explicitly tell it to do so, using the functions available on love.graphics. If you want to draw the text "25 hp", you have to use love.graphics.print. And you have to fill it in with the correct calculated coordinates (x,y) where you want it printed. And attach the string " hp" to the number 25, which is all you have inside player. Oh, and probably set a font to be used before that.
So I was just wondering if there were a way to create sort of a "player" entity in the love engine, and use it the same way, or if there was the possibility of a Love Equivalent. I'm just having some trouble understanding how it works with Love/Lua.
Thanks to Kikito for the response thus far.