Defold Crashing when changing material on a sprite

ERROR:SCRIPT: main/change_shader.script:11: go.set failed with error code -10
stack traceback:
  [C]:-1: in function set
  main/change_shader.script:11: in function <main/change_shader.script:8>

Assertion failed: i < Size(), file D:\a\defold\defold\tmp\dynamo_home\sdk\include\dmsdk/dlib/array.h, line 494
INFO:CRASH: Successfully wrote Crashdump to file: C:\Users\Mike\AppData\Roaming\Defold/_crash
ERROR:CRASH: CALL STACK:

ERROR:CRASH:  0 0x7FF7018E4030 dmCrash::GenerateCallstack D:\a\defold\defold\engine\crash\src\backtrace_win32.cpp:144
ERROR:CRASH:  1 0x7FF7019DBC4C raise minkernel\crts\ucrt\src\appcrt\misc\signal.cpp:547
ERROR:CRASH:  2 0x7FF7019BFBCC abort minkernel\crts\ucrt\src\appcrt\startup\abort.cpp:71
ERROR:CRASH:  3 0x7FF70196F5B8 common_assert_to_stderr<wchar_t> minkernel\crts\ucrt\src\appcrt\startup\assert.cpp:186
ERROR:CRASH:  4 0x7FF70196FC70 _wassert minkernel\crts\ucrt\src\appcrt\startup\assert.cpp:443
ERROR:CRASH:  5 0x7FF701611AF0 dmGameSystem::RenderBatch D:\a\defold\defold\engine\gamesys\src\gamesys\components\comp_sprite.cpp:1319
ERROR:CRASH:  6 0x7FF7016121A0 dmGameSystem::RenderListDispatch D:\a\defold\defold\engine\gamesys\src\gamesys\components\comp_sprite.cpp:1733
ERROR:CRASH:  7 0x7FF7016EA5C0 dmRender::DrawRenderList D:\a\defold\defold\engine\render\src\render\render.cpp:894
ERROR:CRASH:  8 0x7FF701708CB0 dmRender::ParseCommands D:\a\defold\defold\engine\render\src\render\render_command.cpp:183
ERROR:CRASH:  9 0x7FF7016F7090 dmRender::UpdateRenderScriptInstance D:\a\defold\defold\engine\render\src\render\render_script.cpp:3607
ERROR:CRASH: 10 0x7FF70152FB30 dmEngine::StepFrame D:\a\defold\defold\engine\engine\src\engine.cpp:1687
ERROR:CRASH: 11 0x7FF701530890 dmEngineUpdate D:\a\defold\defold\engine\engine\src\engine.cpp:2189
ERROR:CRASH: 12 0x7FF701530AC0 dmEngine::RunLoop D:\a\defold\defold\engine\engine\src\engine_loop.cpp:83
ERROR:CRASH: 13 0x7FF701528CA0 engine_main D:\a\defold\defold\engine\engine\src\engine_main.cpp:152
ERROR:CRASH: 14 0x7FF701951BA4 __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
ERROR:CRASH: 15 0x7FF80B422560 BaseThreadInitThunk <unknown>:0
ERROR:CRASH: 16 0x7FF80C2EAA20 RtlUserThreadStart <unknown>:0
ERROR:CRASH:

This is the code I’m using. And yes I know I’m created the same material twice. I’m just doing this as a test. 2nd material will be entered once I get this working.

function init(self)
	-- Store references to the materials
	self.material1 = hash("/assets/rainbow/rainbow.materialc")
	self.material2 = hash("/assets/rainbow/rainbow.materialc")
	--self.material2 = resource.load("/assets/materials/holographic/holographic.materialc")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("change_shader") then
		if message.shader == 1 then
			go.set("#sprite", "material", self.material1)
		elseif message.shader == 2 then
			go.set("#sprite", "material", self.material2)
		end
		-- ... handle more shaders as needed
	end
end

1 Like

Error code -10 means RESOURCE_NOT_FOUND. In this case, I’m guessing rainbow.materialc isn’t referenced anywhere so it’s not included in the build. The simplest way to fix that is to use go.property() with resource.material():

go.property("material1", resource.material("/assets/rainbow/rainbow.materialc"))

It should be throwing a proper error message instead of crashing the engine though :thinking:

4 Likes

Agree. @schlista could you please create a ticket on GH?

1 Like

done.

Thanks for the work around. Here’s a related question. Any idea why I have to put a c on the end of material when I’m hashing it?

I noticed as I worked with shaders the console will add a c to the material name.
image

image

See this works around the issue as you suggested. But I had to use go.property without the c on the end of material. If I add it it doesn’t run and makes me use .material before it will run. And if I delete the c on these it crashes defold.

self.material1 = hash("/assets/rainbow/rainbow.materialc")
self.material2 = hash("/assets/rainbow/rainbow.materialc")

This is the work around code:

function init(self)
	-- Store references to the materials
	go.property("material1", resource.material("/assets/rainbow/rainbow.material"))
	
	self.material1 = hash("/assets/rainbow/rainbow.materialc")
	self.material2 = hash("/assets/rainbow/rainbow.materialc")
	--self.material2 = resource.load("/assets/materials/holographic/holographic.materialc")
end

All files get a c at the end when compiling (short for “compiled” I guess?).

My bad, Defold adds a c automatically when it’s a a property of a component.

You can skip the hash part by setting it up like in the resource.material() doc:

go.property("my_material", resource.material("/material.material"))
function init(self)
  go.set("#sprite", "material", self.my_material)
end
1 Like

Correct, the compiled versions of files get a c at the end. And by compiled we mean going from text based protobuf format to binary and doing whatever kind of transformation that might be needed on the resource (Lua source to bytecode for instance, or uncompressed to compressed textures).

2 Likes