ZScript Hello World

From ZDoom Wiki
Jump to navigation Jump to search

This is a bare-bones bit of ZScript that logs "Hello World!" into the console, using Event Handlers. Note that due to the nature of ZScript, this only shows a particular set of features in ZScript.

First, we need to set the ZScript version so we can use all available features. (If we do not do this, GZDoom will assume for compatibility reasons that the target version is 2.3.0 and refuse to compile anything with features from later versions.)

As of the creation of this page, the latest release of GZDoom is 4.5.0, so we will insert the following:

 version "4.5.0"

Ideally "Hello World!" should be printed when the player first enters the map. This would be governed by an Event Handler, in particular WorldLoaded, which is executed when a map has finished loading. Thus, we will create an Event Handler called HelloWorldHandler, and override its WorldLoaded method.

 version "4.5.0"
 
 class HelloWorldHandler : EventHandler
 {
     override void WorldLoaded (WorldEvent e)
     {
     }
 }

Now, we just need to make it print "Hello World!" when the world gets loaded. We can use console.printf to print to the console:

 // Put this in a ZSCRIPT lump
 version "4.5.0"
 
 class HelloWorldHandler : EventHandler
 {
     override void WorldLoaded (WorldEvent e)
     {
         console.printf("Hello World!");
     }
 }

This code should print "Hello World!" when the player enters a map. This doesn't quite happen yet though, there is one caveat left. GZDoom does not load Event Handlers automatically, so we have to use the gameinfo section of MAPINFO to specify this event handler should run.

 // Put this in a MAPINFO lump
 gameinfo
 {
     AddEventHandlers = "HelloWorldHandler"
 }