flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.28k stars 913 forks source link

[flame_tiled] Null check on `tileData` fails for infinite maps #2855

Open benni-tec opened 1 year ago

benni-tec commented 1 year ago

Current bug behavior

A null check operator exception is thrown when trying to load (cache) infinite tiled maps

Expected behavior

The map is loaded in chunks or the chunks are consoladated

Steps to reproduce

Simply use an infinite map.

Flutter doctor output

[√] Flutter (Channel stable, 3.13.9, on Microsoft Windows [Version 10.0.22621.2428], locale de-DE)
    • Flutter version 3.13.9 on channel stable at C:\Users\xxxxx\Coding\_sdks\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d211f42860 (3 weeks ago), 2023-10-25 13:42:25 -0700
    • Engine revision 0545f8705d
    • Dart version 3.1.5
    • DevTools version 2.25.0

[√] Windows Version (Installed version of Windows is version 10 or higher)

[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.7.34202.233
    • Windows 10 SDK version 10.0.22621.0

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[√] IntelliJ IDEA Ultimate Edition (version 2023.2)
    • IntelliJ at C:\Users\xxxxx\AppData\Local\Programs\IntelliJ IDEA Ultimate
    • Flutter plugin version 76.2.4
    • Dart plugin version 232.10072.19

[√] VS Code (version 1.84.0)
    • VS Code at C:\Users\xxxx\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22621.2428]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 119.0.6045.124
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 119.0.2151.58

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

More environment information

Log information

ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
#0      OrthogonalTileLayer.cacheTiles (package:flame_tiled/src/renderable_layers/tile_layers/orthogonal_tile_layer.dart:25:36)
#1      FlameTileLayer.refreshCache (package:flame_tiled/src/renderable_layers/tile_layers/tile_layer.dart:181:5)
#2      RenderableTiledMap._refreshCache (package:flame_tiled/src/renderable_tile_map.dart:365:13)
#3      new RenderableTiledMap (package:flame_tiled/src/renderable_tile_map.dart:64:5)
#4      RenderableTiledMap.fromTiledMap (package:flame_tiled/src/renderable_tile_map.dart:300:12)

More information

This bug is caused because (as far as I can see all) FlameTileLayer's force-unwrap layer.tileData however with the current tiled implementation this can be null if the map is infinite and therefore split into chunks.

I see two possible solutions:

  1. Aggregate all chunks into a single matrix (this would be sufficient for my use case) however with very large maps this can lead to performance issues
  2. Implement some mechanism to only render visible chunks and scope the cache/rendering appropriately. This seems like it would require quite a lot of work.

Since finite maps are fine for my use case I probably won't be able to contribute a PR to this! For the time being I think a warning in the docs that infinite maps are not support would be appropriate! (I didn't find one so far, I hope I didn't miss it 😅)

ufrshubham commented 1 year ago

Yeah, probably the quickest way to avoid confusion on user side will be to add an assert for infinite maps.