Share <const> between files?

Can you export/import a <const>?

COLLISION_GROUPS <const> = {
    Neave = 0x1
}

I thought I could use a syntax like this in the file I'm importing (loosing the <const> works fine).

Is there a syntax for this?

I've achieved it like so:

local COLLISION_GROUPS <const> = {
    Neave = 0x1
}

function getCollisionGroups()
    return COLLISION_GROUPS
end

Is this the best way?

I’ve been adding a reference to the const as a local variable in the files I need it, as in

globals.lua:

VARIABLE <const> = …

And then at the top of my scripts:

local variable <const> = VARIABLE

@Gamma do you mean like this?

local MY_GLOBAL_CONSTANT <const> = "My global constant"

MY_GLOBAL_CONSTANT <const> = MY_GLOBAL_CONSTANT

This throws a compile exception too.

MY_GLOBAL_CONSTANT <const> = "My global constant"

-- And then make it local inside other scripts
local MY_LOCAL_CONSTANT <const> = MY_GLOBAL_CONSTANT

That looks like what I did. :thinking:

@Gamma

I just tried compiling a file with MY_GLOBAL_CONSTANT <const> = "My global constant" and get globals.lua:1: syntax error near '<'.

Oops, you're correct. What I do is declare it in my globals file:

COLLISION_GROUPS = {
    Neave = 0x1
}

And then in any files where I need to use COLLISION_GROUPS I store a local copy at the start of the file:

local COLLISION_GROUPS <const> = COLLISION_GROUPS
1 Like

That's a syntax error because <const> is only valid for locals. It'd be nice if the parser made that clearer.

2 Likes