[Solved] Creating player's and adding them to a grid?

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.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

[Solved] Creating player's and adding them to a grid?

Post by Ryne »

Hi, This was a question I asked Kikito and he suggested I post it on the forums.
I was wondering if I could do something like this:
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.
You can do better if you use this other code:

Code: Select all

player_image = love.graphics.newImage("MYIMAGELOCATION")

player = { 
    image = player_image
    hp = 15,
    strength = 20,
    magic = 25
}
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)
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.
Löve does very little automatically.

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:

Code: Select all

player.hp = 26
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_image = image
player_hp = 15
player_strength = 20
player_magic = 25
}
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").

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

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.
Last edited by Ryne on Sat Oct 23, 2010 3:55 am, edited 1 time in total.
@rynesaur
Torquil
Prole
Posts: 3
Joined: Thu Oct 21, 2010 2:42 pm

Re: Creating player's and adding them to a grid?

Post by Torquil »

There are a few 'engines' around that support adding players/entities like that.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Creating player's and adding them to a grid?

Post by vrld »

The misconception here is that LÖVE is a game engine. It is more of a framework for handling graphics, sound and such. It provides you with the tools to (very easily and effectively) build a game engine.
So in order to to add a player to a field with

Code: Select all

field:add(player)
you have to actually define what you mean by that. That is, you have to create a class for fields and implement the add-method so that it does what you expect.
That sounds complicated and tedious, but you will get inside what happens, because you actually made it happen.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Creating player's and adding them to a grid?

Post by Ryne »

Thanks for the reply. I was wondering of doing it this way, and was wondering if it seemed efficient.

- Create a table that holds player data

Code: Select all

player{
hp = 100;
strength = 50
}
- Create a sprite/image for the player like Kikito suggested

Code: Select all

 love.graphics.newImage("assets/units/player.png")
- Make the sprite collide-able (Unsure how to do this yet)

- Everytime the sprite collides with an enemy / damaging object it would subtract 25 from player.hp

- Once player.hp = 0 remove the sprite from the field/play death animation/restart game.

It just seems like the way it would be programmed, I was wondering if this is how most people do something like this?
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Creating player's and adding them to a grid?

Post by Robin »

Ryne wrote:- Make the sprite collide-able (Unsure how to do this yet)
Images can't collide. They can only be drawn. There are two ways to get collisions: love.physics, which is probably overkill right now, or write it yourself. You'll probably want to do something like

Code: Select all

player = {hp = 100, strength = 50, image = love.graphics.newImage...., x = 0, y = 0, width = 32, height = 32}
in the latter case.
Ryne wrote:- Everytime the sprite collides with an enemy / damaging object it would subtract 25 from player.hp
In lotve.physics, you can set a callback. If you write it yourself, well, you already know where to put that code then.
Ryne wrote:- Once player.hp = 0 remove the sprite from the field/play death animation/restart game.
If you want to stop drawing an image, you simply do

Code: Select all

if player.hp > 0 then
    love.graphics.draw(player.image, player.x, player.y)
end
Ryne wrote:It just seemed like the way it would be programmed, I was wondering if this is how most people do something like this?
Well, something like that. :P

The forum is sprawling with projects, by the way. They make a great example base on how to do certain things. They might explain things better than any forum post could.
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Creating player's and adding them to a grid?

Post by Ryne »

Robin wrote:
Ryne wrote:- Make the sprite collide-able (Unsure how to do this yet)
Images can't collide. They can only be drawn. There are two ways to get collisions: love.physics, which is probably overkill right now, or write it yourself. You'll probably want to do something like

Code: Select all

player = {hp = 100, strength = 50, image = love.graphics.newImage...., x = 0, y = 0, width = 32, height = 32}
in the latter case.
Thanks a lot for the reply, I actually programmed basic collisions. I gave a "heart" sprite a heartx, and a hearty. They player also has a playerx, and a playery. So I just said:

Code: Select all

if playerx, playery == heartx, hearty then
player.hp = player.hp + 25
end
Seems to be working okay. I was also wondering something. I have all of my declarations and buttons programmed in "function love.update(dt)" when I try to do something like this:

Code: Select all

if love.keyboard.isDown("t") then
love.graphics.print("hey", 100, 100)
end
I don't get any errors but it wont print or draw anything that is programmed there. However in function love.keypressed it works fine

Code: Select all

function love.keypressed(key, unicode)    --change back to l if needed

	if key == 'l' then
		love.graphics.print(player[2], 100, 100)
	end
end
That works fine, as well as a print command. Any ideas why that is?
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Creating player's and adding them to a grid?

Post by Robin »

It would be cleaner to make playerx and playery into player.x and player.y, just like player.hp, because it's cleaner. For heart.x and heart.y (you'll have to create a table with the name heart first!) it's even a better idea, because it means you can easily have more than one heart without duplicating code many times over.

As for when things don't draw in love.update but do in love.keypressed, see http://love2d.org/wiki/love.run

However, using drawing code outside of love.draw is not a good programming practice. There's a reason love.update and love.draw are two separate callbacks. ;)

For example, you can't draw anything over something you've drawn in love.keypressed and the like, because love.draw has already been called at that point.

A better way of reacting to user input is setting variables.
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Creating player's and adding them to a grid?

Post by Ryne »

I assumed that would be the case. Surprisingly, programming makes a lot of sense to me, though I haven't done a lot of it. I can understand how to do something, just not necessarily know how to go about it.

One more thing. I currently have the entire game programmed in the single main.lua. Is their away to separate files and only use the main to load what I need? I would like to program a menu, so that when you select start game that I have programmed so far. Thanks again Robin!
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Creating player's and adding them to a grid?

Post by Robin »

Ryne wrote:I assumed that would be the case. Surprisingly, programming makes a lot of sense to me, though I haven't done a lot of it. I can understand how to do something, just not necessarily know how to go about it.

That's great! If you don't know how to implement something, you'll probably find out quickly by documentation or trial and error. If you don't, however, don't hesitate to ask. ;)
The reference manual and Programming in Lua are very much recommend, btw.
Ryne wrote:I currently have the entire game programmed in the single main.lua. Is their away to separate files and only use the main to load what I need?
Yes! Just put different parts in other .lua files, for example game.lua, levels.lua and map.lua. Then you have to put the following in your main.lua:

Code: Select all

require "game"
require "levels"
require "map"
You can make it a lot more complex if you want, but that's the basics.
Ryne wrote:I would like to program a menu, so that when you select start game that I have programmed so far.

Kikito will probably push his MindState :monocle:, but there are multiple ways to do gamestates. Probably the best way that's somewhat simple is using tables.

Code: Select all

menu = {} -- it could also contain information about which item is selected etc
function menu.update(dt)
    -- bladibladiblah
end

function menu.draw()
    -- bladibladiblah
end

game = {} -- it could also contain information about the map, players, etc
function game.update(dt)
    -- bladibladiblah
end

function game.draw()
    -- bladibladiblah
end

-- and then either do something like
love.update = menu.update
love.draw = menu.draw
-- or something like
function love.update(dt)
    currstate.update(dt)
end
function love.draw()
    currstate.draw()
end
currstate = menu
Ryne wrote:Thanks again Robin!
No problem. :)
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Creating player's and adding them to a grid?

Post by Ryne »

Oh wow, That's easier than I thought, which is always a good thing, Thanks a lot! :)
@rynesaur
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests