Events
Events
Is it possible to add events to the power grid?
Like when close or at max capacity, that rolling blackouts start? Or power line malfunctions?
Like when close or at max capacity, that rolling blackouts start? Or power line malfunctions?
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
You can check for entities energy in the ontick event.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
Re: Events
Ok, but is it possible to have a chance that something happens? Like 1% chance that a power line fails?
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
Dysoch wrote:[...]rolling blackouts start? Or power line malfunctions?
Interesting Hm...in control.lua you could easily use if math.random(100) == 1 for a 1% chance or math.random(1000/entity.energy)==1 so that the more energy the more likely it is. However electric poles do not have energy so you'd have to measure the entities around them or the energy of generators/solar panels. I think you could take down the power line by changing active to false, but as for a rolling blackout I don't think there is (currently) a way to tell what entities a pole is connected to, so you'd have to use findentities or findfilteredentities and start small, increasing the area when the table is empty. Though even then it's not guaranteed that the poles are connected (since you can manually wire them), just more likely.Ok, but is it possible to have a chance that something happens? Like 1% chance that a power line fails?
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
Re: Events
Ill see what i can create, thx for the help
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
There is couple of more (onbuiltentity, onputitem, onplayermineditem, etc.) and we will be extending this interface as well (there is onplayercrafted coming soon for instance).ficolas wrote:You can check for entities energy in the ontick event.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
Re: Events
Playercrafted is good, need that for my dynamic research system i want to put in place in my mod.slpwnd wrote:There is couple of more (onbuiltentity, onputitem, onplayermineditem, etc.) and we will be extending this interface as well (there is onplayercrafted coming soon for instance).ficolas wrote:You can check for entities energy in the ontick event.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
this is my control.lua:
but the event isnt working (the one with the rubber tree)
what is wrong or missing?
is it because the rubber trees are added by the autoplace?
code is from f-mod (thx ficolas for your amazing codes!) (credits will be given at modpage when released!)
Code: Select all
require "defines"
glob.specieOfTreeTable={"rubber"}
if remote.interfaces["F-mod"] then
remote.call("F-mod", "addboiler", "boiler-mk2")
remote.call("F-mod", "addboiler", "boiler-mk3")
remote.call("F-mod", "addboiler", "boiler-mk4")
remote.call("F-mod", "addboiler", "boiler-mk5")
end
game.onevent(defines.events.ontick, function(event)
if event.tick%3600==0 then
for _,specie in pairs (glob.specieOfTreeTable) do
if glob[specie.."trees"]~=nil then
for i,d in pairs(glob[specie.."trees"]) do
if d.entity.valid then
if math.random(5)==1 and d.entity.name==specie.."-tree" then
d.entity.insert({name=specie.."-tree-resin",count=1})
end
else
table.remove(glob[specie.."trees"],i)
end
end
end
end
end
end)
what is wrong or missing?
is it because the rubber trees are added by the autoplace?
code is from f-mod (thx ficolas for your amazing codes!) (credits will be given at modpage when released!)
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
From what you posted I can not see if you have a glob.rubbertrees to look through, with autoplace you'd need to use onchunkgenerated and add any of your trees to the table, if you didn't then the table is going to be empty when the code runs through it and so it won't do anything. Also you could always throw a game.player.print(serpent.block(glob[specie.."trees"])) into the for loop to see it.
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
Re: Events
I could be wrong, but if i use autoplace in the entity file, isnt that enough? The event i use should search for rubber-tree (is the entity name) and add the resin.FreeER wrote:From what you posted I can not see if you have a glob.rubbertrees to look through, with autoplace you'd need to use onchunkgenerated and add any of your trees to the table, if you didn't then the table is going to be empty when the code runs through it and so it won't do anything. Also you could always throw a game.player.print(serpent.block(glob[specie.."trees"])) into the for loop to see it.
Thats what i think after looking thru all the files. But, still new to modding, so i could be wrong.
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
For this, no. You usedDysoch wrote:if i use autoplace in the entity file, isnt that enough?
Code: Select all
if glob[specie.."trees"]~=nil then
for i,d in pairs(glob[specie.."trees"]) do
[...]
F-code snippet
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
Re: Events
Another advantage using something similar instead of the autoplacer is that you can specify the force, so you probably can get rid off the red dots on map.FreeER wrote:F-code snippet
Re: Events
What you can do to use autoplace is use onchunkgenerated to add the trees to a table, by looking for them in the chunk that has been generated, with findentitiesfiltered.
Re: Events
Code: Select all
game.onevent(defines.events.onchunkgenerated, function(event)
if math.random(5)==1 then
local specieOfTree=glob.specieOfTreeTable[math.random(#glob.specieOfTreeTable)]
local treex = event.area.lefttop.x+math.random(32)
local treey = event.area.lefttop.y+math.random(32)
local typeoftree = math.random(3)
if glob[specieOfTree.."trees"]==nil then
glob[specieOfTree.."trees"]={}
end
local createdentity=game.createentity( {name=(specieOfTree.."-tree"..typeoftree), position={treex,treey}} )
table.insert(glob[specieOfTree.."trees"], {entity=createdentity} )
end
end)
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
Well because you programmed it like that
You used:
where typeoftree is a random number between 1-3
You used:
Code: Select all
game.createentity( {name=(specieOfTree.."-tree"..typeoftree), position={treex,treey}} )
Re: Events
Ok they generate now, resin is produced every once in a while, but still the red dots. How can i change or imply the force of the entity?
using the berry's build by f-mod to grow more trees, as people like to automate the process of rubber making (i think )
using the berry's build by f-mod to grow more trees, as people like to automate the process of rubber making (i think )
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
Code: Select all
game.onevent(defines.events.ontick, function(event)
if event.tick%3600==0 then
for _,specie in pairs (glob.specieOfTreeTable) do
if glob[specie.."trees"]~=nil then
for i,d in pairs(glob[specie.."trees"]) do
if d.entity.valid then
if math.random(5)==1 and d.entity.name==specie.."-tree" then
d.entity.insert({name=specie.."-tree-resin",count=1})
end
else
table.remove(glob[specie.."trees"],i)
end
end
end
end
end
if string.find(event.createdentity.name,"-resin")~=nil then --[[Berries build]]--
specie = string.gsub(event.createdentity.name, '%-resin', '') (THINK ERROR IS HERE!)
if glob[specie.."trees"]==nil then
glob[specie.."trees"]={}
end
table.insert(glob[specie.."trees"], {entity=game.createentity({name=specie.."-tree", position=event.createdentity.position}), time=0})
event.createdentity.destroy()
end
end)
all i did was remove the small tree builds, changed berry to resin and added resin as an entity. think it should work.
but anyway, already thanks all, still learning events, but im slowly getting there
EDIT:
Question 2: this should work, but from what i see, works with the more power, the more chance. but is that full network power usage, or at or near max power production per entity?
Code: Select all
if glob.steamengines~=nil and math.random(1000/entity.energy)==1 then --[[Power failure steam engines]]--
for i,_ in pairs(glob.steamengines) do
if foundsteamengines==true then
if glob.steamengines[i].entity.valid then
glob.steamengines[i].entity.die()
game.getplayer().print("Your steam engine was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
if glob.solarpanels~=nil and math.random(1000/entity.energy)==1 then --[[Power failure solar panels]]--
for i,_ in pairs(glob.solarpanels) do
if foundsolarpanels==true then
if glob.solarpanels[i].entity.valid then
glob.solarpanels[i].entity.die()
game.getplayer().print("Your solar panel was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
after a few tries, and looking throu the wiki, i got the red dots to dissapear. used force=game.forces.neutral to correct it.
but still need help with the resin builds and the events with the power. (post above!)
but still need help with the resin builds and the events with the power. (post above!)
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
Re: Events
First the d.entity.name is not needed herebecause each table is only for that specific tree, if you were using a single table for all of your trees then you'd want to check the name...
as forUnless you have a different resin for each tree then the name should probably be "tree-resin", but since I can't see the full mod I don't know for sure
Now the "attempted to index field 'createdentity' (a nil value)" is because createdentity is not a table that is provided by ontick, it's provided by onbuiltentity. From the looks of it that's where this code was meant to be so all you need to do is end ontick and start onbuiltentity withLooking at the code for it though I'm confused as to why you are looking for a -resin entity being built instead of a tree...I'd have expected to see you finding built trees and adding them to the proper table here, not resin trees that you immediately replace with trees. From my understanding the resin is an item that is being inserted into the tree (container) so it will never be 'built'. With that you'd want eitherorThese should both do the same, just slightly different ways of seeing if the entity is one of your trees. To be honest, the second may be easier since I'm not sure if the specieOfTreeTable index names will be the same as that of the trees. Again, these assume that you are adding the built tree entities to their tables rather than the original code that seems to be immediately replacing any -resin entities with tree entities and adding those to the tables
edit: as to the power solarpanel.energy seems to be the energy given per tick = 0 when night (no output), however the steam engines seem to give 9.0666666666667 when they are not doing anything (perhaps that would be the available peformace??). I'll try to research it a bit more, but I'm not certain I'll be able to understand what I find lol
edit2: steamengine.energy seems to be about 9 when connected but not used, 0 when not connected to anything and 0 when at max output??
solarpanel also seems to be 0 when in full use and goes to 1.06666666666667 when connected but nothing is drawing power. So I'm going to guess that highest use is as energy approaches 0...Thus I think the formula you'd want to use is more along the lines of math.floor(math.random(10*entity.energy+1))==2 (should give 10% chance of failure at nearly full usage, unfortunately if i checked things right then full use would appear the same as disconnected. just change the 10 at the front to make it higher or lower chance) the plus one is so that you don't pass a range of 0 to math.random and you need to check for 2 because of that addition (since 0 could be a disconnected entity, you don't want to get math.random(0+1)==1)
Code: Select all
--ontick code
if math.random(5)==1 and d.entity.name==specie.."-tree" then
as for
Code: Select all
d.entity.insert({name=specie.."-tree-resin",count=1})
Now the "attempted to index field 'createdentity' (a nil value)" is because createdentity is not a table that is provided by ontick, it's provided by onbuiltentity. From the looks of it that's where this code was meant to be so all you need to do is end ontick and start onbuiltentity with
Code: Select all
game.onevent(defines.events.onbuiltentity, function(event)
Code: Select all
for _, specie in pairs(glob.specieOfTreeTable) do
if event.name==specie then
if glob[specie.."trees"]==nil then
glob[specie.."trees"]={}
end
table.insert(glob[specie.."trees"], {entity=createdentity})
break
end
end--note I used entity= in the table.insert because in the for loop in ontick you used d.entity (if you used just d then you could just insert the createdentity)
Code: Select all
if event.name:sub(-5)=="-tree" then --sub(-5) simply takes the last five characters of the string, so this assumes all your trees end with "-tree"
if glob[specie.."trees"]==nil then
glob[specie.."trees"]={}
end
table.insert(glob[specie.."trees"], {entity=createdentity})
end
edit: as to the power solarpanel.energy seems to be the energy given per tick = 0 when night (no output), however the steam engines seem to give 9.0666666666667 when they are not doing anything (perhaps that would be the available peformace??). I'll try to research it a bit more, but I'm not certain I'll be able to understand what I find lol
edit2: steamengine.energy seems to be about 9 when connected but not used, 0 when not connected to anything and 0 when at max output??
solarpanel also seems to be 0 when in full use and goes to 1.06666666666667 when connected but nothing is drawing power. So I'm going to guess that highest use is as energy approaches 0...Thus I think the formula you'd want to use is more along the lines of math.floor(math.random(10*entity.energy+1))==2 (should give 10% chance of failure at nearly full usage, unfortunately if i checked things right then full use would appear the same as disconnected. just change the 10 at the front to make it higher or lower chance) the plus one is so that you don't pass a range of 0 to math.random and you need to check for 2 because of that addition (since 0 could be a disconnected entity, you don't want to get math.random(0+1)==1)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
Re: Events
Code: Select all
if glob.steamengine~=nil and math.floor(math.random(5*entity.energy+1))==2 then --[[Power failure steam engines]]--
for i,_ in pairs(glob.steamengine) do
if foundsteamengine==true then
if glob.steamengine[i].entity.valid then
glob.steamengine[i].entity.die()
game.getplayer().print("Your steam engine was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
if glob.solarpanel~=nil and math.floor(math.random(5*entity.energy+1))==2 then --[[Power failure solar panels]]--
for i,_ in pairs(glob.solarpanel) do
if foundsolarpanel==true then
if glob.solarpanel[i].entity.valid then
glob.solarpanel[i].entity.die()
game.getplayer().print("Your solar panel was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6