Experienced C++ programmer new to LUA

Place to get help with not working mods / modding interface.
Yulgalminakf
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Jun 10, 2015 12:27 am
Contact:

Experienced C++ programmer new to LUA

Post by Yulgalminakf »

I'm trying to obtain access to a variable in an entity prototype.

Code: Select all

for i = 1, game.entityprototypes["item-name"].filter_count do
Comes up with an error saying that item prototypes doesn't contain the key "filter_count". However, the item I'm trying to access does have a filter_count, it's a smart inserter.

My experience with C++ tells me that the table entityprototypes is storing the abstract class of entityprototype, and smart inserter, being a child of that class has more variables. I was hoping that lua would simply be smart enough to typecast the variable for me, but it doesn't. I've looked high and low online for a way to typecast in lua and everyone says it's not possible and not needed. However, I realize that lua is in fact NOT C++, and data is stored differently. So this is me asking a simple question of how to access a child's variables when dealing with the abstracted parent's class in lua.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3733
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Experienced C++ programmer new to LUA

Post by DaveMcW »

The real parent class (the one that loads entities.lua) is written in C++, and is inaccessible by the Lua API.

EntityPrototype is generated for Lua scripts to use, and only contains the keys the developers choose to expose. Unfortunately, filter_count is not one of them.

You can request new keys here.
Yulgalminakf
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Jun 10, 2015 12:27 am
Contact:

Re: Experienced C++ programmer new to LUA

Post by Yulgalminakf »

Really? Every new variable added in lua has to be added in C++ as well? Not only that, it has to be allowed to access it? That's kinda lame and doesn't allow for much creativity in the creation of mods if only the very basic properties are allowed to be accessed and changed.
Even for alpha, that kinda sucks, unless the developers decide to do a massive overhaul of how the modding api works.

*sigh*

Oh well, thanks for the answer.

P.S. I'm betting that adding your own keys is going to be impossible for now...

P.P.S. DaveMcW has 666 posts up at the time of this post xD
Rseding91
Factorio Staff
Factorio Staff
Posts: 14914
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Experienced C++ programmer new to LUA

Post by Rseding91 »

Yulgalminakf wrote:Really? Every new variable added in lua has to be added in C++ as well? Not only that, it has to be allowed to access it? That's kinda lame and doesn't allow for much creativity in the creation of mods if only the very basic properties are allowed to be accessed and changed.
Even for alpha, that kinda sucks, unless the developers decide to do a massive overhaul of how the modding api works.

*sigh*

Oh well, thanks for the answer.

P.S. I'm betting that adding your own keys is going to be impossible for now...

P.P.S. DaveMcW has 666 posts up at the time of this post xD
That is indeed how the mod API works. As a C++ programmer you should understand that a hierarchy defined entity structure that's compiled just doesn't support arbitrary values without a huge performance hit of having to iterate the values list every time you'd read/write them.

If you're trying to access the filters on a smart inserter runtime that is currently possible using this: https://forums.factorio.com/wiki/inde ... #setfilter

If you're looking to change the number of filters on a smart inserter you can do that in the prototype during the data loading startup phase.
If you want to get ahold of me I'm almost always on Discord.
Yulgalminakf
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Jun 10, 2015 12:27 am
Contact:

Re: Experienced C++ programmer new to LUA

Post by Yulgalminakf »

It'd only really take a performance hit at load/compile time if done properly.

And no, I'm trying to access the number of filters, not set them. I'm attempting to loop through all of the filters in the inserter and add them to the inventory it's pointed at (trying to make a creative chest, based off of the mod called creative mode). For now, I can simply hardcode the value. I was just hoping that I wouldn't have to do that cuz it's messy.
jorgenRe
Filter Inserter
Filter Inserter
Posts: 535
Joined: Wed Apr 09, 2014 3:32 pm
Contact:

Re: Experienced C++ programmer new to LUA

Post by jorgenRe »

Yulgalminakf wrote:It'd only really take a performance hit at load/compile time if done properly.

And no, I'm trying to access the number of filters, not set them. I'm attempting to loop through all of the filters in the inserter and add them to the inventory it's pointed at (trying to make a creative chest, based off of the mod called creative mode). For now, I can simply hardcode the value. I was just hoping that I wouldn't have to do that cuz it's messy.
You can just do this:

Code: Select all

/c game.players[1].selected.insert({name = "iron-ore", count = 300})
And changing the game.players[1].selected with an entity of your choice.
This will basically insert 300 iron ore when it's called.
Now if you put in some more effort into it and use this code to always keep iron ore 95 in the chest:
Whereas e = the chosen entity.

Code: Select all

local inv = e.getinventory(1)
for itemname, count in pairs(inv.getcontents()) do
 if itemname == "iron-ore" then
   ironfound = 1
   if count < 95 then 
    moving = {name = itemname, count = 95-count}
    e.insert(moving)
  end
 end
end
if ironfound == 0 then
 e.insert({name = "iron-ore", count = 100})
end
But yea as you say it would require to be hardcoded each item :(
Edit:
Though it could probably be modularised so that each item that is to be in the chest is put into a table so that only 1 word and a number is neccecary to keep the chest filled with the items.
Logo
Noticed the told change in FFF #111 so il continue to use my signature ^_^
Thanks for listening to our suggestions, devs :D!
I would jump of joy if we could specify which tiles spawned in a surfaces
Post Reply

Return to “Modding help”