(SOLVED) Inconsistent behavior when generating path for require statement

So, I ran into a weird problem when playing around with the possibility of generating the path to modules I’m requiring in a table. I’ll just post the relevant code and walk through what’s going on:

require "scripts.utils"
local hashed = require "modules.hashed"
local actor_root = "assets.prefabs.actors."
--[[
emitters = require "assets.prefabs.actors.player.player_emitters",
sounds = require "assets.prefabs.actors.player.player_sounds",
animations = require "assets.prefabs.actors.player.player_animations"
--]]

local ACTOR_PARAMS = {
	[hash_with_reference("player")] = {
		root = true,
		emitters = true,
		sounds = true,
		animations = true
	},
	
	[hash_with_reference("doppelganger")] = {
		emitters = require "assets.prefabs.actors.doppelganger.doppelganger_emitters",
		sounds = require "assets.prefabs.actors.player.player_sounds",
		animations = require "assets.prefabs.actors.player.player_animations",
		ai = require "assets.prefabs.actors.doppelganger.doppelganger_ai",
		actions = require "assets.prefabs.actors.doppelganger.doppelganger_actions"
	}
}

for k,v in pairs(ACTOR_PARAMS) do
	for j,w in pairs(v) do
		if v.root and j ~= "root" and type(w) == "boolean" then
			local path = actor_root..hashed[k].."."..hashed[k].."_"..j
			print(path)
			v[j] = require(path)
			print("no error")
		end
	end
end
return ACTOR_PARAMS

The ACTOR_PARAMS module is used to organize a bunch of modules under the actor type they correspond to, and are then are appropriately referenced in the actor script’s init function. hash_with_reference is just a function that hashes the input string and stores the input string in the hashed table, indexed by the hash it produced, so I can “convert” from hash back to string. This all works fine if I set the requires manually, as I’ve kept in the “doppelganger” sub-table.

Now, I normally wouldn’t be too surprised to get this sort of error as I know defold is pretty picky about wanting to know required resources up front, but what confuses me is the error only gets thrown on player_emitters, and if I replace only emitters with the manual require statement I get no errors and everything works in-game. I was wondering if anyone with more experience could point out anything dumb I’m missing, or if this is just a bug (either that it works at all generating the path this way or that it doesn’t work in that particular case).

This is the filepath in my asset browser, so you can verify that it’s consistent with what is being generated and the commented-out manual require statement:

Thank you to anyone that may have some insight into this issue!

You’re manually requiring all the modules except player_emitter in the doppelganger sub-table, so Defold sees that and includes those modules in the build. Since player_emitter isn’t required until the game runs, it isn’t included, and you get that error. As long as a module is explicitly required somewhere, it’ll work everywhere.

3 Likes

Oh you’re right! I had a feeling it’d be something dumb like that. Forgot that I was reusing some player modules there. Guess my initial impulse on the limitations of require statements was correct. Thank you for clearing that up, I’ll mark this solved!

2 Likes