Tilengine - The 2D retro graphics engine forum
Objects list : How to use it? - Printable Version

+- Tilengine - The 2D retro graphics engine forum (http://tilengine.org/forum)
+-- Forum: English forums (http://tilengine.org/forum/forumdisplay.php?fid=3)
+--- Forum: Support (http://tilengine.org/forum/forumdisplay.php?fid=7)
+--- Thread: Objects list : How to use it? (/showthread.php?tid=982)

Pages: 1 2 3 4


RE: Objects list : How to use it? - System64 - 12-20-2020

with TLN_AddSpriteToList() and TLN_RemoveSpriteToList()?

TLN_AddSpriteToList() is mentionned in the references but nothing about it


RE: Objects list : How to use it? - megamarc - 12-20-2020

Hi,
That's an error in the documentation, sorry. TLN_AddSpriteToList doesn't exist, It should say TLN_AddTileObjectToList(). This function is used in case you want to populate a TLN_ObjectList by hand at runtime. However you usually will use TLN_LoadObjectList() on a .tmx file with an object layer.


RE: Objects list : How to use it? - System64 - 12-20-2020

TLN_AddTileObjectToList()

isn't it for adding Tiles as objects in the List?
also, seems there is no function to remove objects from the list, so if my ennemy generates a lot of bullets for exemple, isn't there a risk the list grows more and more?


RE: Objects list : How to use it? - megamarc - 12-20-2020

This is for ading tile objects to an objectlist, in other words, to build a bitmap-based tileset (.tsx) at runtime. That's why you don't se a "destroy/delete" companion function. Once created, the tileset remains immutable until its final deletion. This function is used by the .tsx loader, that's you will usually do.

It's NOT to manage an active actors llist. The actor list is a gameplay construction and must be provided by the game itself. Tilengine is a graphics engine, so it will draw, but it's not a game engine, so it won't managed gameplay mechanics for you.

Take a look at python platformer project. The active actors list is entirely managed in python, Tilengine itself doesn't know nothing about it.


RE: Objects list : How to use it? - System64 - 12-20-2020

I think I found important things about what I want in the cide

Code:
def load_objects(file_name, layer_name, first_gid):
""" loads tiles in object layer from a tmx file.
Returns list of Item objects """
tree = ET.parse(file_name)
root = tree.getroot()
for child in root.findall("objectgroup"):
name = child.get("name")
if name == layer_name:
item_list = list()
for item in child.findall("object"):
gid = item.get("gid")
if gid is not None:
x = item.get("x")
y = item.get("y")
item_list.append(Item(int(gid) - first_gid, int(x), int(y)))
return item_list
return None

Code:
class Item(object):
""" Generic item declared in tilemap object layer awaiting to spawn """
Opossum, Eagle, Frog = range(3)
def __init__(self, item_type, x, y):
self.type = item_type
self.x = x
self.y = y
self.alive = False

def try_spawn(self, x):
""" Tries to spawn an active game object depending on screen position and item type """
if self.alive is False and x < self.x < x + WIDTH:
self.alive = True
if self.type is Item.Eagle:
Eagle(self, self.x, self.y - Eagle.size[1])
elif self.type is Item.Opossum:
Opossum(self, self.x, self.y - Opossum.size[1])

class Actor(object):
""" Generic active game entity base class """
spriteset = None
def __init__(self, item_ref, x, y):
self.x = x
self.y = y
self.sprite = engine.sprites[engine.get_available_sprite()]
self.animation = engine.animations[engine.get_available_animation()]
self.sprite.setup(self.spriteset)
self.item = item_ref
actors.append(self)

def __del__(self):
self.animation.disable()
self.sprite.disable()
if self.item is not None:
self.item.alive = False

def kill(self):
""" definitive kill of active game entity, removing from spawn-able item list too """
world.objects.remove(self.item)
self.item = None
actors.remove(self)

Code:
class World(object):
""" world/play field entity """
def __init__(self):
self.foreground = engine.layers[0]
self.background = engine.layers[1]
self.clouds = 0.0
self.foreground.setup(Tilemap.fromfile("layer_foreground.tmx"))
self.background.setup(Tilemap.fromfile("layer_background.tmx"))
self.x = 0
self.x_max = self.foreground.width - WIDTH
self.objects = load_objects("assets/layer_foreground.tmx", "Capa de Objetos 1", 973)
engine.set_background_color(self.background.tilemap)
actors.append(self)

def pick_gem(self, tiles_list):
""" updates tilemap when player picks a gem """
for tile_info in tiles_list:
if tile_info.type is Tiles.Gem:
self.foreground.tilemap.set_tile(tile_info.row, tile_info.col, None)
Effect(tile_info.col*16, tile_info.row*16, spriteset_vanish, seq_vanish)
sounds.play("pickup", 1)
break

def update(self):
""" updates world state once per frame """
oldx = self.x

if player.x < 240:
self.x = 0
else:
self.x = int(player.x - 240)
if self.x > self.x_max:
self.x = self.x_max
self.clouds += 0.1

if self.x is not oldx:
self.foreground.set_position(self.x, 0)
self.background.set_position(self.x/8, 0)

# spawn new entities from object list
for item in self.objects:
item.try_spawn(self.x)

return True

But I have the impression that Actor.remove and world.object.remove are indefined
Also, seems I need ElementTree for the code, something I probably don't have in other languages. Should I use an external library for that?


RE: Objects list : How to use it? - megamarc - 12-21-2020

actorsand world.objects are standard Python lists. They have the remove() method to remove items. That's why you can't find their definitions.

Python list reference:
https://docs.python.org/3/tutorial/datastructures.html
  • Actor is the base class for any game entity (everything that lives is an Actor)
  • actors is the active actors list populated with Actor instances
  • world.objects is the object list loaded from the .tmx file (object layer)
ElementTree is part of Python standard library for loading XML files:
https://docs.python.org/3/library/xml.etree.elementtree.html

When I made this sample project, Tilengine couldn't yet load object layers from .tmx files, so I used Python xml facility to do it. Now it can load TLN_ObjectList by itself, you don't need to load it separately anymore.


RE: Objects list : How to use it? - System64 - 12-21-2020

So I should treat this Tilengine's Object list as a common list? So I can use the language's list management functions?


RE: Objects list : How to use it? - megamarc - 12-21-2020

Not directly. You must create a native python list, and use TLN_GetListObject() to iterate over all elements of the TLN_ObjectList returned, cloning each instance of TLN_ObjectInfo data returned, and adding them to the python list.


RE: Objects list : How to use it? - System64 - 12-21-2020

I guess it's the same principle for all programming languages? Now I understand better I think

Creating the list with Tilengine with TLN_LoadObjectList

Creating a list with the language

put everything from the Tilengine list into the language's list

And if my ennemy creates bullets, I have to add a slot into the language's list and when it's destroyed, remove it from the language's list by querying its id


RE: Objects list : How to use it? - megamarc - 12-21-2020

Yes! That's the way to go :-)

You usually will manage two lists:
  • The map "landmarks" list. This is the data you load with TLN_LoadObjectList(). Those are the special points in your map (spawn points, event triggers, etc). It is more or less immutable (static) as it covers special points on he map.
  • The active actors list: this one starts empty, but it's highly dynamic. When you spawn something (enemy, bullet, smoke effect, whatever), you add it there. And when you delete something, remove from this list.
Sometimes you'll need feedback from the actors list to the landmarks list: for example, when a spawn point creates an enemy at a specific place, and the player defeats the enemy, you will have to disable (or delete) that spawn point. If not, the enemy will resurrect each time the player goes to that zone again. This is not what you expect: once an enemy is defeated, it's gone forever. You can also remove the spawn point as soon as it spawns the enemy, but there's the risk that the player "kills" the enemy just letting it go offscreen. If the player hasn't legally killed the enemy, it should respawn again if the player goes back. 

I have this effect into account in my sample project "python platformer". When the player kills an enemy -removing it from the actors list-, the spawn point that created it is also removed from the "object list".