How to set your enemy pcs to random.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 1
- Joined: Sat Sep 09, 2017 8:16 pm
How to set your enemy pcs to random.
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.
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.
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: How to set your enemy pcs to random.
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
Who is online
Users browsing this forum: Google [Bot] and 4 guests