[Solved] I'm blind and can't see my lua bug

Place to get help with not working mods / modding interface.
Post Reply
Kingdud
Long Handed Inserter
Long Handed Inserter
Posts: 76
Joined: Sat Dec 16, 2017 3:23 am
Contact:

[Solved] I'm blind and can't see my lua bug

Post by Kingdud »

I get an error on the line

Code: Select all

		["normal"] = 1 / (time_to_produce_stock[1] / assembler_total_speed_bonus), 
The error is "Attempt to perform arithmetic on field '?' (a nil value)

Code: Select all

function getResultItemsPerSecond(stock_name, new_name, compression_ratio, vanilla_recipe)
	local recipe_name = new_name .. "-recipe"
	local new_recipe = data.raw.recipe[stock_name]

	local time_to_produce_stock = {}
	if new_recipe.energy_required then
		time_to_produce_stock = { new_recipe.energy_required, new_recipe.energy_required }
	else
		--normal and expensive aren't guaranteed to exist, but they *should* in this case.
		time_to_produce_stock = { new_recipe.normal.energy_required, new_recipe.expensive.energy_required }
	end
	
	local items_per_second = { 
		["normal"] = 1 / (time_to_produce_stock[1] / assembler_total_speed_bonus), 
		["expensive"] = 1 / (time_to_produce_stock[2] / assembler_total_speed_bonus)
	}
	
	return items_per_second
end
The fundamental problem appears to be that time_to_produce_stock isn't being updated the way that I think it is. Almost as if setting a table's values inside an if statement wasn't allowed. But that makes no sense to me. What am I missing?

Relevant code for assembler_total_speed_bonus:

Code: Select all

beacon_count = 12
--//modules (level 3)
local spd_module_speed_bonus = data.raw.module["speed-module-3"].effect.speed.bonus
local spd_module_pwr_penality = data.raw.module["speed-module-3"].effect.consumption.bonus
local prod_mod_speed_penalty = data.raw.module["productivity-module-3"].effect.speed.bonus
local prod_mod_prod_bonus = data.raw.module["productivity-module-3"].effect.productivity.bonus
local prod_mod_pwr_penality = data.raw.module["productivity-module-3"].effect.consumption.bonus
prod_mod_pollution_penalty = data.raw.module["productivity-module-3"].effect.pollution.bonus

total_beacon_speed_bonus = spd_module_speed_bonus * beacon_count

base_ass_entity = data.raw["assembling-machine"]["assembling-machine-3"]
assembler_base_speed = base_ass_entity.crafting_speed
assembler_base_modules = base_ass_entity.module_specification.module_slots

local assembler_modules_speed_effect = assembler_base_modules * prod_mod_speed_penalty

assembler_total_speed_bonus = assembler_base_speed * (assembler_modules_speed_effect + total_beacon_speed_bonus + 1)
Last edited by Kingdud on Mon May 11, 2020 5:41 pm, edited 1 time in total.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: I'm blind and can't see my lua bug

Post by Optera »

Your check doesn't handle all valid ways energy_required can be set.

Going by the final examples in https://wiki.factorio.com/Prototype/Recipe it should be:

Code: Select all

  local normal_time = 
    new_recipe.normal and new_recipe.normal.energy_required 
    or new_recipe.energy_required 
    or new_recipe.expensive and new_recipe.expensive.energy_required
  local expensive_time = 
    new_recipe.expensive and new_recipe.expensive.energy_required
    or new_recipe.energy_required 
    or new_recipe.normal and new_recipe.normal.energy_required

Kingdud
Long Handed Inserter
Long Handed Inserter
Posts: 76
Joined: Sat Dec 16, 2017 3:23 am
Contact:

Re: I'm blind and can't see my lua bug

Post by Kingdud »

I found the problem. energy_required is not required to be defined at all. Green circuits don't define this property, instead using the default of 0.5. That was the problem.

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 883
Joined: Sun Mar 20, 2016 2:51 am
Contact:

Re: [Solved] I'm blind and can't see my lua bug

Post by Impatient »

Now that you know it, can you change the title to something that describes what the specific problem (and what the solution was)? This would make this thread more valuable than with a generic "[Solved] I'm blind and can't see my lua bug" as it is now. Which can be anything. I am thinking the stackoverflow way ( https://stackoverflow.com/ )

Post Reply

Return to “Modding help”