Page 1 of 1
[Solved] How do I make a structure do something at regular intervals?
Posted: Mon Apr 29, 2019 11:08 pm
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.
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 2:50 am
by mrudat
You could look at how Scanning Radar or Big Brother do things?
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 7:41 am
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.
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 8:54 am
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.
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 9:24 am
by Yemto
How do I look at vanilla entities? "data/core" and "data/base" doesn't have a radar script.
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 10:37 am
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.
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 11:23 am
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?
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 11:57 am
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, ...}
Re: How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 12:01 pm
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.
Re: [Solved] How do I make a structure do something at regular intervals?
Posted: Tue Apr 30, 2019 7:51 pm
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.