Trying to change resource, and resource isn't loading (SOLVED)

I’ve been following the Modify atlas tutorial after searching through forum threads, but I am having no luck.

I keep failing the assert check with the error message “Failed to load resource: /!!Assets/Tiles/BattleTilesTwo.png (-3)”, Now this is a direct resource path copy like the tutorial so I have no idea why it cannot find it.

It could have something to do with finding the compiled asset but I’ve been looking though the build file and my !!Assets file is empty.

I’ve attached the code encase it I mistyped something. Please Help

local function create_buffer_from_image(filename)
	local png = assert(sys.load_resource(filename)) --This is the line giving an 
                                                    --Error message
end

--Newtexture ==  "/!!Assets/Tiles/BattleTilesTwo.png"
function StageAPI.SwapStageTexture(NewTexture)
	local PixelBuffer = create_buffer_from_image(NewTexture)
end

Is this path added to the Custom Resources field of game.project?

“In order for the engine to include custom resources in the build process, you need to specify them in the “custom_resources” key in your “game.project” settings file”

1 Like

I have done this now, and I seems to be running the whole code now, up until the last line, the actual setting of the texture. I’m now getting a crashdump error with no real feedback on what is going wrong. It’s all just errors from the SRC and Engine files

The only clue I have is a assertation fail, not in the buffer function (see above) but in
file ..\src\ddf\ddf.cpp, line 163
which I found in the return print of the create_buffer_from_image function

My inputs are
"/!!Features/Tiles/BattleTiles/BattleTileSource.t.texturesetc" for the tilesource
"/!!Assets/Tiles/BattleTilesTwo.png" for the png (Now in the custom resource)

I suspect It has something to do with how I implemented the png, as I am using the same create_buffer_function from the example I have linked.

I will attach the crash dump as well-

INFO:CRASH: Successfully wrote Crashdump to file: C:/Users/user/AppData/Roaming/Defold/_crash
ERROR:CRASH: CALL STACK:

ERROR:CRASH:  0 0x7FF629776BB0 dmCrash::GenerateCallstack D:\a\defold\defold\engine\crash\src\backtrace_win32.cpp:143
ERROR:CRASH:  1 0x7FF629777410 dmCrash::SignalHandler D:\a\defold\defold\engine\crash\src\backtrace_win32.cpp:294
ERROR:CRASH:  2 0x7FF629875CB4 raise minkernel\crts\ucrt\src\appcrt\misc\signal.cpp:547
ERROR:CRASH:  3 0x7FF62985990C abort minkernel\crts\ucrt\src\appcrt\startup\abort.cpp:71
ERROR:CRASH:  4 0x7FF629809238 common_assert_to_stderr<wchar_t> minkernel\crts\ucrt\src\appcrt\startup\assert.cpp:186
ERROR:CRASH:  5 0x7FF6298098F0 _wassert minkernel\crts\ucrt\src\appcrt\startup\assert.cpp:443
ERROR:CRASH:  6 0x7FF6293D5590 dmDDF::LoadMessage D:\a\defold\defold\engine\ddf\src\ddf\ddf.cpp:164
ERROR:CRASH:  7 0x7FF6293D5570 dmDDF::LoadMessage D:\a\defold\defold\engine\ddf\src\ddf\ddf.cpp:158
ERROR:CRASH:  8 0x7FF62946B3F0 dmGameSystem::ResTextureSetRecreate D:\a\defold\defold\engine\gamesys\src\gamesys\resources\res_textureset.cpp:185
ERROR:CRASH:  9 0x7FF6293EEFC0 dmResource::SetResource D:\a\defold\defold\engine\resource\src\resource.cpp:1324
ERROR:CRASH: 10 0x7FF629440CE0 dmGameSystem::SetTextureResource D:\a\defold\defold\engine\gamesys\src\gamesys\gamesys_resource.cpp:267
ERROR:CRASH: 11 0x7FF629452B70 dmGameSystem::SetTexture D:\a\defold\defold\engine\gamesys\src\gamesys\scripts\script_resource.cpp:1555
ERROR:CRASH: 12 0x7FF62937700E lj_BC_FUNCC <unknown>:0
ERROR:CRASH: 13 0x7FF6295CD140 lua_pcall <unknown>:0
ERROR:CRASH: 14 0x7FF62959D480 dmScript::PCallInternal D:\a\defold\defold\engine\script\src\script.cpp:1453
ERROR:CRASH: 15 0x7FF62976DDB0 dmGui::RunScript D:\a\defold\defold\engine\gui\src\gui.cpp:2034
ERROR:CRASH: 16 0x7FF629765EA0 dmGui::DispatchInput D:\a\defold\defold\engine\gui\src\gui.cpp:2288
ERROR:CRASH: 17 0x7FF62940F340 dmGameSystem::CompGuiOnInput D:\a\defold\defold\engine\gamesys\src\gamesys\components\comp_gui.cpp:2684
ERROR:CRASH: 18 0x7FF6293B31D0 dmGameObject::DispatchInput D:\a\defold\defold\engine\gameobject\src\gameobject\gameobject.cpp:2949
ERROR:CRASH: 19 0x7FF629394AB0 dmEngine::StepFrame D:\a\defold\defold\engine\engine\src\engine.cpp:1915
ERROR:CRASH: 20 0x7FF629395B40 dmEngineUpdate D:\a\defold\defold\engine\engine\src\engine.cpp:2473
ERROR:CRASH: 21 0x7FF629395ED0 dmEngine::RunLoop D:\a\defold\defold\engine\engine\src\engine_loop.cpp:83
ERROR:CRASH: 22 0x7FF62938CD00 engine_main D:\a\defold\defold\engine\engine\src\engine_main.cpp:152
ERROR:CRASH: 23 0x7FF6297EB7A4 __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
ERROR:CRASH: 24 0x7FFA70562560 BaseThreadInitThunk <unknown>:0
ERROR:CRASH: 25 0x7FFA7172AEE0 RtlUserThreadStart <unknown>:0

Can you please zip up the entire project and share it here?

Btw, you can download the modify atlas example here: examples/resource/modify_atlas at master · defold/examples · GitHub

Ok I realised what the issue was, I wan’t running the tilesource as an atlas just a raw filepath

However I have now hit another wall

The tiles look like this when I try to change them, for context this is what it usually looks like (Just recoloured).

I can confirm its reading the height and width correctly and the texture info is looking like

	local texture_info = {
		type = resource.TEXTURE_TYPE_2D,
		width = width,
		height = height,
		format = resource.TEXTURE_FORMAT_RGBA,
		compression_type = resource.COMPRESSION_TYPE_DEFAULT,
	}

I have no idea what is causing this, and yes, the orange tiles are the lower-wall and upper-wall tiles stuck together

It must have something to do with the uv’s textureset.geometries[1].uvs is outputting

  1 = 2,
  2 = 34,
  3 = 2,
  4 = 2,
  5 = 34,
  6 = 2,
  7 = 34,
  8 = 34

For a texture 320x160 pixels, which doesn’t seem right, but I don’t know what I would do to fix it.

Figured it out, heres the full code for swapping a tilesource at runtime (assuming both png’s are acting as static spritesheets)


local function create_buffer_from_image(filename)
	local png = assert(sys.load_resource(filename))
	local loaded_image = image.load(png)
	local width = loaded_image.width
	local height = loaded_image.height
	local pixels = loaded_image.buffer

	local buffer_declaration = {
		{
			name = hash("rgba"),
			type = buffer.VALUE_TYPE_UINT8,
			count = 4
		}
	}
	local pixel_buffer = buffer.create(width * height, buffer_declaration)
	local pixel_stream = buffer.get_stream(pixel_buffer, hash("rgba"))

	for y = 1, height do
		for x = 1, width do
			-- flip image vertically
			local pixels_index = ((height - y) * width * 4) + ((x - 1) * 4) + 1
			local r = pixels:byte(pixels_index + 0)
			local g = pixels:byte(pixels_index + 1)
			local b = pixels:byte(pixels_index + 2)
			local a = pixels:byte(pixels_index + 3)

			local stream_index = ((y - 1) * width * 4) + ((x - 1) * 4) + 1
			pixel_stream[stream_index + 0] = r
			pixel_stream[stream_index + 1] = g
			pixel_stream[stream_index + 2] = b
			pixel_stream[stream_index + 3] = a
		end
	end

	return pixel_buffer, width, height
end
--My StageAPI object, could just be a local function
function StageAPI.ChangeStage()
	--Put in custom resources
	local textureset = resource.get_atlas("/!!Features/Tiles/BattleTiles/BattleTileSource.t.texturesetc")
--Tilesources are considered atlas's (I'm not sure if this is mentioned anywhere)
	local texture = resource.get_texture_info(textureset.texture)
	local pixel_buffer, width, height = create_buffer_from_image("/!!Assets/Tiles/BattleTiles.png")
	-- certain position and with a certain size
	local first_uvs = textureset.geometries[1].uvs
	local x = first_uvs[1] - 48 --Tilesize * 3
	local y = first_uvs[2] + 64 --Tilesize * 4
	local texture_info = {
		type = resource.TEXTURE_TYPE_2D,
		width = width,
		height = height,
		x = x,
		y = y,
		format = resource.TEXTURE_FORMAT_RGBA,
		compression_type = resource.COMPRESSION_TYPE_DEFAULT,
		num_mip_maps = texture.mipmaps,
	}
	resource.set_texture(textureset.texture, texture_info, pixel_buffer)
end
1 Like

Cool! Happy to hear that you got it working!

Another option if you dont need the super dynamic solution of loading any image at runtime would be to use resource properties.

Thank you but the whole reason I did this in the first place was to make sure I only had one tilesource and set of animations.