Use Electric Energy as Burner? Is it even possible?

Place to get help with not working mods / modding interface.
Paul aka D3rPr0
Manual Inserter
Manual Inserter
Posts: 2
Joined: Thu Jan 14, 2016 8:58 pm
Contact:

Use Electric Energy as Burner? Is it even possible?

Post by Paul aka D3rPr0 »

I had a pretty neat idea to make a Electric car which you drive up to for example a Substation and charge it up. I'm new to modding Factorio, so this might be a really simple Problem, but
1. I want to detect if the car is in an powered Area(next to an electric Pole) so i can trigger the charge up and add up the Variable ElectricChargeOfCar which I will be using down below by seconds:

Code: Select all

local clock = os.clock
function wait(n)
local t0 = clock()
while clock() - t0 <= n do end
end

while(true)do
ElectricChargeOfCar = ElectricChargeOfCar + 1
wait(1)
end
2. I want to simply make the car take a variable as a fuel level so I can charge it up with a loop(I just guess this would be simpler than trying to use energy in the burner bracket):

Code: Select all

burner = {
fuellevel= ElectricChargeOfCar
}

Somewhere else, don't know where:
if (driving) then
ElectricChargeOfCar = ElectricChargeOfCar - 1
end
(Tab is not working in the Editor in this Forum so excuse the bad formating)

I hope you understand this well because I tried to explain my Problem as understandable as possible (with my knowledge of the englisch language, I'm not a native speaker)

I also already thought about solving this Problem by creating a new type like

Code: Select all

type = "newcartype"
so I can give this type all the contents the normal type "car", but just not with a burner, just with a electric charge or something else, that would be my Idea #2, even tho I still think Idea #1 (the one above) is better.

memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

Re: Use Electric Energy as Burner? Is it even possible?

Post by memcallen »

First, never use an infinite while loop when modding factorio, it tends to make the game freeze completely. Use the 'on_tick' event instead (on_tick)

Second, there are a few different ways to do this, the most efficient (I think) is to look for an accumulator around the car using this method. Unfortunately, electric poles don't actually contain any energy afaik, so you would need to make something like an accumulator instead.


For example:

Code: Select all

require("defines")
script.on_event(defines.events.on_tick, function(event)
  
  if not global.carEnergy then
    global.carEnergy = 0
  end
  
  if not game.player.vehicle then return end
  
  local car = game.player.vehicle -- gets a reference to the car
  local surface = car.surface -- gets the car's current surface, normally game.surfaces.nauvis works fine though
  
  local position = car.position -- gets the car's position
  
  local entitiesFound = surface.find_entities_filtered{area = {{position.x-10, position.y-10}, {position.x+10, position.y+10}}, type = "accumulator"} -- finds all the entities around the car in a 10 meter radius
  
  for _,entity in pairs(entitiesFound) do -- loops through all the accumulators
    entity.energy = entity.energy - 1
    global.carEnergy = global.carEnergy + 1
  end
  
  if car.energy < 1000 and global.carEnergy > 0 then
    car.energy = 3000
    global.carEnergy = global.carEnergy - 1 -- cars use a lot of energy
    game.player.print(global.carEnergy)
  end
end)
Keep in mind that this will make any car you enter an electric car, and it's not multiplayer compatible. It should give you a basic idea of how cars and some other key things work.

Paul aka D3rPr0
Manual Inserter
Manual Inserter
Posts: 2
Joined: Thu Jan 14, 2016 8:58 pm
Contact:

Re: Use Electric Energy as Burner? Is it even possible?

Post by Paul aka D3rPr0 »

Thanks for that, again sorry for this simple Question, but where will this script go and shouldn't I need to define this new energytype in the cars' entity.lua?

Post Reply

Return to “Modding help”