[SOLVED] How to get table in the order it’s written in

For tower defense game I wanna make a module script with waves and mobs inside them.

But Idk how to save order and put same mobs in one wave but they should spawn one in the beginning and the second ones in the end of the wave? (Like Dog in e.g.)

E.g.:

Waves = {
		[1] = {
			Car = 3	
		},
		
		[2] = {
			Dog = 2,
			Car = 3,
			Dog = 1
		},
	}

I know that I can do like so, but table becomes so mess:

Waves = {
		[1] = {
			[1] = {Car = 2},
		},
		
		[2] = {
			[1] = {Dog = 2},
			[2] = {Car = 3},
			[3] = {Dog = 1}
		},
	}

Mb there’s a better solution?

If you just don’t feel like typing out all those numbers, then you can also just do it like this. This is the same:

Waves = {
	[1] = {
		[1] = {Car = 1},
	},

	-- ...
}

Waves = {
	{
		{Car = 1},
	},

	{
		{Dog = 2},
		{Car = 3},
		{Dog = 1}
	},
}

But you could also just use the third parameter of for, which allows you to add a different amount to i than 1. (In this case 2)

Waves = {
	{
		"Car", 1,
	},

	{
		"Dog", 2,
		"Car", 3,
		"Dog", 1
	},
}

for i=1,#Waves,2 do
	local enemy_type = Waves[i]
	local enemy_number = Waves[i+1]

	-- ...
end

Best regards,
Pinker

1 Like