Page 1 of 1
How to set your enemy pcs to random.
Posted: Sat Sep 09, 2017 11:01 pm
by b_loved_observer77
I am unsure on how to setup a game scenarios in which my enemy npcs will show up randomly. By this I mean I want one of my three enemy npcs to be generated rather than just one enemy type to be displayed on the screen.
Re: How to set your enemy pcs to random.
Posted: Sun Sep 10, 2017 12:57 am
by Atone
Check out love.math.random on the wiki. You can use that to get random numbers which you can then use to determine which enemy to spawn. For example, if you had 3 types of enemies, you'd get a random number between 1 and 3 and then spawn in the corresponding npc.
Re: How to set your enemy pcs to random.
Posted: Sun Sep 10, 2017 2:44 pm
by Sir_Silver
Well, I have to say, I am unsure how your game's code works, so telling you how to make it work in a specific way isn't really going to be possible unless you want to share some code and or be more specific about what you need. However, here is an example solution of a way to generate random enemies:
Code: Select all
math.randomseed(os.time())
local enemies = {
{name = "gargoyle", damage = 5},
{name = "rat", damage = 1},
{name = "skeleton", damage = 2}
}
for i = 1, #enemies do
local enemy = enemies[i]
enemy.__index = enemy
end
local function create_random_enemy()
return setmetatable({}, enemies[math.random(#enemies)])
end
local my_random_enemy = create_random_enemy()
print(my_random_enemy.name, my_random_enemy.damage) --skeleton 2