You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
In the previous three tutorials we went over a lot of code that didn't have anything to do directly with the game. All of that code can be used independently of the game you're making which is why I call it the engine code in my head, even though I guess it's not really an engine. As we make more progress in the game I'll constantly be adding more and more code that falls into that category and that can be used across multiple games. If you take anything out of these tutorials that code should definitely be it and it has been extremely useful to me over time.
Before moving on to the next part where we'll start with the game itself you need to be comfortable with some of the concepts taught in the previous tutorials, so here are some more exercises.
Exercises
52. Create a getGameObjects function inside the Area class that works as follows:
-- Get all game objects of the Enemy classall_enemies=area:getGameObjects(function(e)
ife:is(Enemy) thenreturntrueendend)
-- Get all game objects with over 50 HPhealthy_objects=area:getGameObjects(function(e)
ife.hpande.hp>=50thenreturntrueendend)
It receives a function that receives a game object and performs some test on it. If the result of the test is true then the game object will be added to the table that is returned once getGameObjects is fully run.
54. Create a function named printAll that receives an unknown number of arguments and prints them all to the console. printAll(1, 2, 3) will print 1, 2 and 3 to the console and printAll(1, 2, 3, 4, 5, 6, 7, 8, 9) will print from 1 to 9 to the console, for instance. The number of arguments passed in is unknown and may vary.
55. Similarly to the previous exercise, create a function named printText that receives an unknown number of strings, concatenates them all into a single string and then prints that single string to the console.
56. How can you trigger a garbage collection cycle?
57. How can you show how much memory is currently being used up by your Lua program?
58. How can you trigger an error that halts the execution of the program and prints out a custom error message?
59. Create a class named Rectangle that draws a rectangle with some width and height at the position it was created. Create 10 instances of this class at random positions of the screen and with random widths and heights. When the d key is pressed a random instance should be deleted from the environment. When the number of instances left reaches 0, another 10 new instances should be created at random positions of the screen and with random widths and heights.
60. Create a class named Circle that draws a circle with some radius at the position it was created. Create 10 instances of this class at random positions of the screen with random radius, and also with an interval of 0.25 seconds between the creation of each instance. After all instances are created (so after 2.5 seconds) start deleting once random instance every [0.5, 1] second (a random number between 0.5 and 1). After all instances are deleted, repeat the entire process of recreation of the 10 instances and their eventual deletion. This process should repeat forever.
61. Create a queryCircleArea function inside the Area class that works as follows:
-- Get all objects of class 'Enemy' and 'Projectile' in a circle of 50 radius around point 100, 100objects=area:queryCircleArea(100, 100, 50, {'Enemy', 'Projectile'})
It receives an x, y position, a radius and a list of strings containing names of target classes. Then it returns all objects belonging to those classes inside the circle of radius radius centered in position x, y.
62. Create a getClosestGameObject function inside the Area class that works follows:
-- Get the closest object of class 'Enemy' in a circle of 50 radius around point 100, 100closest_object=area:getClosestObject(100, 100, 50, {'Enemy'})
It receives the same arguments as the queryCircleArea function but returns only one object (the closest one) instead.
63. How would you check if a method exists on an object before calling it? And how would you check if an attribute exists before using its value?
64. Using only one for loop, how can you write the contents of one table to another?
The text was updated successfully, but these errors were encountered:
I'm not really understanding the first question...do you just return a table with that one game object after the function is executed if it returns true? Because this is what I got from that...
function Area:getGameObject(func, e)
if func(e) then
return {e}
end
end
I'm not really understanding the first question...do you just return a table with that one game object after the function is executed if it returns true? Because this is what I got from that...
function Area:getGameObject(func, e)
if func(e) then
return {e}
end
end
No, the getGameObjects function will receive only the callback, apply it to all the gameObjects inside the Area and return only the ones that pass the filter
In the previous three tutorials we went over a lot of code that didn't have anything to do directly with the game. All of that code can be used independently of the game you're making which is why I call it the
engine
code in my head, even though I guess it's not really an engine. As we make more progress in the game I'll constantly be adding more and more code that falls into that category and that can be used across multiple games. If you take anything out of these tutorials that code should definitely be it and it has been extremely useful to me over time.Before moving on to the next part where we'll start with the game itself you need to be comfortable with some of the concepts taught in the previous tutorials, so here are some more exercises.
Exercises
52. Create a
getGameObjects
function inside theArea
class that works as follows:It receives a function that receives a game object and performs some test on it. If the result of the test is true then the game object will be added to the table that is returned once
getGameObjects
is fully run.53. What is the value in
a
,b
,c
,d
,e
,f
andg
?54. Create a function named
printAll
that receives an unknown number of arguments and prints them all to the console.printAll(1, 2, 3)
will print 1, 2 and 3 to the console andprintAll(1, 2, 3, 4, 5, 6, 7, 8, 9)
will print from 1 to 9 to the console, for instance. The number of arguments passed in is unknown and may vary.55. Similarly to the previous exercise, create a function named
printText
that receives an unknown number of strings, concatenates them all into a single string and then prints that single string to the console.56. How can you trigger a garbage collection cycle?
57. How can you show how much memory is currently being used up by your Lua program?
58. How can you trigger an error that halts the execution of the program and prints out a custom error message?
59. Create a class named
Rectangle
that draws a rectangle with some width and height at the position it was created. Create 10 instances of this class at random positions of the screen and with random widths and heights. When thed
key is pressed a random instance should be deleted from the environment. When the number of instances left reaches 0, another 10 new instances should be created at random positions of the screen and with random widths and heights.60. Create a class named
Circle
that draws a circle with some radius at the position it was created. Create 10 instances of this class at random positions of the screen with random radius, and also with an interval of 0.25 seconds between the creation of each instance. After all instances are created (so after 2.5 seconds) start deleting once random instance every [0.5, 1] second (a random number between 0.5 and 1). After all instances are deleted, repeat the entire process of recreation of the 10 instances and their eventual deletion. This process should repeat forever.61. Create a
queryCircleArea
function inside theArea
class that works as follows:It receives an
x
,y
position, aradius
and a list of strings containing names of target classes. Then it returns all objects belonging to those classes inside the circle of radiusradius
centered in positionx, y
.62. Create a
getClosestGameObject
function inside theArea
class that works follows:It receives the same arguments as the
queryCircleArea
function but returns only one object (the closest one) instead.63. How would you check if a method exists on an object before calling it? And how would you check if an attribute exists before using its value?
64. Using only one
for
loop, how can you write the contents of one table to another?The text was updated successfully, but these errors were encountered: