Page 1 of 1

Need help with laser gun

Posted: Sat May 02, 2020 1:10 pm
by RedLegion
Hi. I'll try make laser weapon mod, which shoot beam projectile. Can anyone help with LUA?

Code: Select all

{
    type = "ammo",
    name = "laser_ammo",
    icon = "__laser_weapon__/graphics/icons/laser_ammo.png",
    icon_size = 64, icon_mipmaps = 4,
    flags = {},
    ammo_type =
    {
      category = "laser-battery",
      action =
      {
        type = "direct",
        action_delivery =
        {
          type = "beam",
          beam = "laser-beam",
          max_length = 15,
          duration = 40,
          source_offset = {0, -1.31439 }
          target_effects =
          {
            {
              type = "damage",
              damage = { amount = 10 , type = "laser"}
            }
          },
        },
      }
    },
    magazine_size = 20,
    subgroup = "ammo",
    order = "a[basic-clips]-b[laser_ammo]",
    stack_size = 100
  },
Game was crash at this code part.

Re: Need help with laser gun

Posted: Sat May 02, 2020 7:22 pm
by Hiladdar
I looked at your code, and it looks like what you are trying to do is take the existing pistol, automatic rifle, turn them into laser pistol and laser rifle with a new type of ammo, an expandable chemical battery.

With in the base game, programming examples for laser weapon entity definition is in ../data/base/prototype/entity/turrets.lua. The just do a search for laser_turret. The code for laser turrets starts with:

Code: Select all

  {
    type = "electric-turret",
    name = "laser-turret",
and ends with:

Code: Select all

    call_for_help_radius = 40
  },
Another chunk of code that it would make sense for you to look at is how the laser for personal armor works. Both of those chunks of code are great to study for how the game handles lasers. The file ../data/base/prototype/entity/beams.lua contains code for some of the display of beams to support laser fire.

I looked at your code, and it looks like what you are trying to do is take the existing pistol and automatic rifle, turn them into laser pistol and laser rifle with a new type of ammo, an expandable battery to power the laser pistol / rifle.

The crash message usually provides several clues as where it crashed i.e. what line, and what the cause was. There is not enough information provided as to why or what caused the crash. One debugging technique I use, is commenting code out and dealing with only one entity definition at a time.

Depending on the scope of change, and for future code maintainability consider using deepcopy when possible, so if the something in the base game gets changed that change is propagated to your module.

Consider downloading some modules that others have written something similar to what you are attempting to do to see how they programmed it, so that you can learn from them. In addition to going to the published API, doing self tutorials, that is one of the techniques I use to learn to code new programming languages. There are six mods for Factorio 0.18.xx from which you might be able use to figure out how to implement what you are intend on programming, they are:
  • WeaponPack
  • Laser Rifle
  • Desert Eagle
  • Assault Rifle
  • Sniper Rifle
  • Electric Weapons
Hiladdar

Re: Need help with laser gun

Posted: Sat May 02, 2020 9:12 pm
by darkfrei
See the code of hidden vanilla railgun:
code from: data\base\prototypes\item\item.lua

Code: Select all

{
    type = "gun",
    name = "railgun",
    icon = "__base__/graphics/icons/railgun.png",
    icon_size = 64, icon_mipmaps = 4,
    flags = {"hidden"},
    subgroup = "gun",
    order = "c[railgun]",
    attack_parameters =
    {
      type = "projectile",
      ammo_category = "railgun",
      cooldown = 3 * 60,
      movement_slow_down_factor = 0.6,
      projectile_creation_distance = 0.6,
      range = 20,
      sound =
      {
        {
          filename = "__base__/sound/railgun.ogg",
          volume = 0.5
        }
      }
    },
    stack_size = 5
  }
Than ammo_category = "railgun", we are need the ammo for it:
code from: data\base\prototypes\item\ammo.lua

Code: Select all

{
    type = "ammo",
    name = "railgun-dart",
    icon = "__base__/graphics/icons/railgun-ammo.png",
    icon_size = 64, icon_mipmaps = 4,
    flags = {"hidden"},
    ammo_type =
    {
      category = "railgun",
      target_type = "direction",
      clamp_position = true,
      action =
      {
        type = "line",
        range = 25,
        width = 0.5,

        source_effects =
        {
          type = "create-explosion",
          entity_name = "railgun-beam"
        },
        action_delivery =
        {
          type = "instant",
          target_effects =
          {
            type = "damage",
            damage = { amount = 100, type="physical"}
          }
        }
      }
    },
    magazine_size = 4,
    subgroup = "ammo",
    order = "c[railgun]",
    stack_size = 200
  }
The "railgun-beam" is an "explosion",
the code is in the: data\base\prototypes\entity\explosions.lua

Code: Select all

{
    type = "explosion",
    name = "railgun-beam",
    flags = {"not-on-map"},
    subgroup = "explosions",
    rotate = true,
    beam = true,
    animations = explosion_animations.railgun(),
    light = {intensity = 1, size = 10, color = {r = 1.0, g = 1.0, b = 1.0}},
    smoke = "smoke-fast",
    smoke_count = 2,
    smoke_slow_down_factor = 1
  }
You can test vanilla "laser" gun just with this console command:

Code: Select all

/c game.player.insert{name="railgun", count=1} game.player.insert{name="railgun-dart", count=200}