[Solved] How do I make a structure do something at regular intervals?

Place to get help with not working mods / modding interface.
Post Reply
Yemto
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Aug 28, 2016 1:52 pm
Contact:

[Solved] How do I make a structure do something at regular intervals?

Post by Yemto »

So I have a structure which is supposed to do something every X amount of time/ticks, similar to how radars works (but faster). since this is the first time I worked with anything like this, I have no idea how to do it.

Edit: I figured out how to use on_tick and find_entities_filtered to do what I want, so unless there is a better way, that's what I'm using.
Last edited by Yemto on Tue Apr 30, 2019 6:41 pm, edited 1 time in total.

User avatar
mrudat
Fast Inserter
Fast Inserter
Posts: 229
Joined: Fri Feb 16, 2018 5:21 am
Contact:

Re: How do I make a structure do something at regular intervals?

Post by mrudat »

You could look at how Scanning Radar or Big Brother do things?

Yemto
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Aug 28, 2016 1:52 pm
Contact:

Re: How do I make a structure do something at regular intervals?

Post by Yemto »

I just looked at Bob's warfare and Big Brother, and both mods seem to just use/modify the vanilla functionality of the radar, which I can't use since my structure I'm making is not going to the same thing as a radar.

User avatar
mrudat
Fast Inserter
Fast Inserter
Posts: 229
Joined: Fri Feb 16, 2018 5:21 am
Contact:

Re: How do I make a structure do something at regular intervals?

Post by mrudat »

That's why I suggested Scanning Radar as well; it provides a 'radar', that does all of the work via scripting in order to scan a circular area, rather than a square area.

Yemto
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Aug 28, 2016 1:52 pm
Contact:

Re: How do I make a structure do something at regular intervals?

Post by Yemto »

How do I look at vanilla entities? "data/core" and "data/base" doesn't have a radar script.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: How do I make a structure do something at regular intervals?

Post by eduran »

The best way is to find a vanilla entity that can be modified to do what you need. If that is not possible, you will have to write a script and run it in on_tick or on_nth_tick. In that case, make sure to keep what you are doing to a minimum. Calling find_entities_filtered on every tick will quickly get you into UPS-drop territory. A better approach is to keep a list of your custom entities and use that instead.
Yemto wrote:
Tue Apr 30, 2019 9:24 am
How do I look at vanilla entities? "data/core" and "data/base" doesn't have a radar script.
You can't. Vanilla entity behavior is not written in Lua.

Yemto
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Aug 28, 2016 1:52 pm
Contact:

Re: How do I make a structure do something at regular intervals?

Post by Yemto »

eduran wrote:
Tue Apr 30, 2019 10:37 am
The best way is to find a vanilla entity that can be modified to do what you need. If that is not possible, you will have to write a script and run it in on_tick or on_nth_tick. In that case, make sure to keep what you are doing to a minimum. Calling find_entities_filtered on every tick will quickly get you into UPS-drop territory. A better approach is to keep a list of your custom entities and use that instead.

Ok, thanks. That's what I have been doing so far. While I'm new to Factorio modding, but I have been programming for quite a while I know to avoid things like that.

Edit: Just a quick question, can I store custom variables in entities? or do I have to set up my own tables for that?

User avatar
AmatorPhasma
Fast Inserter
Fast Inserter
Posts: 126
Joined: Sat Aug 05, 2017 8:20 pm
Contact:

Re: How do I make a structure do something at regular intervals?

Post by AmatorPhasma »

Yemto wrote:
Tue Apr 30, 2019 11:23 am
eduran wrote:
Tue Apr 30, 2019 10:37 am
The best way is to find a vanilla entity that can be modified to do what you need. If that is not possible, you will have to write a script and run it in on_tick or on_nth_tick. In that case, make sure to keep what you are doing to a minimum. Calling find_entities_filtered on every tick will quickly get you into UPS-drop territory. A better approach is to keep a list of your custom entities and use that instead.

Ok, thanks. That's what I have been doing so far. While I'm new to Factorio modding, but I have been programming for quite a while I know to avoid things like that.

Edit: Just a quick question, can I store custom variables in entities? or do I have to set up my own tables for that?
No not possible for the entity or the prototype, you have to setup your own global.table,
somthing like:

Code: Select all

global.my_table[entity.unit_number] = somthing or {somthing=somthing, ...}

Yemto
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Aug 28, 2016 1:52 pm
Contact:

Re: How do I make a structure do something at regular intervals?

Post by Yemto »

AmatorPhasma wrote:
Tue Apr 30, 2019 11:57 am
Yemto wrote:
Tue Apr 30, 2019 11:23 am
eduran wrote:
Tue Apr 30, 2019 10:37 am
The best way is to find a vanilla entity that can be modified to do what you need. If that is not possible, you will have to write a script and run it in on_tick or on_nth_tick. In that case, make sure to keep what you are doing to a minimum. Calling find_entities_filtered on every tick will quickly get you into UPS-drop territory. A better approach is to keep a list of your custom entities and use that instead.

Ok, thanks. That's what I have been doing so far. While I'm new to Factorio modding, but I have been programming for quite a while I know to avoid things like that.

Edit: Just a quick question, can I store custom variables in entities? or do I have to set up my own tables for that?
No not possible for the entity or the prototype, you have to setup your own global.table,
somthing like:

Code: Select all

global.my_table[entity.unit_number] = somthing or {somthing=somthing, ...}
Ok, thanks so much.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: [Solved] How do I make a structure do something at regular intervals?

Post by eduran »

AmatorPhasma wrote:
Tue Apr 30, 2019 11:57 am

Code: Select all

global.my_table[entity.unit_number] = somthing or {somthing=somthing, ...}
Two remarks to help with this approach:
  • There is no way to get an entity from its unit_number. If you need to access the actual entity in your script, you should store it:

    Code: Select all

    global.my_table[entity.unit_number] = {entity = entity, userdata = my_data, ...}
  • You should subscribe to on_robot_mined_entity, on_pre_player_mined_item and on_entity_died events to remove invalid entities from your table.

Post Reply

Return to “Modding help”