Page 1 of 1

Why the error?

Posted: Wed Jul 20, 2016 1:51 pm
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


Re: Why the error?

Posted: Wed Jul 20, 2016 2:45 pm
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.

Re: Why the error?

Posted: Wed Jul 20, 2016 3:19 pm
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?

Re: Why the error?

Posted: Wed Jul 20, 2016 4:42 pm
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