ss_Baum's questions, now:Add circuit network condition

Place to get help with not working mods / modding interface.
ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

ss_Baum's questions, now:Add circuit network condition

Post by ss_Baum »

Hi everyone, because I asked two questions in two days and because I won't flood this forum I have decided to create a topic, that contains all of my future, simple ore more advanced questions.

Today's question: Add circuit network condition

Is there a way to add a circuit network condition ore do I have to do this with an own gui and control-script?
Last edited by ss_Baum on Wed Feb 03, 2021 7:32 pm, edited 17 times in total.

User avatar
bormand
Fast Inserter
Fast Inserter
Posts: 201
Joined: Fri Jun 05, 2020 9:59 am
Contact:

Re: ss_Baum's questions

Post by bormand »

ss_Baum wrote: ↑
Tue Jan 26, 2021 5:05 pm
I tried it with windows powershell, but I didn't get any prints.
Do you use steam version? Steam version relaunches itself through steam, so it may lose connection to the terminal. Try DRM-free version from the Factorio site, as mentioned in that forum thread.

Anyway, there is a log() function that writes messages to the usual factorio-current.log.

I.e. log(serpent.block(data.raw["item"]["sulfur"]))

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

bormand wrote: ↑
Tue Jan 26, 2021 5:33 pm
ss_Baum wrote: ↑
Tue Jan 26, 2021 5:05 pm
I tried it with windows powershell, but I didn't get any prints.
Do you use steam version? Steam version relaunches itself through steam, so it may lose connection to the terminal. Try DRM-free version from the Factorio site, as mentioned in that forum thread.

Anyway, there is a log() function that writes messages to the usual factorio-current.log.

I.e. log(serpent.block(data.raw["item"]["sulfur"]))
Actually thank you very much. I actually did not read far enough :oops: Sry for wasting your time

User avatar
bormand
Fast Inserter
Fast Inserter
Posts: 201
Joined: Fri Jun 05, 2020 9:59 am
Contact:

Re: ss_Baum's questions

Post by bormand »

ss_Baum wrote: ↑
Tue Jan 26, 2021 5:05 pm
I can't figure out the ID of an object created with create_object.
https://lua-api.factorio.com/latest/Lua ... nit_number probably?

You may want to look at the stop-events.lua in the LTN mod sources. Their station is compound entity consisting of the train stop and two lamps. Very similar to what are you trying to achieve.
ss_Baum wrote: ↑
Tue Jan 26, 2021 5:05 pm
is there a way to create a new global table
Yeah, see https://lua-api.factorio.com/latest/Global.html

Something like global.my_table = {}. And then you can store associations like global.my_table[first_entity.unit_number] = second_entity.

Hope it works. I have very little experience with modding, so don't trust me too much.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

bormand wrote: ↑
Tue Jan 26, 2021 6:00 pm
ss_Baum wrote: ↑
Tue Jan 26, 2021 5:05 pm
I can't figure out the ID of an object created with create_object.
https://lua-api.factorio.com/latest/Lua ... nit_number probably?

You may want to look at the stop-events.lua in the LTN mod sources. Their station is compound entity consisting of the train stop and two lamps. Very similar to what are you trying to achieve.
ss_Baum wrote: ↑
Tue Jan 26, 2021 5:05 pm
is there a way to create a new global table
Yeah, see https://lua-api.factorio.com/latest/Global.html

Something like global.my_table = {}. And then you can store associations like global.my_table[first_entity.unit_number] = second_entity.

Hope it works. I have very little experience with modding, so don't trust me too much.
Thank you very much. I never got the idea to search for unit number. I thought it is called id.
I have actually no experience with factorio modding, and a little with Arma, so you probably know more than do.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

Well, new day, new question.

How do I get the max energy usage of an entity. I can't figure it out with serpent.block print.
I know, how to get the current energy usage, but I also need the max possible. It also would later be possible, to get it from the gui, but there I also need to set the max energy usage, so I have to solve this anyways.

the prototype energy usage won't work, because it should be customizable for every entity.

edit: the prototype print just returns
{
__self = "userdata"
}

if you are already here, can you get more data about the table from the print?

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

I just figured it out myself, it was electric_input_flow_limit. But getting more data from print's would still be nice.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

One question a day is no question a day.

Does anyone have an Idea, if it is possible to change the speed of a belt entity?
I mean probably not, but than I have to create about 30 prototypes for the different speeds.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: ss_Baum's questions

Post by PFQNiet »

Belt speeds are indeed fixed, so you need a different entity for each belt speed you want.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

PFQNiet wrote: ↑
Wed Jan 27, 2021 4:36 pm
Belt speeds are indeed fixed, so you need a different entity for each belt speed you want.
Ok, then I will create them. by the way, can you get the Items on a belt or the ground tile or do I have to get them with the position?

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: ss_Baum's questions

Post by PFQNiet »

You will need to call get_max_transport_line_index() on the entity, then for each line call get_transport_line(index) to get the transport lines. From there you can get the #length of the line, which tells you how many items are on it, then loop through that to get the item stack.

It's a relatively lengthy process, but so long as you don't do it often it'll be fine.

Items on the ground are "item-on-ground" entities, you can use find_entities_filtered() to find them.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

PFQNiet wrote: ↑
Wed Jan 27, 2021 5:26 pm
You will need to call get_max_transport_line_index() on the entity, then for each line call get_transport_line(index) to get the transport lines. From there you can get the #length of the line, which tells you how many items are on it, then loop through that to get the item stack.

It's a relatively lengthy process, but so long as you don't do it often it'll be fine.

Items on the ground are "item-on-ground" entities, you can use find_entities_filtered() to find them.
Ok, thank you very much.
The problem is I will need to do this often, because everytime the belt power supply changes or the player changes the speed, I have to replace the belt with an new belt. That delets the Item on the belt. Or can I change the prototype on an entity with the control lua. I think not, but I will try it first.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: ss_Baum's questions

Post by PFQNiet »

Replace the belt with fast_replace = true and spill = false. This will preserve items on the belt.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

PFQNiet wrote: ↑
Wed Jan 27, 2021 6:04 pm
Replace the belt with fast_replace = true and spill = false. This will preserve items on the belt.
Thank you so much, I didn't saw this.
You saved me so much time

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions

Post by ss_Baum »

Well, new day, new question.

How do I change the electric energy consumption of an unit? I am not allowed to change the prototype in control and I can't find the modifyer.

By the way, I changed the prototype problem, you are actually allowed to loop in the data stage, so I just create the automatically using a for loop.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: ss_Baum's questions, today: modify electric energy-consumption

Post by PFQNiet »

Oh yeah, you can do any code you want in the data stage, the key point is to "data:extend{}" or change "data.raw" entries as needed.

The only entity whose energy consumption can be changed in control is the electric-energy-interface. You can change its various energy properties at control-time.

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions, today: modify electric energy-consumption

Post by ss_Baum »

PFQNiet wrote: ↑
Thu Jan 28, 2021 3:12 pm
Oh yeah, you can do any code you want in the data stage, the key point is to "data:extend{}" or change "data.raw" entries as needed.

The only entity whose energy consumption can be changed in control is the electric-energy-interface. You can change its various energy properties at control-time.
Ok, I actually tried that too and it didn't work. But I tried it with entity.prototype.energy_consumption, I think. I will try it again

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions, now: how to code efficient with the factorio api

Post by ss_Baum »

Today's question: How do I generally write efficient code?

I think, my control.lua will become very long and run very often, so the question is, how to write it in a way, that it run's as fast as possible.
Should I define function's for regularly used things and call the functions with the specified values?
Or is it faster for the code if the code is directly, in the if, then statement?
Does it effect the runtime, if a not executed if condition is longer?

To be clear, the code should be readable for the user, so I don't use shortest possible variables.
Of course I don't print anything in the final code and of course I don't create unnecessary variables ore if-conditions.

If someone knows something in this direction It would be very nice.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: ss_Baum's questions, now: how to code efficient with the factorio api

Post by PFQNiet »

"UPS efficiency" is... a bit of a cult obsession around here :p But realistically, many of the more "quirky" mods like your transport belt idea are just that: quirky. Nobody is going to build a megabase with your mod, and that's okay. In fact, beyond early-game, people aren't likely to be low on power so your custom belts will be running at full speed. If someone is going to build a megabase, they aren't gonna do it with your mod.

With that in mind, efficiency isn't quite as big of an issue for a mod like yours.

Functions are good for organising your code and you should absolutely use them - but they are a (tiny) performance cost so if you've got a function that's only called in one or two places, it may be worthwhile to "unwrap" that function. That is, copy-paste the code from the function and replace the function call with the code directly. However this should only be done if you really need to, because it makes the code harder to maintain later on and the savings is really quite tiny.

If you want to improve your mod performance, you should use a profiler. If you're using VSCode there's a Factorio Debugger extension that comes with profiling, give that a look. It's worth noting that the debugger itself is a huge performance hog - I found this out when code that was taking several seconds to run (during map generation) suddenly completed faster than the eye could see when I ran it without the debugger. But anyway, the profiler will tell you how often your code is run and how long it takes. Look for things that are run often or take a long time and reduce them as much as you can.

Conventional wisdom is to avoid running code "on_tick". But if you limit the amount that you do - I believe I saw you mention only having a single "manager" entity per network - then you can get a surprising amount done in the tick without impacting performance. The game itself is extremely efficient so in general (ie. not in megabases) you will have almost 10ms to "play" with. That's an eternity in computer-time! So don't be afraid to do stuff and be creative. See: self-driving car mods, which have to update every vehicle every tick to keep things running smoothly!

ss_Baum
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Wed Apr 04, 2018 10:58 am
Contact:

Re: ss_Baum's questions, now: how to code efficient with the factorio api

Post by ss_Baum »

PFQNiet wrote: ↑
Fri Jan 29, 2021 12:17 pm
"UPS efficiency" is... a bit of a cult obsession around here :p But realistically, many of the more "quirky" mods like your transport belt idea are just that: quirky. Nobody is going to build a megabase with your mod, and that's okay. In fact, beyond early-game, people aren't likely to be low on power so your custom belts will be running at full speed. If someone is going to build a megabase, they aren't gonna do it with your mod.

With that in mind, efficiency isn't quite as big of an issue for a mod like yours.

Functions are good for organising your code and you should absolutely use them - but they are a (tiny) performance cost so if you've got a function that's only called in one or two places, it may be worthwhile to "unwrap" that function. That is, copy-paste the code from the function and replace the function call with the code directly. However this should only be done if you really need to, because it makes the code harder to maintain later on and the savings is really quite tiny.

If you want to improve your mod performance, you should use a profiler. If you're using VSCode there's a Factorio Debugger extension that comes with profiling, give that a look. It's worth noting that the debugger itself is a huge performance hog - I found this out when code that was taking several seconds to run (during map generation) suddenly completed faster than the eye could see when I ran it without the debugger. But anyway, the profiler will tell you how often your code is run and how long it takes. Look for things that are run often or take a long time and reduce them as much as you can.

Conventional wisdom is to avoid running code "on_tick". But if you limit the amount that you do - I believe I saw you mention only having a single "manager" entity per network - then you can get a surprising amount done in the tick without impacting performance. The game itself is extremely efficient so in general (ie. not in megabases) you will have almost 10ms to "play" with. That's an eternity in computer-time! So don't be afraid to do stuff and be creative. See: self-driving car mods, which have to update every vehicle every tick to keep things running smoothly!
Thank you very much for your expertise.
I will give the VS-Debugger a try. at the moment I actually use Notepad++, I know it's not a nice developement environment, but I have the code on a USB and edit it here and there.

I would actually like to see a setup with this in a Megabase, but I know, it's probably surreal, If I don't add any special features like power Belts (probably good Idea, you wouldn't need to place poles in the factory).

Post Reply

Return to β€œModding help”