Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Can't seem to use "require" with Apollo

A topic by Carson K. created Apr 15, 2021 Views: 347 Replies: 2
Viewing posts 1 to 2
(1 edit)

I have a tiles.lua:

local tiles = {}
function tiles.Create()
    tileTest = Tile.Add("Test Tile", "tiles/test_tile.png", "test_tile")
    World.AddTile(tileTest, 0, 0)
    World.AddTile(tileTest, 16, 0)
    World.AddTile(tileTest, 32, 0)
end
return tiles

and a main.lua:

require "src.tiles"
function init()
    tiles.Create()
end

My game runs the init function of main.lua on game start, and the function tiles.Create() in tiles.lua works perfectly fine when placed inside function init() in main.lua.

(It should be noted that tiles.lua is located in src/tiles.lua in relation to main.lua. I've also tried `require "tiles"` with tiles.lua in the same directory as main.lua but that doesn't work either)

EDIT: Turns out require does not want the path relative to your current path, but from the root datafiles path. Changing this to

local tiles = require "mods.base.src.tiles"

when the folder structure is

datafiles
|mods
||base
|||src
||||tiles.lua
|||main.lua

works perfectly fine. Leaving this post up in case others encounter a similar issue :)

Developer(+1)

You can adjust this behaviour in apollo_core.gmllua_add_file - change

lua_set_cwd(dir);

to

lua_set_cwd(filename_dir(fp));

or call lua_set_cwd yourself and set the optional chdir argument to false when calling lua_add_file.

This is very useful info! Thanks for the continued exceptional customer support :)