# HelloWorld.cog # # Print "Hello World" on the screen when the level starts. # # [SM] #==============================================# symbols message startup end #==============================================# code #----------------------------------- startup: Sleep(1); Print("Hello World"); // Print the string return; #----------------------------------- endAll this cog does is print "Hello World" to the screen when a level is loaded. The first command in the code section is
Sleep(1)
. This tells the cog to wait for 1 second before doing anything. The next command is
Print("Hello World")
. This prints the text to screen. The next command is return
. This command tells JK
to stop reading the cog.
Comments are added to a cog to either improve its structure or to explain what a command is doing. Comments can visually improve a cog's structure so that it's easier to see where one part of a cog ends and another begins - this is done in the cog above with pound signs and dashes.
Other comments explain something that isn't obvious when read. Although the print command is very easy to understand, the comment beside it is only there as an example.
symbols
and ends with the keyword end
.
In the symbols section, every variable that the cog uses will be defined and optionally given an initial
value. Don't worry about variables yet, we'll cover them in much more detail later on.
You're given a lot of freedom when it comes to whitespace (spaces, tabs, and line breaks) in
the code section. With the symbols section, each variable declaration must be on its own line.
But JK allows you to write everything between code
and end
on one line if you want. But you should
always try to make your code as readable as possible. This means lots of comments and proper
indentation.
It's easy to think that you'll never need comments to understand what you've written. But that's far from the truth. It doesn't matter how good your memory is, once you have to come back to a cog you've written a few weeks before, you'll find that you have no memory of most of the cog. It's much better to leave comments for yourself, than to waste time later by trying to figure out what you've already done.