Difference between revisions of "Levelgen Tutorial 02"

From Bitfighter
(textItem -> myTextItem)
Line 21: Line 21:
 
function main()
 
function main()
  
   local textItem = TextItem.new()
+
   local myTextItem = TextItem.new()
   textItem:setText("Hello, world!")
+
   myTextItem:setText("Hello, world!")
   levelgen:addItem(textItem)
+
   levelgen:addItem(myTextItem)
  
 
end
 
end
Line 39: Line 39:
  
 
<source lang="lua">
 
<source lang="lua">
   local textItem = TextItem.new()
+
   local myTextItem = TextItem.new()
 
</source>
 
</source>
Here we are creating a new TextItem and storing it into the variable <tt>textItem</tt> for later use. Remember that variables can refer to anything in Lua, whether it be a number, a string of text, a game object, or "nothing". Here, it's referring to our new TextItem, which is a game object.
+
Here we are creating a new TextItem and storing it into the variable <tt>myTextItem</tt> for later use. Remember that variables can refer to anything in Lua, whether it be a number, a string of text, a game object, or "nothing". Here, it's referring to our new TextItem, which is a game object.
  
 
Now we will set the text of this TextItem to say hello:
 
Now we will set the text of this TextItem to say hello:
  
 
<source lang="lua">
 
<source lang="lua">
   textItem:setText("Hello, world!")
+
   myTextItem:setText("Hello, world!")
 
</source>
 
</source>
  
This line means "set the text of <tt>textItem</tt> to <tt>Hello, world!</tt>".
+
This line means "set the text of <tt>myTextItem</tt> to <tt>Hello, world!</tt>".
  
The part before the colon (the <tt>:</tt> sign) is our variable name (<tt>textItem</tt>). The part after it is the function we're calling (<tt>setText</tt>).
+
The part before the colon (the <tt>:</tt> sign) is our variable name (<tt>myTextItem</tt>). The part after it is the function we're calling (<tt>setText</tt>).
  
 
Again, we are passing some text as the argument to a function. This time, it is <tt>setText</tt> instead of <tt>print</tt>, but as you can see there is not much of a difference in how we write it.
 
Again, we are passing some text as the argument to a function. This time, it is <tt>setText</tt> instead of <tt>print</tt>, but as you can see there is not much of a difference in how we write it.
Line 58: Line 58:
  
 
<source lang="lua">
 
<source lang="lua">
   levelgen:addItem(textItem)
+
   levelgen:addItem(myTextItem)
 
</source>
 
</source>
After creating our TextItem and setting it up as we'd like, we must add it to the game. To do this, we call <tt>levelgen:addItem</tt>. <tt>levelgen</tt> is a variable which is predefined by Bitfighter when it runs a levelgen script. This variable represents the currently running script, and provides functions for doing things to the game. <tt>levelgen:addItem</tt> takes one argument, the object to add to the game. In this case, it is our TextItem instace, `textItem`.
+
After creating our TextItem and setting it up as we'd like, we must add it to the game. To do this, we call <tt>levelgen:addItem</tt>. <tt>levelgen</tt> is a variable which is predefined by Bitfighter when it runs a levelgen script. This variable represents the currently running script, and provides functions for doing things to the game. <tt>levelgen:addItem</tt> takes one argument, the object to add to the game. In this case, it is our TextItem instace, `myTextItem`.
  
 
''Note:'' If you do not call <tt>addItem</tt> on objects you create with <tt>.new()</tt>, they will not appear in-game.
 
''Note:'' If you do not call <tt>addItem</tt> on objects you create with <tt>.new()</tt>, they will not appear in-game.

Revision as of 12:27, 9 August 2013


Introduction

Welcome to Part 02 of the Levelgen Tutorial. In this lesson, we will learn how to create new game objects, and add them to the level.

Prerequisites

You should have completed Levelgen Tutorial 01. Specifically, you should know:

  • How to create and use levelgen files in a level.
  • What the main() function is for.

Outcomes

When finished with this tutorial, you should know:

  • How to create a new TextItem and add it to the game.
  • How to set the text of a TextItem.

Code

Save the following code as a .levelgen file, and create an empty level which uses it.

function main()
 
  local myTextItem = TextItem.new()
  myTextItem:setText("Hello, world!")
  levelgen:addItem(myTextItem)
 
end

Now test the level. If everything went well you should see a text item that says "Hello World!" right in the center of your empty level. Behold, the magic of levelgens!

If it didn't work, check the Troubleshooting page of the wiki.

How It Works

function main()

As in every levelgen, we put the main chunk of code in the main function.

  local myTextItem = TextItem.new()

Here we are creating a new TextItem and storing it into the variable myTextItem for later use. Remember that variables can refer to anything in Lua, whether it be a number, a string of text, a game object, or "nothing". Here, it's referring to our new TextItem, which is a game object.

Now we will set the text of this TextItem to say hello:

  myTextItem:setText("Hello, world!")

This line means "set the text of myTextItem to Hello, world!".

The part before the colon (the : sign) is our variable name (myTextItem). The part after it is the function we're calling (setText).

Again, we are passing some text as the argument to a function. This time, it is setText instead of print, but as you can see there is not much of a difference in how we write it.

Next, we must actually add our TextItem to the game:

  levelgen:addItem(myTextItem)

After creating our TextItem and setting it up as we'd like, we must add it to the game. To do this, we call levelgen:addItem. levelgen is a variable which is predefined by Bitfighter when it runs a levelgen script. This variable represents the currently running script, and provides functions for doing things to the game. levelgen:addItem takes one argument, the object to add to the game. In this case, it is our TextItem instace, `myTextItem`.

Note: If you do not call addItem on objects you create with .new(), they will not appear in-game.

end

And again, we end our main function.

Conclusion

If you understand everything mentioned in the Outcomes section above, you are ready for Levelgen Tutorial 03.