Need some help with for a loop through ammo-turrets

Place to get help with not working mods / modding interface.
Post Reply
atlas1205
Inserter
Inserter
Posts: 30
Joined: Tue Oct 11, 2016 2:18 am
Contact:

Need some help with for a loop through ammo-turrets

Post by atlas1205 »

Hi all,

I'm trying to write some code that makes all turrets that shoot bullets accept different ammo types from my mod.

I tried plain adding ammo_categories to the attack_parameters, and it worked fine. But when I used a for loop to iterate through all ammo turrets (mainly for bob's), it doesn't. They still only fire base game bullets. From what I can see bob's turrets aren't really defined differently from the base game's.

My code is the following. Any help would be appreciated. Yes I know this code isn't perfectly compatible with other mods yet, but I'll get to it after it starts working.

Code: Select all

local a = {"bullet"}
--add ammo types from my mod. this works.
for i, f in ipairs(ATF_calibers) do
table.insert(a,f)
end

--test code. this works fine on base game turret (it added all ammo types, base game bullets included)
--data.raw["ammo-turret"]["gun-turret"].attack_parameters.ammo_categories = a

--this is the for loop i use to add ammo_categories to all turrets. Doesn't work.
tur = data.raw["ammo-turret"]
for i, v in ipairs(tur) do
		tur.v.attack_parameters.ammo_categories = a
end


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

Re: Need some help with for a loop through ammo-turrets

Post by DaveMcW »

Code: Select all

tur = data.raw["ammo-turret"]
for i, v in ipairs(tur) do
	v.attack_parameters.ammo_categories = a
end

atlas1205
Inserter
Inserter
Posts: 30
Joined: Tue Oct 11, 2016 2:18 am
Contact:

Re: Need some help with for a loop through ammo-turrets

Post by atlas1205 »

DaveMcW wrote:
Mon Sep 13, 2021 11:11 pm

Code: Select all

tur = data.raw["ammo-turret"]
for i, v in ipairs(tur) do
	v.attack_parameters.ammo_categories = a
end
Thanks for the reply, but sadly that doesn't work. So far the only way I could get it to is hand-write code to include all turrets.

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

Re: Need some help with for a loop through ammo-turrets

Post by DaveMcW »

Sorry, there is another error.

Code: Select all

tur = data.raw["ammo-turret"]
for i, v in pairs(tur) do
	v.attack_parameters.ammo_categories = a
end

atlas1205
Inserter
Inserter
Posts: 30
Joined: Tue Oct 11, 2016 2:18 am
Contact:

Re: Need some help with for a loop through ammo-turrets

Post by atlas1205 »

DaveMcW wrote:
Mon Sep 13, 2021 11:47 pm
Sorry, there is another error.

Code: Select all

tur = data.raw["ammo-turret"]
for i, v in pairs(tur) do
	v.attack_parameters.ammo_categories = a
end
Oh GREAT, it worked! Turns out I can't use ipairs here. Thanks for the help!

It makes sense now. Pairs return key-value pairs and ipairs return index-value pairs. No wonder it worked when I did table.insert for the ammo types.

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

Re: Need some help with for a loop through ammo-turrets

Post by eradicator »

DaveMcW wrote:
Mon Sep 13, 2021 11:47 pm
Sorry, there is another error.

Code: Select all

tur = data.raw["ammo-turret"]
for i, v in pairs(tur) do
	v.attack_parameters.ammo_categories = a
end
You shouldn't use a global variable to store a temporary table reference that is only used once.

Either

Code: Select all

local tur = data.raw["ammo-turret"]
for i, v in pairs(tur) do
	v.attack_parameters.ammo_categories = a
end
Or:

Code: Select all

for i, v in pairs(data.raw["ammo-turret"]) do
	v.attack_parameters.ammo_categories = a
end
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.

atlas1205
Inserter
Inserter
Posts: 30
Joined: Tue Oct 11, 2016 2:18 am
Contact:

Re: Need some help with for a loop through ammo-turrets

Post by atlas1205 »

eradicator wrote:
Tue Sep 14, 2021 11:47 am
DaveMcW wrote:
Mon Sep 13, 2021 11:47 pm
Sorry, there is another error.

Code: Select all

tur = data.raw["ammo-turret"]
for i, v in pairs(tur) do
	v.attack_parameters.ammo_categories = a
end
You shouldn't use a global variable to store a temporary table reference that is only used once.

Either

Code: Select all

local tur = data.raw["ammo-turret"]
for i, v in pairs(tur) do
	v.attack_parameters.ammo_categories = a
end
Or:

Code: Select all

for i, v in pairs(data.raw["ammo-turret"]) do
	v.attack_parameters.ammo_categories = a
end
Yes I'm aware. And it was fixed. Thanks for mentioning it, anyway.

Post Reply

Return to “Modding help”