Page 1 of 1
Experienced C++ programmer new to LUA
Posted: Wed Jun 10, 2015 12:37 am
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.
Re: Experienced C++ programmer new to LUA
Posted: Wed Jun 10, 2015 1:50 am
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.
Re: Experienced C++ programmer new to LUA
Posted: Wed Jun 10, 2015 3:09 am
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
Re: Experienced C++ programmer new to LUA
Posted: Wed Jun 10, 2015 3:02 pm
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.
Re: Experienced C++ programmer new to LUA
Posted: Fri Jun 12, 2015 3:40 am
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.
Re: Experienced C++ programmer new to LUA
Posted: Sat Jun 13, 2015 10:07 am
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
![Sad :(](./images/smilies/icon_e_sad.gif)
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.