Adding in New Weapons

Place to get help with not working mods / modding interface.
Infiniti
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 22, 2017 4:09 pm
Contact:

Adding in New Weapons

Post by Infiniti »

Hopefully someone here has dealt with this before! My friends and I didn't really like the current weapons in the game, so I decided to add an Assault Rifle and a Sniper Rifle. Everything is working 100% perfectly except for the research. Since they use bullets and are magazine-fed, I figured it would be nice if they just worked off of the bullet damage and speed technologies. Problem is, they use different ammo types in order to prevent players from loading the high-damage Sniper Rifle rounds into a very rapid firing SMG..
Anyone happen to know how I could add these new ammo types to the base game's bullet damage and speed technologies?
Oh! Also I would like to add a couple more steps of military technology research, that way I can add the Assault Rifle after the SMG and the Sniper Rifle after the Flamethrower. So I would need to edit the base game's research prerequisites as well..
I've been searching for an answer for hours, and it's getting really frustrating.. Any help is greatly appreciated, thanks!

I've tried adding technology.lua in prototypes>technology, and inside the file I have this code:

Code: Select all

data.raw["technology"]["bullet-damage-1"].effects =
    {
      {
        type = "ammo-damage",
        ammo_category = {"bullet", "rifle-bullet", "sniper-bullet"},
        modifier = "0.1"
      }
    }
Upon attempting to load the game, I am met with this error:

Image
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Adding in New Weapons

Post by Nexela »

For #1 just add your information to the existing technologies preferably in data-updates.lua. Doing it this way should mostly guarantee that your modifier gets added to mod added increased tech counts

Code: Select all

for i=1, 1000 do
  local tech = data.raw["technology"]["bullet-damage-"..i] --Local reference to the tech
  if tech then --If this tech exists add to it, otherwise break out of the loop
    tech.effects[#tech.effects+1] =   { --take the existing effects table and add a new table to it
          type = "ammo-damage",
          ammo_category = "my-ammo-category",
          modifier = "0.4"
        }
  else
    break --exit out of the loop on the first tech upgrade that doesn't exist (in vanilla .14 this will stop after bullet-damage-6)
  end
end
Last edited by Nexela on Wed Feb 22, 2017 5:31 pm, edited 2 times in total.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Adding in New Weapons

Post by Nexela »

Upon attempting to load the game, I am met with this error:
You need to insert your effect table into technology.effects see my example above
Infiniti
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 22, 2017 4:09 pm
Contact:

Re: Adding in New Weapons

Post by Infiniti »

Nexela wrote:For #1 just add your information to the existing technologies preferably in data-updates.lua. Doing it this way should mostly guarantee that your modifier gets added to mod added increased tech counts

Code: Select all

for i=1, 1000 do
  local tech = data.raw["technology"]["bullet-damage-"..i] --Local reference to the tech
  if tech then --If this tech exists add to it, otherwise break out of the loop
    tech.effects[#tech.effects+1] =   { --take the existing effects table and add a new table to it
          type = "ammo-damage",
          ammo_category = "my-ammo-category",
          modifier = "0.4"
        }
  else
    break --exit out of the loop on the first tech upgrade that doesn't exist (in vanilla .14 this will stop after bullet-damage-6)
end
Sorry to kind of needing to be um.. spoon fed answers.. I'm brand new to this! D:

So in my prototypes>technology folder in my mod I create a file called data-updates.lua, and then put this code into there?
Nexela wrote:
Upon attempting to load the game, I am met with this error:
You need to insert your effect table into technology.effects see my example above
And I'm really not quite sure what you mean by this xc

So sorry!
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Adding in New Weapons

Post by Nexela »

Oh! Also I would like to add a couple more steps of military technology research, that way I can add the Assault Rifle after the SMG and the Sniper Rifle after the Flamethrower. So I would need to edit the base game's research prerequisites as well..
Well this is easy, just copy the existing mil 3 tech and modify it

Code: Select all

data:extend{
{
    type = "technology",
    name = "military-4",
    icon = "__base__/graphics/technology/military.png",
    effects =
    {
      {
        type = "unlock-recipe",
        recipe = "my-super-rifle"
      }
    },
    prerequisites = {"military-3"},
    unit =
    {
      count = 50,
      ingredients =
      {
        {"science-pack-1", 1},
        {"science-pack-2", 2},
        {"science-pack-3", 1}
      },
      time = 30
    },
    order = "e-a-c"
  }}
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Adding in New Weapons

Post by Nexela »

Infiniti wrote: Sorry to kind of needing to be um.. spoon fed answers.. I'm brand new to this! D:
So in my prototypes>technology folder in my mod I create a file called data-updates.lua, and then put this code into there?
Create a new file in the same folder as data.lua called data-updates.lua data-updates.lua is automatically ran after every mod has done their data.lua stuff.
Infiniti wrote:And I'm really not quite sure what you mean by this xc
Technology effects is a table which takes tables of effect data as parameters.
tech.effects[#tech.effects+1] = {} --- is almost the same as saying table.insert(tech.effects, {})

Nice and quick LUA primer:
https://www.youtube.com/watch?v=S4eNl1rA1Ns
Infiniti
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 22, 2017 4:09 pm
Contact:

Re: Adding in New Weapons

Post by Infiniti »

I created data-updates.lua in the same folder as data.lua, and added this code:

Code: Select all

for i=1, 1000 do
  local tech = data.raw["technology"]["bullet-damage-"..i] --Local reference to the tech
  if tech then --If this tech exists add to it, otherwise break out of the loop
    tech.effects[#tech.effects+1] =   { --take the existing effects table and add a new table to it
          type = "ammo-damage",
          ammo_category = "rifle-bullet",
          modifier = "0.4"
        }
  else
    break --exit out of the loop on the first tech upgrade that doesn't exist (in vanilla .14 this will stop after bullet-damage-6)
end
This is the error I get upon loading the game now:

Image

Is it some kind of syntax or run-time error? I can't read LUA code at all.. Any language without semi-colons or brackets is very hard for me to visualize xc
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Adding in New Weapons

Post by Nexela »

Don't be afraid to read the error It is telling you that the "for" loop is missing an "end".

Which is saying oops, I forgot to add the end for the for loop, after the end for the if check. (updating post above)
Infiniti
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 22, 2017 4:09 pm
Contact:

Re: Adding in New Weapons

Post by Infiniti »

Nexela wrote:Don't be afraid to read the error It is telling you that the "for" loop is missing an "end".

Which is saying oops, I forgot to add the end for the for loop, after the end for the if check. (updating post above)
I tried changing the break to an end to see if that would work, but it didn't, but oh well, here is my code now:

Code: Select all

for i = 1, 1000 do
  local tech = data.raw["technology"]["bullet-damage-"..i] --Local reference to the tech
  if tech then --If this tech exists add to it, otherwise break out of the loop
    tech.effects[#tech.effects + 1] =   { --take the existing effects table and add a new table to it
          type = "ammo-damage",
          ammo_category = "rifle-bullet",
          modifier = "0.2"
        }
	tech.effects[#tech.effects + 1] =   { --take the existing effects table and add a new table to it
          type = "ammo-damage",
          ammo_category = "sniper-bullet",
          modifier = "0.2"
        }
  else
    break
  end	--exit out of the loop on the first tech upgrade that doesn't exist (in vanilla .14 this will stop after bullet-damage-6)
end
Game loads now but the magazines don't display an increased damage next to their damage (I have everything researched on this save).
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Adding in New Weapons

Post by Nexela »

This is where you will need a migration file.

create /migrations/migration_0.0.1.lua

and put this in it.

Code: Select all

for _, force in pairs(game.forces) do
  for i = 1, 1000 do
    local tech = force.technologies["bullet-damage-"..i] --Local reference to the tech
    if tech then
      if tech.researched then 
        force.set_ammo_modifier("rifle-bullet", force.get_ammo_modifier("rifle-bullet")+.02)
        force.set_ammo_modifier("sniper-bullet", force.get_ammo_modifier("sniper-bullet")+.02)
      end
    else
      break
    end
  end
end
Infiniti
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 22, 2017 4:09 pm
Contact:

Re: Adding in New Weapons

Post by Infiniti »

Nexela wrote:This is where you will need a migration file.

create /migrations/migration_0.0.1.lua

and put this in it.

Code: Select all

for _, force in pairs(game.forces) do
  for i = 1, 1000 do
    local tech = force.technologies["bullet-damage-"..i] --Local reference to the tech
    if tech then
      if tech.researched then 
        force.set_ammo_modifier("rifle-bullet", force.get_ammo_modifier("rifle-bullet")+.02)
        force.set_ammo_modifier("sniper-bullet", force.get_ammo_modifier("sniper-bullet")+.02)
      end
    else
      break
    end
  end
end
I'm sure it should work after that. Thank you so so so much!! I have a few afternoon classes to go to, so I won't really be able to test these changes for another few hours or so.

Thanks again!

Edit: Just got around to testing the code: set_ammo_modifier wasn't recognized by LuaForce, so I guessed and changed it to set_ammo_damage_modifier, and it worked! The numbers are a bit wonky so I'll need to use a round function I think. Also, 0.02 is 2%, not 20%! Now I just need to add checks to see what the research level is, and increment it based on that (the increase in damage and fire rate get higher as research level gets higher).
Post Reply

Return to “Modding help”