Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

Using Multiple Files

A topic by Hedges created 57 days ago Views: 23 Replies: 2
Viewing posts 1 to 3
(1 edit)

My main.js file is getting pretty big, is there a way to split this into multiple files? I tried exporting and importing modules but this caused an "Uncaught ReferenceError: a is not defined" error.

Update: Error was fixed by adding const to the forloop on line 954. See post below for more details.

Developer

Probably the easiest stuff to break out are the UI functions from the end of the file. Let me have a think about how to split it up.

(1 edit)

UPDATE :

I was able to fix the "Uncaught ReferenceError: a is not defined" message by adding 'const' to the forloop on line 954. So it's now, for (const a in attrs). type="module" can now be added to the main.js <script> tag and static imports work. Using dynamic imports is no longer needed. I'll add a new pastebin example.

https://pastebin.com/fCCzG1p5

Original Post:

I finally found a working solution using dynamic imports. For some reason adding type="module" to <script src="main.js"... breaks at line 954. Using dynamic imports skips the need for type="module" and allows imported modules to be used. All you need to do is all 'async' to the IIFE in main.js then dynamically import the modules. I have added a simple pastebin example moving 'gametitle' to another file. 

https://pastebin.com/4Y0P3VsW

I don't know the limitations of this solution, but it has been working so far. I was able to create a sound.js file and move the sfx constant into it.