need help with for .... in pairs

Place to get help with not working mods / modding interface.
Post Reply
Ketlambek
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Jun 12, 2021 11:51 pm
Contact:

need help with for .... in pairs

Post by Ketlambek »

Hi Guys! Sorry, I'm bad in English.

I'm trying to change the stack size for some items. I have this code:

Code: Select all

--data-final-fixes.lua
local items = data.raw.item

function get_stack_size(str)
	-- if the specified name has such a substring, then return the specified stack size
	if str:find("transport-belt") ~= nil then return settings.startup["Ket-transport-belt-stack-size"].value end
	return nil
end

local size = nil
for i, e in pairs(items) do
	size = get_stack_size(e.name)
	if size ~= nil then
		e.stack_size = size
		size = nil
	end
end
but nothing change... It seems to me that in "e" is not an item structure and in "i" is not an index of items array. Or is the problem something else?

UPD: I understand that problem is not with "e" and "i". If I use item internal name in condition operator then stack size is already changes. Why it doesn't works with finding a substring?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: need help with for .... in pairs

Post by DaveMcW »

https://www.lua.org/manual/5.2/manual.html#6.4.1 wrote:The following combinations are allowed in describing a character class:
x: where x is not one of the magic characters ^$()%.[]*+-?
%x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
To search for - in a string, you need to escape it.

Code: Select all

str:find("transport%-belt")

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: need help with for .... in pairs

Post by eradicator »

Or use plaintext find mode.

Code: Select all

str:find("transport-belt", 1, true)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Ketlambek
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Jun 12, 2021 11:51 pm
Contact:

Re: need help with for .... in pairs

Post by Ketlambek »

thanks everyone for solutions! I use eradicator's method before reading his reply.

Mernom
Fast Inserter
Fast Inserter
Posts: 122
Joined: Sun Jul 15, 2018 10:05 pm
Contact:

Re: need help with for .... in pairs

Post by Mernom »

You could also iterate over all transport-belt type entities and find their item-to-place-this, no?

Ketlambek
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Jun 12, 2021 11:51 pm
Contact:

Re: need help with for .... in pairs

Post by Ketlambek »

Then I did so, and then I abandoned the mod...

Post Reply

Return to “Modding help”