## Display Groups?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
mastastealth
Prole
Posts: 30
Joined: Thu Jul 03, 2008 2:44 am
Location: Barranquilla, Colombia
Contact:

### Display Groups?

Been a while since I've been here. Recently I've been working on my first Android game using Corona, but thinking about the future remembered about the awesome Love2D project in hopes of one day porting my game to PC's as well!

Now, taking a quick skim through the wiki, and my own code, I believe 70-80% of it can be practically used as-is or with a few function swaps (image loading, etc.) However there's one bit that I didn't see an alternative for and that was display groups. They seem to act almost like "layers" for the scene, and believe are just tables with some extra metadata stuff? They allow manipulation of various display objects at the same time. Is there something similar, planned, or possible to add in love?

thelinx
The Strongest
Posts: 848
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

### Re: Display Groups?

Take a look at SpriteBatches.

mastastealth
Prole
Posts: 30
Joined: Thu Jul 03, 2008 2:44 am
Location: Barranquilla, Colombia
Contact:

### Re: Display Groups?

Hmm, close, though I can probably modify my code to use them properly.

However, group display seems to be a little more general. For example, I see in SpriteBatch, to add an image I also have to give coordinates. Although this might work in my case (with modifications), but I don't specifically need to add coordinates to simply "add it to the group". Also, in my specific case of a mancala-like game, each of the 12 pits serves as a group, while the objects I stick in them are the stones. So, when I play, a for loop empties out the current pit's group into the player's "hand" group (an object can't be in 2 display groups at once. So adding to the hand automatically removes from the pit) and then drops 1 stone (with updated position) into a new pit. Does Spritebatch have a single remove function? Or can sprites be in multiple batches at the same time (that way I can add all current stones to new hand, clear old pit, add stones to new pits, then clear hand)?

For memory's sake, I made each of the 48 stones (the amount in your typical mancala game) is an individual object that specifcally is moved around the board (until captured) instead of deleting old objects, and recreating them over and over; so I'd like this "unique instance" ability to remain if possible.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Display Groups?

A SpriteBatch is a collection of Quads to be applied to an image, it sound different than what you're using right now, though I suppose that could also be emulated with some clever tables.
To answer your question, you can't remove an entry from a SpriteBatch due to the way they work.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Display Groups?

mastastealth wrote:Hmm, close, though I can probably modify my code to use them properly.

However, group display seems to be a little more general. For example, I see in SpriteBatch, to add an image I also have to give coordinates. Although this might work in my case (with modifications), but I don't specifically need to add coordinates to simply "add it to the group". Also, in my specific case of a mancala-like game, each of the 12 pits serves as a group, while the objects I stick in them are the stones. So, when I play, a for loop empties out the current pit's group into the player's "hand" group (an object can't be in 2 display groups at once. So adding to the hand automatically removes from the pit) and then drops 1 stone (with updated position) into a new pit. Does Spritebatch have a single remove function? Or can sprites be in multiple batches at the same time (that way I can add all current stones to new hand, clear old pit, add stones to new pits, then clear hand)?

For memory's sake, I made each of the 48 stones (the amount in your typical mancala game) is an individual object that specifcally is moved around the board (until captured) instead of deleting old objects, and recreating them over and over; so I'd like this "unique instance" ability to remain if possible.
What's the advantage of using that versus using plain tables?

Code: Select all

local pits = {}
for i=1, 12 do table.insert(pits, {}) end
...

-- insert into pit 12
table.insert(pits[12], ball)

-- get all the balls in pit 12
pits[12]

-- remove all the balls from pit 12
pits[12] = {}

When I write def I mean function.

mastastealth
Prole
Posts: 30
Joined: Thu Jul 03, 2008 2:44 am
Location: Barranquilla, Colombia
Contact:

### Re: Display Groups?

In my case, I think I might of actually been able to use normal tables instead (now that I look it over). Actually, I was thinking that with that "MiddleClass" library I would just create the most-basic (i.e. these normal table functions with a different name) version of these "Display Group" tables, just for compatibility's sake. (Wouldn't have to go through all my code to fix to normal tables).

However, if I remember correctly, one can manipulate the display group to modify all the display objects at once, which I don't think you can do with just normal tables (without having to write up special functions).

For instance, let's say (using your example) that I want all the balls in the pits to become transparent because I opened a menu or something. I would be able to say

Code: Select all

pits.opacity = 0.5

or something like that and that's it. With the tables I assume I'd need a for loop to manually set each ball's opacity individually. Correct? Same thing for moving, I can update the pits.x and pits.y and have all the children object balls adjust relatively to that, instead of one by one. Lastly, like I mentioned, inserting into a display group vs a table let's me forget about having to clear the old table, because it's automatic.

Of course you can probably make these functions yourself for the table, which I believe is what I first stated with my idea above about using the MiddleClass library, but as far as I know, your standard Lua table couldn't do the 3 above mentioned examples right off-the-bat...?

thelinx
The Strongest
Posts: 848
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

### Re: Display Groups?

Code: Select all

containerTable = {}
setmetatable(containerTable, {
__newindex = function(tbl, key, val)
for _, child in pairs(tbl) do
child[key] = val
end
end
})

However, if the containerTable is a class created with Middleclass, doing this would overwrite that metatable.
Fortunately, Middleclass allows you to create metafunctions in your regular class definition, like so:

Code: Select all

function containerTable.__newindex(tbl, key, val)
...
end


### Who is online

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