Worm | Spawning fruit.

When the game starts an apple will be randomly placed on the field for the worm to collect.

  1. In the assets panel, in the 'main' folder, double click, 'game.collection'.
  2. In the outline panel, right click, 'Collection' and select, 'Add Game Object'.
  3. In the properties panel, change the 'Id' property to, 'food'.
  4. In the assets panel, right click, 'main' and select, 'New Script'.
  5. Name the script, 'food' and click, 'Create Script'.
  6. Update the on_message function to spawn a new piece of food.

function on_message(self, message_id, message, sender)
	-- Spawn food when message received
	if message_id == hash("spawn_food") then
		local valid = false
		-- Avoid spawning food on top of anything except grass
		while valid == false do
			local food = {x = math.random(3, 28), y = math.random(3, 28)}
			-- Spawn food on grass tile
			if tilemap.get_tile("/field#tilemap", "layer1",food.x, food.y) == 15 then
				valid = true
				tilemap.set_tile("/field#tilemap", "layer1", food.x, food.y, 45)
			end
		end
	end
end
What does this code do?

The food object will react when it receives a "spawn_food" message.

Using a while loop we can ensure the function only ends when a valid tile is found to place the food. We don't want it spawning on top of a wall or worm segment, only on grass.

food is a local table (its use here is similar to a record structure) that has x and y properties set to random numbers. 3 and 28 ensure there is a small margin and food can't spawn at the edge of the playing field. You could use 1 and 30 if you wanted to.

If the current tile at that location is grass (tile number 15 in the tile source) then the tile is changed to an apple tile and the loop is terminated.

Now we need to attach the script to the food game object.

  1. In the assets panel, in the 'main' folder, double click, 'game.collection'.
  2. In the outline panel, right click, 'food' and select, 'Add Component File'.
  3. Select, '/main/food.script'.

The food must be spawned when the worms are spawned.

  1. In the assets panel, in the 'main' folder, double click, 'main.script'.
  2. In the on_message function, underneath the line of code, '-- Insert code to spawn food here later' add this extra line:

msg.post("game:/food#food","spawn_food")
What does this code do?

This sends a "spawn_food" message to the game collection, food game object script.

Understanding the Url structure for message passing is critical to avoiding run time errors in Defold developments. The Url of the script can be copied from it's properties. The name of the collection is a prefix to that. The syntax is:

collection:/script_Url

There is no need to add the collection name if the object is in the same collection as the code sending the message.

  1. Save the changes by pressing CTRL-S or 'File', 'Save All'.
  2. Run the program by pressing F5 or choosing, 'Debug', 'Start / Attach' from the menu bar. You should be able to select '1up' and see the apple spawn.
    Run

The next stage is to add some collision detection for when the apple is eaten and spawn a new piece of food. Stage 5b >