[RIP]I dont know why this Fails ...
Posted: Sat Dec 10, 2016 11:25 am
by LuziferSenpai
Hey Guys,
I'm working on rewriting all my MODs and than i came in to this Error:
First to improve some stuff, second to make it much easier to add higher electric locomotives and new locomotives.
Pls help me.
Greetz,
Luzifer
I'm working on rewriting all my MODs and than i came in to this Error:
Code: Select all
Error while running event on_built_entity (ID 6)
__SenpaisElectricTrains__/control.lua:45: attempt to get length of field 'Trains' (a nil value)
OLD Code
data lua
control lua
Code: Select all
local trainname = "Senpais-Electric-Train"
local accuname = "Senpais-Power-Provider"
local MODNAME = "__SenpaisElectricTrains__/graphics/"
local train_entity = util.table.deepcopy( data.raw["locomotive"]["diesel-locomotive"] )
train_entity.name = trainname
train_entity.icon = MODNAME..trainname..".png"
train_entity.minable = { mining_time = 1, result = trainname }
train_entity.energy_source = { type = "burner", effectivity = 0.01, fuel_inventory_size = 0, smoke = { { name = "train-smoke", deviation = { 0.3, 0.3 }, frequency = 100, position = { 0, 0 }, starting_frame = 0,
starting_frame_deviation = 60, height = 2, height_deviation = 0.5, starting_vertical_speed = 0.2, starting_vertical_speed_deviation = 0.1 } } }
train_entity.color = { r = 40, g = 210, b = 154 }
local train_item = util.table.deepcopy( data.raw["item-with-entity-data"]["diesel-locomotive"] )
train_item.name = trainname
train_item.icon = MODNAME..trainname..".png"
train_item.order = "a[train-system]-z["..trainname.."]"
train_item.place_result = trainname
local train_recipe = util.table.deepcopy( data.raw["recipe"]["diesel-locomotive"] )
train_recipe.name = trainname
train_recipe.ingredients = { { "diesel-locomotive", 1 }, { "battery", 10 }, { "electric-engine-unit", 20 } }
train_recipe.result = trainname
local accu_entity = util.table.deepcopy( data.raw["electric-energy-interface"]["electric-energy-interface"] )
accu_entity.name = accuname
accu_entity.icon = MODNAME..accuname.."-i.png"
accu_entity.minable = { hardness = 0.2, mining_time = 0.5, result = accuname }
accu_entity.energy_source = { type = "electric", buffer_capacity = "1200KJ", usage_priority = "primary-input", input_flow_limit = "1100KW", output_flow_limit = "0W" }
accu_entity.energy_production = "0kW"
accu_entity.energy_usage = "0kW"
accu_entity.picture = { filename = MODNAME..accuname.."-e.png", priority = "extra-high", width = 124, height = 103, shift = { 0.6875, -0.203125 } }
accu_entity.charge_animation = { filename = MODNAME..accuname.."-charge.png", width = 138, height = 135, line_length = 8, frame_count = 24, shift = { 0.46875, -0.640625 }, animation_speed = 0.5 }
accu_entity.discharge_animation = { filename = MODNAME..accuname.."-discharge.png", width = 147, height = 128, line_length = 8, frame_count = 24, shift = { 0.390625, -0.53125 }, animation_speed = 0.5 }
local accu_item = util.table.deepcopy( data.raw["item"]["accumulator"] )
accu_item.name = accuname
accu_item.icon = MODNAME..accuname.."-i.png"
accu_item.order = "e[accumulator]-d["..accuname.."]"
accu_item.place_result = accuname
local accu_recipe = util.table.deepcopy( data.raw["recipe"]["accumulator"] )
accu_recipe.name = accuname
accu_recipe.ingredients = { { "accumulator", 2 }, { "battery", 5 }, { "electronic-circuit", 10 } }
accu_recipe.result = accuname
local senpais_electric_train_tech = util.table.deepcopy( data.raw["technology"]["railway"] )
senpais_electric_train_tech.name = trainname
senpais_electric_train_tech.icon = MODNAME.."tech.png"
senpais_electric_train_tech.icon_size = 128
senpais_electric_train_tech.effects = { { type = "unlock-recipe", recipe = trainname }, { type = "unlock-recipe", recipe = accuname } }
senpais_electric_train_tech.prerequisites = { "railway", "electric-engine", "battery", "electric-energy-distribution-2" }
senpais_electric_train_tech.unit = { count = 150, ingredients = { { "science-pack-1", 2 }, { "science-pack-2", 2}, { "science-pack-3", 1} }, time = 50 }
senpais_electric_train_tech.order = "s-e-t"
data:extend( { train_entity, train_item, train_recipe, accu_entity, accu_item, accu_recipe, senpais_electric_train_tech } )
Code: Select all
script.on_init(function() On_Load() end)
script.on_load(function() On_Load() end)
script.on_event(defines.events.on_tick, function() On_Tick() end)
script.on_event(defines.events.on_built_entity, function(event) On_Built(event) end)
script.on_event(defines.events.on_robot_built_entity, function(event) On_Built(event) end)
script.on_event(defines.events.on_preplayer_mined_item, function(event) On_Removed(event) end)
script.on_event(defines.events.on_robot_pre_mined, function(event) On_Removed(event) end)
script.on_event(defines.events.on_entity_died, function(event) On_Removed(event) end)
PowerMax = 10666.6666666667
function On_Load()
if not global.SenpaisTrainsList then
global.SenpaisTrainsList = {}
end
if not global.SenpaisAccuList then
global.SenpaisAccuList = {}
end
end
function On_Tick()
local NeededPower = 0
for i=1,#global.SenpaisTrainsList do
NeededPower = NeededPower + (PowerMax - global.SenpaisTrainsList[i].energy)
end
local StoredPower = 0
for i=1,#global.SenpaisAccuList do
StoredPower = StoredPower + global.SenpaisAccuList[i].energy
end
local AvaiblePower = 0
AvaiblePower = StoredPower - NeededPower
if AvaiblePower >= 0 then
for i=1,#global.SenpaisTrainsList do
global.SenpaisTrainsList[i].energy = PowerMax
end
local PerPower = 0
PerPower = AvaiblePower / #global.SenpaisAccuList
for i=1,#global.SenpaisAccuList do
global.SenpaisAccuList[i].energy = PerPower
end
else
for i=1,#global.SenpaisAccuList do
global.SenpaisAccuList[i].energy = 0
end
PerPower = (NeededPower + AvaiblePower) / #global.SenpaisTrainsList
for i=1,#global.SenpaisTrainsList do
global.SenpaisTrainsList[i].energy = PerPower
end
end
end
function On_Built(event)
local entity = event.created_entity
if entity.name == "Senpais-Power-Provider" then
global.SenpaisAccuList[#global.SenpaisAccuList + 1] = entity
end
if entity.name == "Senpais-Electric-Train" then
global.SenpaisTrainsList[#global.SenpaisTrainsList + 1] = entity
end
if entity.name == "elec-battle-loco-1" then
global.SenpaisTrainsList[#global.SenpaisTrainsList + 1] = entity
end
if entity.name == "elec-battle-loco-2" then
global.SenpaisTrainsList[#global.SenpaisTrainsList + 1] = entity
end
if entity.name == "elec-battle-loco-3" then
global.SenpaisTrainsList[#global.SenpaisTrainsList + 1] = entity
end
if entity.name == "elec-battle-loco-4" then
global.SenpaisTrainsList[#global.SenpaisTrainsList + 1] = entity
end
end
function On_Removed(event)
local entity = event.entity
if entity.name == "Senpais-Power-Provider" then
for i=1,#global.SenpaisAccuList do
if entity == global.SenpaisAccuList[i] then
table.remove( global.SenpaisAccuList, i )
return
end
end
end
if entity.name == "Senpais-Electric-Train" then
for i=1,#global.SenpaisTrainsList do
if entity == global.SenpaisTrainsList[i] then
table.remove(global.SenpaisTrainsList, i)
return
end
end
end
if entity.name == "Senpais-Power-Provider" then
for i=1,#global.SenpaisAccuList do
if entity == global.SenpaisAccuList[i] then
table.remove(global.SenpaisAccuList, i)
return
end
end
end
if entity.name == "elec-battle-loco-1" then
for i=1,#global.SenpaisTrainsList do
if entity == global.SenpaisTrainsList[i] then
table.remove(global.SenpaisTrainsList, i)
return
end
end
end
if entity.name == "elec-battle-loco-2" then
for i=1,#global.SenpaisTrainsList do
if entity == global.SenpaisTrainsList[i] then
table.remove(global.SenpaisTrainsList, i)
return
end
end
end
if entity.name == "elec-battle-loco-3" then
for i=1,#global.SenpaisTrainsList do
if entity == global.SenpaisTrainsList[i] then
table.remove(global.SenpaisTrainsList, i)
return
end
end
end
if entity.name == "elec-battle-loco-4" then
for i=1,#global.SenpaisTrainsList do
if entity == global.SenpaisTrainsList[i] then
table.remove(global.SenpaisTrainsList, i)
return
end
end
end
end
New Code
Why I rework this?data lua
control lua
Code: Select all
if not Senpais then Senpais = {} end
if not Senpais.ElectricTrains then Senpais.ElectricTrains = {} end
if not Senpais.ElectricTrains.Functions then Senpais.ElectricTrains.Functions = {} end
if not Senpais.ElectricTrains.Trains then Senpais.ElectricTrains.Trains = {} end
local accuname = "Senpais-Power-Provider"
local MODNAME = "__SenpaisElectricTrains__"
function Senpais.ElectricTrains.Functions.Create_Locomotive ( nama, ican, colar, orderi, recipa, healthy, Weight, MaxSpeed, multiplyer, categore )
local MaxPower = multiplyer * 600 .. "kW"
local train_entity = util.table.deepcopy( data.raw["locomotive"]["diesel-locomotive"] )
train_entity.name = nama
train_entity.icon = ican
train_entity.minable = { mining_time = 1, result = nama }
train_entity.health = healthy
train_entity.weight = Weight
train_entity.max_speed = MaxSpeed
train_entity.power = MaxPower
train_entity.energy_source = { type = "burner", effectivity = 0.01, fuel_inventory_size = 0, smoke = { { name = "train-smoke", deviation = { 0.3, 0.3 }, frequency = 100, position = { 0, 0 }, starting_frame = 0,
starting_frame_deviation = 60, height = 2, height_deviation = 0.5, starting_vertical_speed = 0.2, starting_vertical_speed_deviation = 0.1 } } }
train_entity.color = colar
local train_item = util.table.deepcopy( data.raw["item-with-entity-data"]["diesel-locomotive"] )
train_item.name = nama
train_item.icon = ican
train_item.order = orderi
train_item.place_result = nama
if categore ~= nil then
train_item.category = categore
end
local train_recipe = util.table.deepcopy( data.raw["recipe"]["diesel-locomotive"] )
train_recipe.name = nama
train_recipe.ingredients = recipa
train_recipe.result = nama
data:extend( { train_entity, train_item, train_recipe } )
local data = { name = nama, multiplier = multiplyer }
Senpais.ElectricTrains.Trains[#Senpais.ElectricTrains.Trains + 1] = data
end
Senpais.ElectricTrains.Functions.Create_Locomotive( "Senpais-Electric-Train", MODNAME .. "/graphics/Senpais-Electric-Train.png", { r = 40, g = 210, b = 154 }, "a[train-system]-z[Senpais-Electric-Train]",
{ { "diesel-locomotive", 1 }, { "battery", 10 }, { "electric-engine-unit", 20 } }, 1000, 2000, 1.2, 1 )
local accu_entity = util.table.deepcopy( data.raw["electric-energy-interface"]["electric-energy-interface"] )
accu_entity.name = accuname
accu_entity.icon = MODNAME .. "/graphics/" .. accuname .. "-i.png"
accu_entity.minable = { hardness = 0.2, mining_time = 0.5, result = accuname }
accu_entity.energy_source = { type = "electric", buffer_capacity = "1200KJ", usage_priority = "primary-input", input_flow_limit = "1100KW", output_flow_limit = "0W" }
accu_entity.energy_production = "0kW"
accu_entity.energy_usage = "0kW"
accu_entity.picture = { filename = MODNAME .. "/graphics/" .. accuname.. "-e.png", priority = "extra-high", width = 124, height = 103, shift = { 0.6875, -0.203125 } }
accu_entity.charge_animation = { filename = MODNAME .. "/graphics/" .. accuname .. "-charge.png", width = 138, height = 135, line_length = 8, frame_count = 24, shift = { 0.46875, -0.640625 }, animation_speed = 0.5 }
local accu_item = util.table.deepcopy( data.raw["item"]["accumulator"] )
accu_item.name = accuname
accu_item.icon = MODNAME .. "/graphics/" .. accuname .. "-i.png"
accu_item.order = "e[accumulator]-d["..accuname.."]"
accu_item.place_result = accuname
local accu_recipe = util.table.deepcopy( data.raw["recipe"]["accumulator"] )
accu_recipe.name = accuname
accu_recipe.ingredients = { { "accumulator", 2 }, { "battery", 5 }, { "electronic-circuit", 10 } }
accu_recipe.result = accuname
local senpais_electric_train_tech = util.table.deepcopy( data.raw["technology"]["railway"] )
senpais_electric_train_tech.name = "Senpais-Electric-Train"
senpais_electric_train_tech.icon = MODNAME .. "/graphics/tech.png"
senpais_electric_train_tech.icon_size = 128
senpais_electric_train_tech.effects = { { type = "unlock-recipe", recipe = "Senpais-Electric-Train" }, { type = "unlock-recipe", recipe = accuname } }
senpais_electric_train_tech.prerequisites = { "railway", "electric-engine", "battery", "electric-energy-distribution-2" }
senpais_electric_train_tech.unit = { count = 150, ingredients = { { "science-pack-1", 2 }, { "science-pack-2", 2}, { "science-pack-3", 1} }, time = 50 }
senpais_electric_train_tech.order = "s-e-t"
data:extend( { accu_entity, accu_item, accu_recipe, senpais_electric_train_tech } )
Code: Select all
if not Senpais then Senpais = {} end
if not Senpais.ElectricTrains then Senpais.ElectricTrains = {} end
if not Senpais.ElectricTrains.TrainsList then Senpais.ElectricTrains.TrainsList = {} end
if not Senpais.ElectricTrains.AccuList then Senpais.ElectricTrains.AccuList = {} end
MaxPower = 10666.6666666667
local function ONTICK ()
local PowerNeed = 0
local PowerStorage = 0
local PowerAvaible = 0
local PowerPer = 0
for i = 1, #Senpais.ElectricTrains.TrainsList do
PowerNeed = PowerNeed + ( ( MaxPower * Senpais.ElectricTrains.TrainsList[i].multi ) - Senpais.ElectricTrains.TrainsList[i].entitie.energy )
end
for i = 1, #Senpais.ElectricTrains.AccuList do
PowerStorage = PowerStorage + Senpais.ElectricTrains.AccuList[i].energy
end
PowerAvaible = PowerStorage - PowerNeed
if PowerAvaible >= 0 then
for i = 1, #Senpais.ElectricTrains.TrainsList do
Senpais.ElectricTrains.TrainsList[i].entitie.energy = MaxPower * Senpais.ElectricTrains.TrainsList[i].multi
end
PowerPer = PowerAvaible / #Senpais.ElectricTrains.AccuList
for i = 1, #Senpais.ElectricTrains.AccuList do
Senpais.ElectricTrains.AccuList[i].energy = PowerPer
end
else
for i = 1, #Senpais.ElectricTrains.AccuList do
Senpais.ElectricTrains.AccuList[i].energy = 0
end
PowerPer = ( PowerNeed + PowerAvaible ) / #Senpais.ElectricTrains.TrainsList
for i = 1, #Senpais.ElectricTrains.TrainsList do
Senpais.ElectricTrains.TrainsList[i].entitie.energy = PowerPer
end
end
end
local function ONBUILT ( event )
local entity = event.created_entity
if entity.name == "Senpais-Power-Provider" then
Senpais.ElectricTrains.AccuList[#Senpais.ElectricTrains.AccuList + 1] = entity
end
if entity.type == "locomotive" then
for i = 1, #Senpais.ElectricTrains.Trains do
if entity.name == Senpais.ElectricTrains.Trains[i].name then
Senpais.ElectricTrains.TrainsList[#Senpais.ElectricTrains.TrainsList + 1] = { multi = Senpais.ElectricTrains.Trains[i].multiplier, entitie = entity }
break
end
end
end
end
local function ONREMOVE ( event )
local entity = event.entity
if entity.name == "Senpais-Power-Provider" then
for i = 1, #Senpais.ElectricTrains.AccuList do
if entity == Senpais.ElectricTrains.AccuList[i] then
table.remove( Senpais.ElectricTrains.AccuList, i )
break
end
end
end
if entity.type == "locomotive" then
for o = 1, #Senpais.ElectricTrains.Trains do
if entity.name == Senpais.ElectricTrains.Trains[o].name then
for i = 1, #Senpais.ElectricTrains.TrainsList do
if entity == Senpais.ElectricTrains.TrainsList[i].entitie then
table.remove( Senpais.ElectricTrains.TrainsList, i )
break
end
end
end
end
end
end
script.on_event(defines.events.on_tick, ONTICK )
script.on_event(defines.events.on_built_entity, ONBUILT )
script.on_event(defines.events.on_robot_built_entity, ONBUILT )
script.on_event(defines.events.on_preplayer_mined_item, ONREMOVE )
script.on_event(defines.events.on_robot_pre_mined, ONREMOVE )
script.on_event(defines.events.on_entity_died, ONREMOVE )
First to improve some stuff, second to make it much easier to add higher electric locomotives and new locomotives.
Pls help me.
Greetz,
Luzifer