Page 1 of 1

Need some help with for a loop through ammo-turrets

Posted: Mon Sep 13, 2021 9:53 pm
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


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

Posted: Mon Sep 13, 2021 11:11 pm
by DaveMcW

Code: Select all

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

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

Posted: Mon Sep 13, 2021 11:33 pm
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.

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

Posted: Mon Sep 13, 2021 11:47 pm
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

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

Posted: Mon Sep 13, 2021 11:51 pm
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.

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

Posted: Tue Sep 14, 2021 11:47 am
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

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

Posted: Tue Sep 14, 2021 8:23 pm
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.