Namespaces and files
Files
A game is every .tide file in its folder and its subfolders, but for hidden folders (a name starting with a dot, like tide's own .tide/ and .build/), and those of the packages it uses. Every declaration is visible from every file, and there are no imports between files. How you split a game into files is up to you: the editors can move a declaration to a file of its own. A subfolder with a tide.packages of its own is a game or a package of its own, and isn't part of the game.
Files compile in order of their paths, and that's the default order of systems and views: by file, then as they're written in each file (see Systems). Packages' files come first.
Namespaces
Large games keep names apart with namespaces. namespace at the top of a file puts everything in it in that namespace:
// combat.tide
namespace Combat;
component Health { int value = 100; }Code outside the namespace names its declarations with it, Combat.Health, or imports the namespace with using:
// main.tide
using Combat;
scene Main { }
event(Spawned) Setup(with Main)
{
Spawn(Health, Items.Health { value = 3 });
}- A file has at most one
namespace Name;line, and files without one are in the global namespace. Namespaces can be dotted:namespace Game.Combat;. namespace Name;and namespace imports (using Combat;) come before any declaration.- A plain name is looked up in the namespace of the code that writes it, then the namespaces around it out to the global namespace, then the
usingones. So a name your game declares itself always means yours, even when a namespace you import has one too: write the namespace,Physics.Body, for the imported one. If twousingnamespaces both have a name that nothing of yours has, it's ambiguous: write the namespace. - Inside a namespace its own names win wherever they're used the same way: a package's enum
Sideis whatSide(i)means in the package's code, even if your game has a function calledSide. In a package's code, your game's global names come after the namespaces the package imports, so nothing your game declares can break a package, or hide another package's names from it. In your game's global code, a name is looked for as what it's used as: a call finds a function in ausingnamespace past a command handler of the same name. - Qualified names work wherever a type is named: parameters (
mut Combat.Health health),withandwithout, component values,Spawn,AddandRemove, and[Before]and[After]([After(Physics.Gravity)]). They work for constants too:Combat.CRIT_MULTIPLIER. - The same name can be declared in different namespaces.
- An override names what it replaces the same way:
override system Combat.Move(...), orMovealone afterusing Combat;. - There's exactly one
Mainin a game, in any file or namespace.
Several namespaces in one file
A namespace can also be a block, which puts only what's between its braces in it. A file can have several, so small things that belong together can stay in one file and still keep their names apart:
namespace Combat
{
component Health { int value = 100; }
namespace Rules
{
int Cap(int value) { return Math.Min(value, 100); }
}
system Heal(mut Health health)
{
health.value = Combat.Rules.Cap(health.value + 1);
}
}
namespace Items
{
component Bag { int coins; }
}
scene Main { }
event(Spawned) Setup(with Main)
{
Spawn(Combat.Health, Items.Bag);
}- A block goes wherever a declaration does, and one can be inside another:
Rulesabove isCombat.Rules. A block's name can be dotted,namespace Game.Items { }. - A block is inside the namespace it's written in. In a file that starts with
namespace Game;, the blocknamespace Combat { }isGame.Combat. - The same namespace can be a block more than once, in one file or in several, and a file's own namespace somewhere else: it's one namespace, and each sees what the others declare by its plain name.
- Code in a block finds plain names in its block's namespace first, then the ones around it, then the file's imported namespaces. Namespace imports (
using Combat;) stay at the top of the file, and are for every block in it. - A name with a namespace is written whole, from the outermost one:
Combat.Rules.Cap, insideCombattoo. - A block changes no order: its systems run where they're written in the file, like any others.
- Attributes,
local,virtualandoverridego before the declarations inside a block, not before the block.
Type aliases
using Name = Type; gives a closed type another name. Declare it alongside other namespace declarations, at the top level of a file or inside a namespace block.
A file's namespace Game; header and namespace imports go before its aliases, as they go before other declarations. Namespace blocks can follow aliases and other declarations.
namespace Game
{
using Point = float3;
using Points = List<Point>;
using Matrix = float4x4;
using Health = Combat.Health;
struct Path
{
Points points;
}
}Point is exactly float3, and Points is exactly List<float3>. The alias keeps the underlying type's conversions, members, defaults and restrictions. An alias for a component still names that same component; it doesn't give an entity a second component type. Functions whose parameter types differ only by an alias have the same signature.
Aliases belong to their namespace, like structs: another file can write Game.Point, or using Game; and then Point. A top-level alias in a file with namespace Game; also belongs to Game; without a namespace it is global. The target's names are resolved where the alias is declared.
The target can be a primitive, a built-in vector or matrix, a struct, an enum, a component or a closed container type, including qualified names and other aliases. Alias chains are allowed; a cycle is an error. Supply every type argument: using Points = List<float3>; is valid, while generic alias declarations such as using Points<T> = List<T>; aren't supported.
The = distinguishes a type alias from a namespace import: using Combat; imports names for that file, while using Health = Combat.Health; declares one alias in its namespace.
Code for other versions of tide
Tide still changes between versions. Code that has to build with several, like a package's, picks what each version reads with #if:
#if TIDE_0_4_OR_NEWER
const int SLOTS = 8;
#elif TIDE_0_3_OR_NEWER
const int SLOTS = 4;
#else
const int SLOTS = 1;
#endifTIDE_0_4_OR_NEWERis true in tide 0.4 and every version after it, andTIDE_0_4_2_OR_NEWERin 0.4.2 and after. A nightly version counts as the version it comes before: 0.4.0-nightly.3 is 0.4.- Conditions take
!,&&,||,==,!=, parentheses,trueandfalse. A symbol that starts withTIDE_and isn't one of tide's versions is an error, so a misspelled one doesn't quietly leave code out. #if,#elif,#elseand#endifeach go on a line of their own, which a//comment can end, and an#ifcan hold more of them.- The lines a directive leaves out aren't read as code, only looked through for the directives that end them. They can hold code a version of tide you don't have reads, with syntax it doesn't know yet.
Editors gray out the lines left out, and the formatter leaves them, and the directives, as they are.
Symbols of your own
An #if also reads symbols that you, the build or the game's packages define. To #if, a symbol is defined or it isn't, and one that nothing defines is false. A build can give one a value too, which code reads as a constant: see Values from the build.
#define TRACE_HITS
#if TRACE_HITS && DEBUG
event(Hit hit) Trace(Health health)
{
Debug.Log($"hit for {hit.damage}, {health.current} left");
}
#endif#define NAMEdefines a symbol for the file it's in, and#undef NAMEtakes one away there, the build's too. Both go at the top of the file, before its first declaration, as they're for all of it. Other files don't see them.--define NAMEontide run,tide build,tide scheduleandtide shadersdefines a symbol for every file of the game and its packages, as many times as you have symbols:tide build --define DEMO --define STEAM.--define NAME=valuedefines it with a value, and#if NAMEis true either way.DEBUGis defined in default debug builds, rather than--releaseor--distributionbuilds.PACKAGE_<NAME>is defined for each package the game builds with, its name in capitals:PACKAGE_BOX2Dwhen the game'stide.packageslists Box2D. So a package, or a game, can have code that's only there beside another package:
#if PACKAGE_BOX2D
using Box2D;
system Splash(Body body, with Raindrop) { ... }
#endif- Names that start with
TIDE_andPACKAGE_are tide's: neither#definenor--definetakes one. - There are no symbols for platforms: a game is the same code on desktop, phones and the web, so their players can share a match.
Values from the build
Code reads the same symbols as constants. defined(NAME) is true where #if NAME would be, and define(NAME, default) is the value --define NAME=value gave, or default when the build gives none:
const bool PREPASS = !defined(NO_PREPASS);
const int ARMY_SIDE = define(ARMY_SIDE, 500);
const float SCALE = define(SCALE, 1);
const string BANNER = define(BANNER, "Mount up");
const Difficulty START = define(START, Difficulty.Normal);tide run --define ARMY_SIDE=50 --define SCALE=0.5 --define "BANNER=Night battle" --define START=Hard builds with those; a plain tide run with the defaults. Where #if picks between pieces of code, these pick between values, so a choice between three army sizes is one line instead of an #if, an #elif and an #else.
- The value reads as the constant's type: the type of what it goes into when the default goes there too, so
define(SCALE, 1)in afloatreads--define SCALE=0.5as a float, anddefine(MANY, 5)in alongreads a long. That's aconst, a field, a local, a parameter, areturn, or a part of a vector, a color or a quaternion:float3(define(X, 0), 0, 0). Anywhere else, in arithmetic say, it's the default's type, so write the default as the kind of number you want:define(SCALE, 1.0) * 2. A default that doesn't go where it goes,const int SIDE = define(SIDE, 1.5), is the error it would be without thedefine, whatever the build gives. - The values: a
bool(trueorfalse), abyte, anintor along(whole, in digits, a-before a negative one, and fitting the type), afloator adouble(a number in decimal, like2.5or-1e3, that the type holds, not so small it would be 0), astring(the text as it is, which has to be UTF-8), or an enum (a member's name, as written). A value that isn't one is an error that says what's wanted:--define ARMY_SIDE=lotsis "the build gives 'ARMY_SIDE' the value 'lots', and 'define(ARMY_SIDE, ...)' reads an int here". - A symbol defined with no value (
--define FAST,DEBUG, or a file's#define FAST) istrueto abooldefine, and an error to any other, which says what to do: give it a value on the command line, or take away the file's#define, which is over the build's.--define DEBUG=3keeps its value in a debug build too. Tide's versions and the packages'PACKAGE_symbols never have a value, sodefinedoesn't read them:defined(PACKAGE_BOX2D)does. - They're constants, so they go wherever a constant does: a
const, a field's default, an array's size, aswitchcase, a GPU function. The default has to be one too. #ifreads whether a symbol is defined, never its value:#if ARMY_SIDE == 50,#if ARMY_SIDE > 50and, with--define START=Hard,#if START == Hardare errors, and so is#if defined(FAST), which is code's spelling (#if FASTis the condition's). Compare the constant in code,if (ARMY_SIDE == 50), or define a symbol for each choice.definedanddefineare names of the language's: no type, function or constant is called either, and an editor won't rename one to them. A method, a parameter or a localFuncwith one of the names is called where it's in scope, as one calledSpawnwould be.- On Windows,
tidereads its command line in the console's code page, so a text value with characters outside ASCII is refused as not UTF-8. Keep such text in the game and choose it with an enum's member:define(BANNER, Banner.Day)and--define BANNER=Night.
Builds that differ are different games
Every machine of a match has to run the same code. So when a game's #if lines, or its defined and define, read DEBUG, a --define symbol or a package's, the answers, with each symbol's value, are part of what the game is: a debug build and a release build of such a game, or two builds with different --defines, can't join each other's matches. A game that reads none of them, or only tide's versions and its files' own #defines, isn't affected.
Editors read DEBUG as defined, and know the game's packages. They don't know what a command line will define, so code under #if DEMO is grayed out there, and define(ARMY_SIDE, 500) is 500.