Why the error?

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:

Why the error?

Post by TheSAguy »

Hi,
I'm gettign the below error, and the fix is to change the order of entities built. If I put the wall first, no error, if I put the wall after the control station, I get this error when building the control station. The error line actually refers to the wall code.

Line 134 is "if entity and entity.name == "ne-living-wall" then", but I get the error when building the "AlienControlStation_Area" entity.

again, if the Control station code is at the bottom, no problem, if I put the call under it, error.

Image

No problem:

Code: Select all


---------------------------------------------
function On_Built(event)

local entity = event.created_entity

	
	--- Living Wall built
	if entity and entity.name == "ne-living-wall" then
		if global.Living_Walls_Table == nil then
          global.Living_Walls_Table = {}
		end
		writeDebug("Living Wall has been built")				

		local Created_L_Wall = event.created_entity
		
		table.insert(global.Living_Walls_Table, Created_L_Wall)
		
	end

   --- Terraforming Station has been built
	if entity and entity.name == "TerraformingStation" then
	
	if global.numTerraformingStations < 0 then
		global.numTerraformingStations = 0
	end

      global.numTerraformingStations = global.numTerraformingStations + 1
      
      global.factormultiplier = GetFactorPerTerraformingStation(global.numTerraformingStations)
	  writeDebug("The the number of Terraforming Stations: " .. global.numTerraformingStations)
	  
	end   

	
	--- Alien Control Station has been built
	
	
	if entity and entity.name == "AlienControlStation_Area" then
	local newAlienControlStation
	local surface = event.created_entity.surface
	local force = event.created_entity.force
		
		newAlienControlStation = surface.create_entity({name = "AlienControlStation", position = event.created_entity.position, force = force})
		event.created_entity.destroy()

		table.insert(global.beacons, newAlienControlStation)
	end	

	
end


Error Code. With Wall under Control Station:

Code: Select all

---------------------------------------------
function On_Built(event)

local entity = event.created_entity

	


   --- Terraforming Station has been built
	if entity and entity.name == "TerraformingStation" then
	
	if global.numTerraformingStations < 0 then
		global.numTerraformingStations = 0
	end

      global.numTerraformingStations = global.numTerraformingStations + 1
      
      global.factormultiplier = GetFactorPerTerraformingStation(global.numTerraformingStations)
	  writeDebug("The the number of Terraforming Stations: " .. global.numTerraformingStations)
	  
	end   

	
	--- Alien Control Station has been built
	
	
	if entity and entity.name == "AlienControlStation_Area" then
	local newAlienControlStation
	local surface = event.created_entity.surface
	local force = event.created_entity.force
		
		newAlienControlStation = surface.create_entity({name = "AlienControlStation", position = event.created_entity.position, force = force})
		event.created_entity.destroy()

		table.insert(global.beacons, newAlienControlStation)
	end	
	--- Living Wall built
	if entity and entity.name == "ne-living-wall" then
		if global.Living_Walls_Table == nil then
          global.Living_Walls_Table = {}
		end
		writeDebug("Living Wall has been built")				

		local Created_L_Wall = event.created_entity
		
		table.insert(global.Living_Walls_Table, Created_L_Wall)
		
	end
	
end

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Why the error?

Post by DedlySpyder »

(Reading on my phone, so I may have missed something)

I think you destroy the entity when it's the control station? If so, any code after that is looking for it will see an invalid entity. If your function should just end after it does everthing to the control station then add a return at the end of it's if block, that will stop the function.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Why the error?

Post by TheSAguy »

DedlySpyder wrote:(Reading on my phone, so I may have missed something)

I think you destroy the entity when it's the control station? If so, any code after that is looking for it will see an invalid entity. If your function should just end after it does everthing to the control station then add a return at the end of it's if block, that will stop the function.
So simple!
So I destroy the entity and then on the next check I don't gave an entity, thus the error.

So how would I handle multiple entity destroy cases?
(my case only has 1, so putting it last will work)

Just use elseif? Any better way?
User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Why the error?

Post by DedlySpyder »

TheSAguy wrote:So simple!
So I destroy the entity and then on the next check I don't gave an entity, thus the error.

So how would I handle multiple entity destroy cases?
(my case only has 1, so putting it last will work)

Just use elseif? Any better way?
You elseif, or you could have a return statement at the end of each if. It could return nothing, but as soon as you have a return statement it ends the function
Post Reply

Return to “Modding help”