How to set your enemy pcs to random.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
b_loved_observer77
Prole
Posts: 1
Joined: Sat Sep 09, 2017 8:16 pm

How to set your enemy pcs to random.

Post 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.
Atone
Prole
Posts: 6
Joined: Mon Aug 28, 2017 5:16 am

Re: How to set your enemy pcs to random.

Post 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.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: How to set your enemy pcs to random.

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 206 guests