Spotter | Writing the script to handle a difference being found.
- In the assets panel, right click the 'main' folder and select, 'New...', 'Script'.
- Name the script, 'target' and click, 'Create Script'.
- In the editor panel, change the code to handle the target colliding with a difference.
local tick = hash("tick")
local collision = hash("collision_response")
function on_message(self, message_id, message, sender)
-- if the newly placed target has hit another object...
if message_id == collision then
-- find out what the object is
local other_object_id = tostring(message.other_id)
-- only respond to a difference being found, not another target!
if string.find(other_object_id,"difference") ~= nil then
msg.post("#sprite", "play_animation", { id = tick })
-- disable the difference so it is only triggered once
msg.post(message.other_id, "disable")
end
end
end
Two local variable are used for pre-hashing although this really isn't essential here.
A message will be received by this script if the target object hits a difference object. This can happen when the target is spawned by the factory.
We need to know what the other object is that we have just collided with. It could be a difference, but it could also be another target since we aren't going to despawn the targets if the player makes an incorrect click.
The Id of the other object will be stored in a local variable, 'other_object_id'. This is a Url data type. We cast it to a string so that we can use string manipulation functions on it. This is achieved with the 'tostring' function.
'string.find' will return an integer of the index of where the first occurrence of one string is within another. This is known as an instring function. For eample, string.find("Hello World", "lo") would return 3. "lo" can be found at the third index. Strings are zero indexed.
We set the Id of the difference game object in level1.collection to be "difference1". It will also have the Url "difference1" as a result. If we look at the Url of the object sending the message and don't find the word "difference" in the Url we can't have collided with a difference, it must have been a previously incorrect input (a cross) instead. In this case string.find will return 'nil'.
However, if the word, "difference" is found, the result will be an integer, so we can use the condition, "if ... does not equal nil". to determine that. Not equal to is ~= in Lua.
If a difference has been found we post a message to the sprite to tell it to play an animation. The name of the animation to play is passed as a value in an array as the third parameter: {id = tick}. It's not actually an animation, it is just a static image of a tick, but that doesn't matter. It could be an animation!
Finally we need to disable the collision detection on the difference object. Otherwise it will detect a new collision in every frame, and you'd also be able to cheat by selecting the same difference over and over again. The best way to do this is to completely disable the whole object. This would remove it from the screen, but since a difference does not have a sprite anyway, just a collision object, it works just fine.
Now we need to attach this script to the target game object.
- In the assets panel, in the 'main' folder, double click, 'target.go'.
- In the outline panel, right click, 'Game Object' and select, 'Add Component File'.
- Select, '/main/target.script'.
- Save the changes by pressing CTRL-S or 'File', 'Save All'.
That's the target complete, but we need to code the player actually clicking somewhere on the screen to spawn the target. Stage 4c >