Advertisement
troll_eater

Prefab Import/Export

May 3rd, 2025 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @function scr_ExportPrefab()
  2. /// @desc Exports all objects and tile data in the current room into a JSON prefab structure.
  3. function scr_ExportPrefab() {
  4.     // === Object Types to Ignore During Export ===
  5.     var skip_objects = [
  6.         oRoomOrigin, oPrefabBuilderControl, oCamera, oNavigation,
  7.         oCursor, oHUD, oItemManager, oInventory, oTech
  8.     ];
  9.  
  10.     // === Gather Object Data ===
  11.     var prefab_objects = [];
  12.  
  13.     with (all) {
  14.         if (!array_contains(skip_objects, object_index)) {
  15.             var entry = {
  16.                 object_id: object_index,
  17.                 type: object_get_name(object_index),
  18.                 x: x,
  19.                 y: y,
  20.                 xscale: image_xscale,
  21.                 yscale: image_yscale
  22.             };
  23.             show_debug_message("Exporting object: " + entry.type + " @ " + string(entry.x) + "," + string(entry.y));
  24.             array_push(prefab_objects, entry);
  25.         }
  26.     }
  27.  
  28.     // === Gather Tile Data from "Tiles_Visual" Layer ===
  29.     var tile_layer = "Tiles_Visual";
  30.     var tile_data = [];
  31.  
  32.     var tilemap_id = layer_tilemap_get_id(tile_layer);
  33.     if (tilemap_id != -1) {
  34.         var tile_w = tilemap_get_tile_width(tilemap_id);
  35.         var tile_h = tilemap_get_tile_height(tilemap_id);
  36.         var grid_w = room_width div tile_w;
  37.         var grid_h = room_height div tile_h;
  38.         var tile_columns = 8; // Hardcoded: change if tileset changes
  39.  
  40.         for (var tx = 0; tx < grid_w; tx++) {
  41.             for (var ty = 0; ty < grid_h; ty++) {
  42.                 var tile_index = tilemap_get(tilemap_id, tx, ty);
  43.                 if (tile_index != 0) {
  44.                     var tile_entry = {
  45.                         x: tx,
  46.                         y: ty,
  47.                         tile_x: tile_index mod tile_columns,
  48.                         tile_y: tile_index div tile_columns
  49.                     };
  50.                     show_debug_message("Exporting tile (" + string(tile_entry.tile_x) + "," + string(tile_entry.tile_y) + ") at " + string(tile_entry.x) + "," + string(tile_entry.y));
  51.                     array_push(tile_data, tile_entry);
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.     // === Create Final Prefab Structure ===
  58.     var prefab = {
  59.         objects: prefab_objects,
  60.         tiles: tile_data
  61.     };
  62.  
  63.     // === Write JSON to Disk ===
  64.     var json = json_stringify(prefab, false);
  65.     var file = file_text_open_write("prefab_output.json");
  66.     file_text_write_string(file, json);
  67.     file_text_close(file);
  68.  
  69.     show_debug_message("Export complete: prefab_output.json");
  70. }
  71.  
  72.  
  73. /// @function scr_BuildRoomFromPrefab(prefab, x_offset, y_offset)
  74. /// @desc Builds a room layout from a JSON prefab, placing objects and tiles with pixel offsets.
  75. /// @param prefab {struct} - The parsed JSON prefab data
  76. /// @param x_offset {int} - X pixel offset to position the prefab
  77. /// @param y_offset {int} - Y pixel offset to position the prefab
  78. function scr_BuildRoomFromPrefab(prefab, x_offset, y_offset) {
  79.     show_debug_message("Spawning prefab at offset: " + string(x_offset) + "," + string(y_offset));
  80.  
  81.     // === OBJECT SPAWN ===
  82.     for (var i = 0; i < array_length(prefab.objects); i++) {
  83.         var obj = prefab.objects[i];
  84.  
  85.         if (object_exists(obj.object_id)) {
  86.             var inst = instance_create_layer(x_offset + obj.x, y_offset + obj.y, "Instances", obj.object_id);
  87.             inst.image_xscale = obj.xscale;
  88.             inst.image_yscale = obj.yscale;
  89.         } else {
  90.             show_debug_message("WARNING: Invalid object ID " + string(obj.object_id));
  91.         }
  92.     }
  93.  
  94.     // === TILE PLACEMENT ===
  95.     var tilemap_id = layer_tilemap_get_id("RoomTiles");
  96.     if (tilemap_id == -1) {
  97.         show_debug_message("ERROR: RoomTiles layer not found.");
  98.         return;
  99.     }
  100.  
  101.     tilemap_clear(tilemap_id, 0);
  102.  
  103.     var tile_w = tilemap_get_tile_width(tilemap_id);
  104.     var tile_h = tilemap_get_tile_height(tilemap_id);
  105.     var tile_columns = 8;
  106.     var max_tiles = tile_columns * tile_columns;
  107.  
  108.     for (var j = 0; j < array_length(prefab.tiles); j++) {
  109.         var tile = prefab.tiles[j];
  110.  
  111.         // Add *pixel* offset to grid cell
  112.         var cx = tile.x + floor(x_offset / tile_w);
  113.         var cy = tile.y + floor(y_offset / tile_h);
  114.         var tile_index = tile.tile_y * tile_columns + tile.tile_x;
  115.  
  116.         if (tile_index >= 0 && tile_index < max_tiles) {
  117.             tilemap_set(tilemap_id, cx, cy, tile_index);
  118.             show_debug_message("Placed tile (" + string(tile.tile_x) + "," + string(tile.tile_y) + ") → index " + string(tile_index) + " at cell " + string(cx) + "," + string(cy));
  119.         } else {
  120.             show_debug_message("SKIPPED invalid tile index " + string(tile_index));
  121.         }
  122.     }
  123.  
  124.     show_debug_message("Prefab build complete.");
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement