Page 1 of 1

trying to create new game surface

Posted: Sat May 12, 2018 8:41 am
by kingarthur
im attempting to create a new surface layer and apon starting a new game i get an error stating that it can not index game.

this is the code from the control.lua file inside the mods folder. as far as i can tell according to the api doc it should be game.create_surface to make a new surface but it doesnt seem to work. most of the code has been commented out as i kept getting errors trying to generate a surface when the rocket silo is built.

Code: Select all

--script.on_event(defines.events.on_built_entity, function(event)

--local entity = event.created_entity
--local surface = game.surfaces

--if entity.name == "rocket-silo" then
	--if game.surfaces["Space"] == nil then
		game.create_surface("Space")
		--surface.daytime = 0.5
	--end
--end
--)

Re: trying to create new game surface

Posted: Sat May 12, 2018 10:36 am
by Klonan
kingarthur wrote:im attempting to create a new surface layer and apon starting a new game i get an error stating that it can not index game.

this is the code from the control.lua file inside the mods folder. as far as i can tell according to the api doc it should be game.create_surface to make a new surface but it doesnt seem to work. most of the code has been commented out as i kept getting errors trying to generate a surface when the rocket silo is built.

Code: Select all

--script.on_event(defines.events.on_built_entity, function(event)

--local entity = event.created_entity
--local surface = game.surfaces

--if entity.name == "rocket-silo" then
	--if game.surfaces["Space"] == nil then
		game.create_surface("Space")
		--surface.daytime = 0.5
	--end
--end
--)
I think you are overcomplicating it somehow, this might be clearer:

Code: Select all

script.on_init(function()
  global.surface = game.surfaces.space or game.create_surface("space")
  global.surface.daytime = 0.5
  global.surface.freeze_daytime = true
end)

script.on_event(defines.events.on_build_entity, function(event)
  local entity = event.created_entity
  if not entity.name == "rocket-silo" then return end
  print("Rocket silo built")
end