pythonjsontiledarcade

Read Tiled Map object properties with python arcade library


i am making a platformer game with python arcade and Tiled Map Editor but i havent find a way i can access object custom attributes

i loaded tiled map like this:

self.tile_map = arcade.load_tilemap("TiledMap.json", scaling=1)

and i tried access object custom attributes like this:

self.enemies = self.tile_map.sprite_lists.get("Enemies", arcade.SpriteList())
for e in self.enemies:
    print(e.custom_attribute)

but then i got this error:

Traceback (most recent call last):
File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 429, in <module>    main()
File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 425, in main    
    arcade.run()
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\arcade\window_commands.py", line 323, in run  
    pyglet.app.run()
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\app\__init__.py", line 107, in run     
    event_loop.run(interval)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\app\base.py", line 184, in run
    timeout = self.idle()
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\app\base.py", line 245, in idle        
    self.clock.call_scheduled_functions(dt)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\clock.py", line 277, in call_scheduled_functions
    item.func(now - item.last_ts, *item.args, **item.kwargs)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\arcade\application.py", line 318, in _dispatch_updates
    self.dispatch_event('on_update', delta_time)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\window\__init__.py", line 1361, in dispatch_event
    super().dispatch_event(*args)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\event.py", line 427, in dispatch_event 
    raise e
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\event.py", line 422, in dispatch_event 
    if getattr(self, event_type)(*args):
File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 223, in on_update
    self.update_enemies()
File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 418, in update_enemies
    print(e.custom_attribute)
AttributeError: 'Sprite' object has no attribute 'custom_attribute'

is there any way i can access custom_attribute?


Solution

  • Short answer is no: the type arcade.Sprite() does not (as the error states) contain a custom_attribute attribute. See the docs here: https://api.arcade.academy/en/latest/api/sprites.html#arcade-sprite

    A quick google of the arcade library (which I'm not familiar with at all) doesn't show any hits for anything that might contain a custom_attribute attribute. Where did you get it from, are you setting it anywhere?