surface? help for moding and missing knowlage

Place to get help with not working mods / modding interface.
PlatinumEragong
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Sep 08, 2016 7:13 pm
Contact:

surface? help for moding and missing knowlage

Post by PlatinumEragong »

ive an probel i want to update a mode from 12.x to 13 or 14 im getting some errors when running this in my file:

Code: Select all

		local create = game.players[#].surface.create_entity
		local producer = create{name = "powerproducer", position =
			tarpos, force = entity.force}
		struct.producer = producer
		local consumer = create{name = "powerconsumer", position =
			srcpos, force = entity.force}
		struct.consumer = consumer
		local consumer_pole = create{name = "transformator-connection".."_src_"..entity.direction,
			position = srcconpos, force = entity.force}
		struct.consumer_pole = consumer_pole
		local producer_pole = create{name = "transformator-connection".."_tar_"..entity.direction,
			position = tarconpos, force = entity.force}
		struct.producer_pole = producer_pole
		struct.lastdraw = 0
		table.insert(global.transformators, struct)
or if i try this: error while running event on_buildentity(ID6)/...control.lua a:68 attempt to index field 'surface' (a nil value)

Code: Select all

		local create = game.players.surface.create_entity
		local producer = create{name = "powerproducer", position =
			tarpos, force = entity.force}
		struct.producer = producer
		local consumer = create{name = "powerconsumer", position =
			srcpos, force = entity.force}
		struct.consumer = consumer
		local consumer_pole = create{name = "transformator-connection".."_src_"..entity.direction,
			position = srcconpos, force = entity.force}
		struct.consumer_pole = consumer_pole
		local producer_pole = create{name = "transformator-connection".."_tar_"..entity.direction,
			position = tarconpos, force = entity.force}
		struct.producer_pole = producer_pole
		struct.lastdraw = 0
		table.insert(global.transformators, struct)
ive found a link viewtopic.php?f=3&t=27101but dont know hot to use it in my code or how it have to looks like
Removed game.get_player() and game.get_surface(): game.players[] and game.surfaces[] can be used instead.
Changed game.players, game.surfaces, game.entity_prototypes, game.item_prototypes, game.fluid_prototypes, force.recipes, force,technologies to use custom access + iterator objects for improved performance.
Changed game.players[] to work with both the player index and the player name.
Added on_gui_text_changed and on_gui_checked_state_changed events.
Changed "defines" so they're available by default and removed the defines.lua file.

game.player has been removed (use game.players[#] and associated event.player_index during events).
game.local_player has been renamed to game.player and now works through remote calls.
here is the total lua:

Code: Select all

--[[
global.transformators <- list of transformators

transformator:
	trafo     -- the transformator itself
	producer  -- the power producing part
	consumer  -- the power consuming part
	direction -- direction when first placed
	
all local functions are prefixed with "Transformator"
	
--]]

-- Make sure our functions get called for specific events.
-- A robot built one.
script.on_event(defines.events.on_robot_built_entity, function(event) OnBuilt(event.created_entity) end)
-- A player built one.
script.on_event(defines.events.on_built_entity, function(event) OnBuilt(event.created_entity) end)

-- A player removed one.
script.on_event(defines.events.on_preplayer_mined_item, function(event) OnRemoved(event.entity) end)
-- A robot removed one.
script.on_event(defines.events.on_robot_pre_mined, function(event) OnRemoved(event.entity) end)
-- It died.
script.on_event(defines.events.on_entity_died, function(event) OnRemoved(event.entity) end)

local function TransformatorIsTransformator(entity)
	if entity.name == "transformator" then
		return true
	else
		return false
	end
end

local function TransformatorGetSourcePosition(direction)
	if direction == 0 then		return {x = 0, y = 1}   -- north
	elseif direction == 2 then	return {x = -1, y = 0}  -- ost
	elseif direction == 4 then	return {x = 0, y = -1}  -- south
	else						return {x = 1, y = 0}   -- west
	end
end

local function TransformatorGetTargetPosition(direction)
	if direction == 0 then		return {x = 0, y = -1}   -- north
	elseif direction == 2 then	return {x = 1, y = 00}  -- ost
	elseif direction == 4 then	return {x = 0, y = 1}  -- south
	else						return {x = -1, y = 0}   -- west
	end
end
function OnBuilt(entity)
      --game.createentity{name = "pole-light", position = entity.position, force = entity.force}
	if TransformatorIsTransformator(entity) then
		local struct = {trafo = entity}
		struct.direction = entity.direction
		local srcpos = TransformatorGetSourcePosition(struct.direction)
		local tarpos = TransformatorGetTargetPosition(struct.direction)
		local srcconpos = {x = srcpos.x, y = srcpos.y}
		local tarconpos = {x = tarpos.x, y = tarpos.y}
		srcconpos.x = 2 * srcconpos.x + entity.position.x
		srcconpos.y = 2 * srcconpos.y + entity.position.y
		tarconpos.x = 2 * tarconpos.x + entity.position.x
		tarconpos.y = 2 * tarconpos.y + entity.position.y
		srcpos.x = srcpos.x + entity.position.x
		srcpos.y = srcpos.y + entity.position.y
		tarpos.x = tarpos.x + entity.position.x
		tarpos.y = tarpos.y + entity.position.y
		
		local create = game.players.surface.create_entity
		local producer = create{name = "powerproducer", position =
			tarpos, force = entity.force}
		struct.producer = producer
		local consumer = create{name = "powerconsumer", position =
			srcpos, force = entity.force}
		struct.consumer = consumer
		local consumer_pole = create{name = "transformator-connection".."_src_"..entity.direction,
			position = srcconpos, force = entity.force}
		struct.consumer_pole = consumer_pole
		local producer_pole = create{name = "transformator-connection".."_tar_"..entity.direction,
			position = tarconpos, force = entity.force}
		struct.producer_pole = producer_pole
		struct.lastdraw = 0
		table.insert(global.transformators, struct)
	end
end

function OnRemoved(entity)
	if TransformatorIsTransformator(entity) then
		local found_struct
		local index
		for key, struct in pairs(global.transformators) do
			if struct.trafo == entity then
				found_struct = struct
				index = key
				break
			end
		end
		table.remove(global.transformators, index)
		found_struct.producer.destroy()
		found_struct.producer = nil
		found_struct.consumer.destroy()
		found_struct.consumer = nil
		found_struct.producer_pole.destroy()
		found_struct.producer_pole = nil
		found_struct.consumer_pole.destroy()
		found_struct.consumer_pole = nil
	end
end

script.on_init(function(event)
	if global.transformators == nil then
		global.transformators = {}
	end
end)

script.on_event(defines.events.on_player_rotated_entity, function(event)
	local entity = event.entity;
	if TransformatorIsTransformator(entity) then
		for key, struct in pairs(global.transformators) do
			if struct.trafo == entity then
				entity.direction = struct.direction
				break
			end
		end
	end
end)

script.on_event(defines.events.on_tick, function(event)
	--if (game.tick % 18 ~= 0)
	--then
	--	return
	--end
	local efficiency = 0.98 -- efficiency of 98% (realistic for grid transformers)
	local max_amount = 20 -- 360
	for key, struct in pairs(global.transformators) do
		--local newbox = {type = "water", amount = 10, temperature = 25}
		--struct.producer.fluidbox[1] = newbox
		local target_amount = 0
		if struct.producer.fluidbox[1] ~= nil then
			target_amount = struct.producer.fluidbox[1].amount
		end
		local source_energy = struct.consumer.energy -- we can take so much energy from the source
		local target_energy = (max_amount - target_amount) * 10.0 * 1e3 / efficiency -- so much more energy can be stored
		local energy_to_move
		if source_energy > target_energy then
			energy_to_move = target_energy
		else
			energy_to_move = source_energy
		end

		if struct.lastdraw == nil then
			struct.lastdraw = 0
		end
		if energy_to_move > struct.lastdraw then
			energy_to_move = energy_to_move * 0.05 + 0.95 * struct.lastdraw
		end

		--game.players[#].print(tostring(source_energy).." and "..tostring(target_amount))
		--energy_to_move = 0

		struct.lastdraw = energy_to_move
		if energy_to_move ~= 0 then
			local liquid_amount = energy_to_move / 10.0 / 1e3
			--if target_amount ~= 0 then
			--	game.players[#].print(target_amount)
			--end
			local new_amount = target_amount + liquid_amount * efficiency 
			if new_amount > max_amount then
				new_amount = max_amount -- if rounding gone wrong
			end
			struct.producer.fluidbox[1] = {type="water", amount=new_amount, temperature=25}
			struct.consumer.energy = struct.consumer.energy - energy_to_move
		end
	end
end)
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: surface? help for moding and missing knowlage

Post by Adil »

# there represents the player index, a number under which the player is stored in game.players table. edit:(for the most brainded solution it can be substituted with 1)
I've also said in your previous thread on this subject, that it'd be better to use the surface from the entity passed to the function rather than one from the player[1].
Also, if you're completely desperate, you can give a shot to the last link in my signature. That script sometimes manages to get the mod working in 0.13.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
Post Reply

Return to “Modding help”