[Done] Help with table select

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Done] Help with table select

Post by TheSAguy »

Hi,

I'm trying to select a unit to be spawned by my unit launcher, based on a weight.
The unit can have a different name, depending on the evolution factor. (1 through 6 in the code below)

My code below is erroring, cause I'm trying to concatenate a "global" with a number,
Image

How should I update my code to fix this error?

Code: Select all

function get_unit_to_spawn()

	local spawn_options = --First part of possible unit name
		{
		  {spawn="ne-biter-breeder-" , weight=10},
		  {spawn="ne-biter-fire-" , weight=20},
		  {spawn="ne-biter-fast-" , weight=50},
		  {spawn="ne-biter-wallbreaker-" , weight=40},
		  {spawn="ne-biter-tank-" , weight=10},
		  
		  {spawn="ne-spitter-breeder-" , weight=5},
		  {spawn="ne-spitter-fire-" , weight=3},
		  {spawn="ne-spitter-ulaunch-" , weight=2},
		  {spawn="ne-spitter-webshooter-" , weight=8},
		  {spawn="ne-spitter-mine-" , weight=8}
		}
					  
	local calculate_odds = {}
	for k,spawn in ipairs(spawn_options) do
		for i=1, spawn.weight do
			calculate_odds[#calculate_odds+1] = k
		end
	end

	local random_num = #calculate_odds
	return spawn_options[calculate_odds[math.random(random_num)]]

end



subEnemyNameTable["ne_green_splash_2"] = {}
subEnemyNameTable["ne_green_splash_2"][0] = 			get_unit_to_spawn.."1" -- Evolution Factor 0
subEnemyNameTable["ne_green_splash_2"][1] = 			get_unit_to_spawn.."2"
subEnemyNameTable["ne_green_splash_2"][2] = 			get_unit_to_spawn.."2"
subEnemyNameTable["ne_green_splash_2"][3] = 			get_unit_to_spawn.."3"
subEnemyNameTable["ne_green_splash_2"][4] = 			get_unit_to_spawn.."3"
subEnemyNameTable["ne_green_splash_2"][5] = 			get_unit_to_spawn.."4"
subEnemyNameTable["ne_green_splash_2"][6] = 			get_unit_to_spawn.."4"
subEnemyNameTable["ne_green_splash_2"][7] = 			get_unit_to_spawn.."5"
subEnemyNameTable["ne_green_splash_2"][8] = 			get_unit_to_spawn.."5"
subEnemyNameTable["ne_green_splash_2"][9] = 			get_unit_to_spawn.."6"
subEnemyNameTable["ne_green_splash_2"][10] =			get_unit_to_spawn.."6" -- Evolution Factor 100

Last edited by TheSAguy on Sun Aug 05, 2018 12:41 am, edited 1 time in total.
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Help with table select

Post by Bilka »

TheSAguy wrote:get_unit_to_spawn.."3"
Call the function....

Code: Select all

get_unit_to_spawn() .. "3"
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Help with table select

Post by TheSAguy »

Okay, I updated the code, added the missing "()":

Code: Select all

subEnemyNameTable["ne_green_splash_2"] = {}
subEnemyNameTable["ne_green_splash_2"][0] = 			get_unit_to_spawn().."1"
subEnemyNameTable["ne_green_splash_2"][1] = 			get_unit_to_spawn().."2"
subEnemyNameTable["ne_green_splash_2"][2] = 			get_unit_to_spawn().."2"
subEnemyNameTable["ne_green_splash_2"][3] = 			get_unit_to_spawn().."3"
subEnemyNameTable["ne_green_splash_2"][4] = 			get_unit_to_spawn().."3"
subEnemyNameTable["ne_green_splash_2"][5] = 			get_unit_to_spawn().."4"
subEnemyNameTable["ne_green_splash_2"][6] = 			get_unit_to_spawn().."4"
subEnemyNameTable["ne_green_splash_2"][7] = 			get_unit_to_spawn().."5"
subEnemyNameTable["ne_green_splash_2"][8] = 			get_unit_to_spawn().."5"
subEnemyNameTable["ne_green_splash_2"][9] = 			get_unit_to_spawn().."6"
subEnemyNameTable["ne_green_splash_2"][10] =			get_unit_to_spawn().."6"
But now I'm getting this error:
Image

Line 55 is:
"return spawn_options[calculate_odds[math.random(random_num)]]"
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Help with table select

Post by Bilka »

You are calling math.random() outside of an event, like the error says. Don't do that.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Help with table select

Post by DaveMcW »

Put your table creation code inside on_init().
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] Help with table select

Post by TheSAguy »

Got it working.
Thanks guys!
Post Reply

Return to “Modding help”