Need a way to pass data from data.lua to control.lua

Things that we aren't going to implement
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Need a way to pass data from data.lua to control.lua

Post by Xecutor »

I'm trying to calculate cost of items in raw materials.
But atm there is no legal way to do this!
I have all information required in data.lua, but I can't pass it to control.lua

I tried to create dummy technology with all data encoded in technology name, but I've got this error:
Error None: Error while loading technology prototype "testtech-...." (technology): bad lexical cast: source type value could not be interpreted as target
User avatar
cube
Former Staff
Former Staff
Posts: 1111
Joined: Tue Mar 05, 2013 8:14 pm
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by cube »

It should be possible to create a lua file that is required into both data.lua and control.lua
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by Xecutor »

data.raw is only available in data.lua
I want to analyze it and use result of analysis in control.lua
if script-output folder was in require search path, then yes, it would be possible to generate file in data.lua and require it in control.lua
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by Xecutor »

Now that I think about it, what I really need is ability to get ingredients and result(s) from receipts :)
And ability to get type of ingredient, to distinguish raw resource from craftable.
cartmen180
Filter Inserter
Filter Inserter
Posts: 358
Joined: Fri Jul 25, 2014 2:53 pm
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by cartmen180 »

Xecutor wrote:data.raw is only available in data.lua
no it is not. You can use it in many places.
Check out my mods
User avatar
cube
Former Staff
Former Staff
Posts: 1111
Joined: Tue Mar 05, 2013 8:14 pm
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by cube »

Wait, you mean data from all mods, or just from your mod?
For all mods, I don't think there is a way now (but I'm not 100% sure). For your mod, you can use the double require trick.
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by Xecutor »

For all mods. All available receipts.
cartmen180 wrote:no it is not. You can use it in many places.
This is kind of comment that is worse than no comment at all.
cartmen180
Filter Inserter
Filter Inserter
Posts: 358
Joined: Fri Jul 25, 2014 2:53 pm
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by cartmen180 »

Xecutor wrote:For all mods. All available receipts.
cartmen180 wrote:no it is not. You can use it in many places.
This is kind of comment that is worse than no comment at all.
i corrected your error so other people new to modding don't get confused ;)
data.raw can be used in almost any file you want. There is no rule saying you have to put it in the data.lua

your comment is one that sets back modding communities decades, if not centuries, because frankly I don't really feel like helping you out anymore.
Check out my mods
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by JamesOFarrell »

Unless I am wrong there seems to be no way to access data.raw from things linked by control.lua. So while you could have a file that is required by both data.lua and control.lua I doubt it would be able to pass data as data.lua (and the linked lua files) is run in the main factorio lua context and scripts are run in a different context. As you can't write to files from data.lua (it requires the game object) we need to look at other options.

game.itemprototypes and game.entityprototypes gives you access to some of the information in the prototypes but not the recipe information. game.getplayer.force gives you access to the recipes but only if they are enabled, not there actual ingredients. I think you were on the right track trying to pass information from data.lua to control.lua with a prototype definition but you were trying to use the wrong string. The order string is available on every item and can be accessed though game.itemprototypes["item-name"].order. So create a dummy prototype, set the order to be the information you need to pass and then retrieve that from inside control.lua when it is first loaded.

Edit: Realised this is the suggestions forum and no the help forum, sorry. I agree with this suggestion, an easy way to pass data or objects between the 2 instances would be really useful. That or exposing access to data.raw to the control scripts.
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by Xecutor »

cartmen180 wrote:
Xecutor wrote:For all mods. All available receipts.
cartmen180 wrote:no it is not. You can use it in many places.
This is kind of comment that is worse than no comment at all.
i corrected your error so other people new to modding don't get confused ;)
data.raw can be used in almost any file you want. There is no rule saying you have to put it in the data.lua

your comment is one that sets back modding communities decades, if not centuries, because frankly I don't really feel like helping you out anymore.
As far as I know (correct me if I'm wrong) there are only two 'entry point' scripts for each mod.
data.lua is executed during application startup and global table 'data' is available in it (and obviously in everything that data.lua 'require').
control.lua is executed on new game start and on game load. 'data' global is not available in it.

What are 'almost any file you want' files you are talking about if there are basically only two?
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by Xecutor »

JamesOFarrell wrote:The order string is available on every item and can be accessed though game.itemprototypes["item-name"].order.

Code: Select all

LuaItemPrototype doesn't have readable property order
Also, this prototype shouldn't appear anywhere. Not sure if 'enabled=false' will work for item prototypes.
JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by JamesOFarrell »

Ok, try this. It passes a couple of random number from data to control using the names of items. It is a pretty dodgy hack but it should work. About halfway though I realised I should use an entity instead of an item as they are easier to hide but this illustrates the idea with item names.

Just a note, as the seed is the same each time you launch factorio the random numbers are always the same, I tried to seed it with the os time but failed.
DataToControlTest_0.0.1.zip
v0.0.1
(1.5 KiB) Downloaded 218 times
User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by Xecutor »

If you read original post - I tried to do this with tech name. The game don't like really long tech names. I doubt it's different for items or entities.
DoneAndDead
Burner Inserter
Burner Inserter
Posts: 10
Joined: Tue Feb 10, 2015 5:31 pm
Contact:

Need a way to pass data from data.lua to control.lua

Post by DoneAndDead »

Hello dear modding community!

Im looking for away to pass data från data.lua to control.lua. I have readed the thread and come to the conclusion you need ugly/dodgy hacks, something i don't really see as good. My "solution" to this is just an abstract idea of how the implementation of how this should be done. I want to be able to add my own types to the data.raw, but form experimentation i notice that the games crasches and gives the error "No loader found for "name of new type".". If there is already a fix for this, feel free to tell me about it.
But my solution would be (This is in no way an order to the great gods Factorio, only a humble request from a burner inserter, and brainstorming):

First we have to add a new file to the workings, at the current moment from what i understand we go from data to control and data.raw is isolated in the first and in the last one glob (if this variable dosen't exist it might need to be created) is isolated. In the migration file (that mods can or cannot have) both should be accessible.

First we have to add some events to the event handling that falls under the migration category (that dosen't yet exist from what i have seen).
startMigration - called for all mods at the beginning of migration.
migrate - takes data of a type and converts it or just do something with it. When registered it should say what type it handles.
endMigration - called for all mods at the end of migration.

1. Data files are loaded and the data.raw is filled
2. Migration files are loaded and all events inserted.
3. All startMigration events are called, now changes can easily be done to the data.raw and modder don't need to worry about conflicts.
4. The game iterates over the data.raw table and finds a type that is missing a loader. It should be added to a stack and the iteration can continue. This makes it so that all core types are handled and loaded first.
5. The table with missing loaders for types are iterated and inserted into the event that is registered to handle them.
6. All endMigration events are called and the modder can do things with a sort(There are no players yet) of loaded Game.
7. The game goes over to controll.lua

This division helps to build a structure and to keep the control cleaner. You can probably just have the event registration (i have no clue, as for i have not coded the game) in control.lua, but form what i see it, that should only handle in game events. GUI editing stuff, like adding things to the map generation could be done in the migration file.

Regards DoneAndDead
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: Need a way to pass data from data.lua to control.lua

Post by vzybilly »

*pokes dead thread with stick*

any news on this? also, spreading the data out into multiple fields and entities would help with the issues of really long names... but I have an idea that needs the whole data.raw table in control... not nice to do via that hack...

... *continues google search before submitting*

I've found this thread that might help: https://forums.factorio.com/forum/vie ... f=25&t=741

anyways, good night, way past my sleep time...

... *tests it out real quickly*

seems like that way was removed, still looking for news.
Will code for Food. I also have 11+ mods!
Post Reply

Return to “Won't implement”