what are the rules about the naming of the main class in Haxe?

Viewed 393

On the main page at haxe.org - there is a Rock Scissors Paper game coded inside a class Game.

I have downloaded the HaxeDevelop editor and created a new project. When I went to the src/ folder of this project there was a file Main.hx that contained this skeleton

package;

import js.Lib;

/**
 * ...
 * @author Terrence Brannon
 */
class Main 
{

    static function main() 
    {

    }

}

and I'm wondering if I should change the class from Main to Game and where the rules for naming the entrypoint class for a Haxe project are documented.

2 Answers

Class names need to start with a Capital letter and be followed by letters a-z or A-Z ( underscore can also be used ).

You define your main class when you call haxe command from the terminal followed by parameter pairs such as the name of the main class, -main Main

https://haxe.org/manual/compiler-usage.html

You can put your class in a folder and add 'package folderName;' on the top line above your class and then you can call it with haxe.

haxe -main folderName.Main ...add other compiler property pairs.

So 'Game' is a valid name. One important detail is that Haxe program requires a starting static function this is always named 'main', this can be used to create the class.

class Game {
    static function main(){ new Game(); } // need a main static function.
    public function new(){
        // actual game class starts.
        trace( 'game started');
    }
}

Normally rather than write all the compiler parameters on the command line we put them it a file with a .hxml extension ie: compile.hxml

https://haxe.org/manual/compiler-usage-hxml.html

Some cross platform toolkits use json or xml definitions instead and sometimes tools to create these rather than run haxe compiler directly, this is because they do extra things like place all your assets in the output folder for you, and handles aspects like general settings for targetting c++ or js without having to have different hxml files.

For instance a popular cross platform graphics library for browser, desktop and mobile is openfl it describes it's build tools here:

https://lime.software/docs/command-line-tools/create-new-project/

Unfortunately all the cross target games toolkits use different build process but other haxe users are always happy to explain.

A good place to talk to Haxe users is on the haxe discord server, I think this link probably should work:

https://discord.gg/Agt3sJA

The main cross target - graphics rich ( suited to games ) toolkits for Haxe:

For doing traditional web development you can target js directly, but also Haxe provides some frameworks such as:

And obviously you can use Electron ( or Krom see Kha ) for nodejs apps. - https://github.com/tong/hxelectron

I am not really sure what specific PHP, Java, C#, Lua, Python libraries to mention, but general Haxe ones that seem to be popular and very useful for all targets:

Not everything, but a taster of useful links that come to mind.

For general tutorials on Haxe there are many places, but perhaps a good place to start is to work through the cookbook.

You can name your entry point class how you want.

  • If your file is called Main.hx you should run it with haxe -main Main.
  • If your file is called Game.hx you should run it with haxe -main Game.
  • If your file is called Test.hx you should run it with haxe -main Test.
  • .. et cetera

If you use FlashDevelop you have to set that file as entrypoint. Right-click in the project manager and choose "Document class". That'll give it a different icon too (greenish). entry point in HaxeDevelop

There is no standard naming convention, because it might depend on what your application does. I've seen Main, Application, Test but also fancier names.

https://haxe.org/manual/compiler-usage-hxml.html

Note that you cannot use a different name for the static entry point function inside the class.
This is always static function main() { }.

Related