how to spawn particle effect?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

how to spawn particle effect?

Post by bongobeat »

here I am with another silly question :lol:

how can be a particle (like blob) spawned after shooting with a gun (defined like a firearms)?

while defining a sci fi gun I use that spawning command: (it's log1 script)
SpoilerShow

Code: Select all

			onAttack = function(self)
			spawn("ion_object", self.level, self.x, self.y)
			end,
but it spawn the object in front of the party, which is going directly in the party while it should go in the other way
SpoilerShow

Code: Select all

-- ion gun
defineObject{
	name = "ion_gun",
	baseObject = "base_item",
	components = {
		{
			class = "Model",
	model = "mod_assets/sci_fi_gun/ion_gun.fbx",
		},
		{
			class = "Item",
	uiName = "Ion Canon",
	description = "A powerful weapon made with old technology, it fires high energetic particles.",
	gfxAtlas = "mod_assets/sci_fi_gun/oliveitem.tga",
	gfxIndex = 30,
--			gfxIndexPowerAttack = 432,
			impactSound = "impact_blade",
			weight = 3.0,
			secondaryAction = "ions_rapid",
		},
		{
			class = "FirearmAttack",
			attackPower = 500,
			cooldown = 0.4,
			attackSound = "laser01",
			ammo = "power",
			jamChance = 0,
			backfireChance = 0,
			requirements = { "firearms", 5 },
			onAttack = function(self)
			spawn("ion_object", self.level, self.x, self.y)
			end,
		},
		{
			class = "FirearmAttack",
			name = "ions_rapid",
			uiName = "Rapid Fire",
			gameEffect = "Quickly Fires 5 Ions Shots.",
			energyCost = 50,
			attackPower = 500,
			cooldown = 3,
			attackSound = "laser01",
			ammo = "power",
			repeatCount = 5,
			repeatDelay = 0.10,
			jamChance = 0,
			backfireChance = 0,
			requirements = { "firearms", 5 },
		},
	},
	tags = { "weapon" },
}
the ion_object:
SpoilerShow

Code: Select all

defineObject{
	name = "ion_object",
	baseObject = "base_spell",
	components = {
		{
			class = "Particle",
			particleSystem = "ions",
		},
		{
			class = "Light",
			color = vec(1.0, 1.0, 0.0),
			brightness = 100,
			range = 10,
			fadeOut = 0.5,
			disableSelf = true,
		},
		{
			class = "Projectile",
			spawnOffsetY = 1.35,
			velocity = 20,
			radius = 0.1,
			hitEffect = "ions_blast",
		},
		{
			class = "Sound",
			sound = "laser01",
		},
		{
			class = "Sound",
			name = "launchSound",
			sound = "laser01",
		},
	},
}
another question:
Is it possible to increase the range of any firearm to 15 or more square?
while testing, without any firearms level required, It shoot only on 3 squares max in front of the party.
I had to set the requirements = { "firearms", 5 }, so the range is naturally increased, but is there any way to do this without defining it like so?

finaly:
how can be the particle firearm_hit changed by another particle?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Drakkan
Posts: 1318
Joined: Mon Dec 31, 2012 12:25 am

Re: how to spawn particle effect?

Post by Drakkan »

I saw similar topic about spawning spells here, maybe it helps you a little
viewtopic.php?f=22&t=9416&hilit=spawn+fireball&start=10
Breath from the unpromising waters.
Eye of the Atlantis
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: how to spawn particle effect?

Post by minmay »

bongobeat wrote:how can be a particle (like blob) spawned after shooting with a gun (defined like a firearms)?

while defining a sci fi gun I use that spawning command: (it's log1 script)
The "self" parameter in onAttack is the component. Components do not have level, x, or y fields. So your onAttack hook is passing nil values, which is the same as simply calling

Code: Select all

spawn("ion_object")
which will spawn the item one square in front of the party and facing the party.
Here is the code you should use:

Code: Select all

onAttack = function(self, champion, slot, chainIndex)
  local ion = spawn("ion_object",party.level,party.x,party.y,party.facing,party.elevation)
  ion.projectile:setIgnoreEntity(party)

  -- Set cast by champion so that experience is awarded for kills.
  local ord = champion:getOrdinal()
  ion.projectile:setCastByChampion(true)
  -- When the 2.1.19 patch is released, uncomment the next line and delete the previous line.
  -- ion.projectile:setCastByChampion(ord)

  -- Simulate the position of a player-cast spell.
  local left = nil
  for i = 1,4 do
    if party.party:getChampion(i):getOrdinal() == ord then
      left = i == 1 or i == 3
      break
    end
  end
  local wpos = party:getWorldPosition()
  local dx = nil
  local dz = nil
  if party.facing == 0 then
    dx = left and -0.1 or 0.1
    dz = -1
  elseif party.facing == 1 then
    dz = left and 0.1 or -0.1
    dx = -1
  elseif party.facing == 2 then
    dx = left and 0.1 or -0.1
    dz = 1
  else -- party.facing == 3
    dz = left and -0.1 or 0.1
    dx = 1
  end

  ion:setWorldPosition(vec(wpos[1]+dx,wpos[2]+1.35,wpos[3]+dz))
end,
bongobeat wrote:another question:
Is it possible to increase the range of any firearm to 15 or more square?
while testing, without any firearms level required, It shoot only on 3 squares max in front of the party.
I had to set the requirements = { "firearms", 5 }, so the range is naturally increased, but is there any way to do this without defining it like so?
"range" field in the FirearmAttackComponent definition.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: how to spawn particle effect?

Post by bongobeat »

@Drakkan:
sorry I did not find this topic, I got another one with custom spell, but you need to use a script in the editor. I already use that for a custom spell, but I got an error while using it with the ion object.

@minmay:
this is perfect! Great! thanks! :D

just tested it:
the ion object is spawned even if there is no ammo in the hand. any help?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Eburt
Posts: 69
Joined: Thu Feb 05, 2015 5:44 am

Re: how to spawn particle effect?

Post by Eburt »

Take a look at this thread. About 3/4 of the way down the OP is a topic on having two specific weapons equipped together. Use that to check if both the weapon and your ammo are both in the hands. The FirearmAttackComponent should take care of decrementing the ammo on use.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: how to spawn particle effect?

Post by bongobeat »

thanks,

akroma use this:
SpoilerShow

Code: Select all

          function comboSwordCheckEquip(self, champion, slot)
             local sword1 = champion:getItem(1)
             local sword2 = champion:getItem(2)
             if sword1 and sword2 then
                if sword1.go.name == "long_sword" and sword2.go.name == "rapier"
                or sword2.go.name == "long_sword" and sword1.go.name == "rapier" then
                   hudPrint("this works")
                end
             end
          end
          
          
          function comboSwordCheckUnequip(self, champion, slot)
             local sword1 = champion:getItem(1)
             local sword2 = champion:getItem(2)
             if sword1 and sword2 then
                if sword1.go.name == "long_sword" and sword2.go.name == "rapier"
                or sword2.go.name == "long_sword" and sword1.go.name == "rapier" then
                   hudPrint("this works")
                end
             end
          end
          
How can it be integrated in the ion script?

I can't load the editor with this one: (modified akroma script is added between the quadruple quote)
SpoilerShow

Code: Select all

		{
			class = "FirearmAttack",
			attackPower = 500,
			cooldown = 0.4,
			attackSound = "laser01",
			ammo = "power",
			jamChance = 0,
			backfireChance = 0,
			range = 15,
			onAttack = function(self, champion, slot, chainIndex)
----
             local ammo1 = champion:getItem(1)
             local ammo2 = champion:getItem(2)
                if ammo1.go.name == "ion_gun" and ammo2.go.name == "power_recharge"
                or ammo2.go.name == "ion_gun" and ammo1.go.name == "power_recharge" then
----
      local ion = spawn("ion_object",party.level,party.x,party.y,party.facing,party.elevation)
      ion.projectile:setIgnoreEntity(party)

      -- Set cast by champion so that experience is awarded for kills.
      local ord = champion:getOrdinal()
      ion.projectile:setCastByChampion(true)
      -- When the 2.1.19 patch is released, uncomment the next line and delete the previous line.
      -- ion.projectile:setCastByChampion(ord)

      -- Simulate the position of a player-cast spell.
      local left = nil
      for i = 1,4 do
        if party.party:getChampion(i):getOrdinal() == ord then
          left = i == 1 or i == 3
          break
        end
      end
      local wpos = party:getWorldPosition()
      local dx = nil
      local dz = nil
      if party.facing == 0 then
        dx = left and -0.1 or 0.1
        dz = -1
      elseif party.facing == 1 then
        dz = left and 0.1 or -0.1
        dx = -1
      elseif party.facing == 2 then
        dx = left and 0.1 or -0.1
        dz = 1
      else -- party.facing == 3
        dz = left and -0.1 or 0.1
        dx = 1
      end

      ion:setWorldPosition(vec(wpos[1]+dx,wpos[2]+1.35,wpos[3]+dz))
    end,
----
                end,
             end,
          end
----
		},
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Eburt
Posts: 69
Joined: Thu Feb 05, 2015 5:44 am

Re: how to spawn particle effect?

Post by Eburt »

At a glance, it looks like that should work. It's hard to tell because the code isn't formatted very clearly, but if I had to guess I would say you have too many or not enough "end" statements. I would strongly recommend indenting everything cleanly so that you can tell where functions open/close.

Edit: take a look at this, after formatting, it is very obvious where the extra "end"s are:
SpoilerShow

Code: Select all

{
class = "FirearmAttack",
attackPower = 500,
cooldown = 0.4,
attackSound = "laser01",
ammo = "power",
jamChance = 0,
backfireChance = 0,
range = 15,
onAttack = function(self, champion, slot, chainIndex)
	----
	local ammo1 = champion:getItem(1)
	local ammo2 = champion:getItem(2)
	if ammo1.go.name == "ion_gun" and ammo2.go.name == "power_recharge"
	or ammo2.go.name == "ion_gun" and ammo1.go.name == "power_recharge" then
		----
		local ion = spawn("ion_object",party.level,party.x,party.y,party.facing,party.elevation)
		ion.projectile:setIgnoreEntity(party)

		-- Set cast by champion so that experience is awarded for kills.
		local ord = champion:getOrdinal()
		ion.projectile:setCastByChampion(true)
		-- When the 2.1.19 patch is released, uncomment the next line and delete the previous line.
		-- ion.projectile:setCastByChampion(ord)

		-- Simulate the position of a player-cast spell.
		local left = nil
		for i = 1,4 do
			if party.party:getChampion(i):getOrdinal() == ord then
				left = i == 1 or i == 3
				break
			end
		end
		local wpos = party:getWorldPosition()
		local dx = nil
		local dz = nil
		if party.facing == 0 then
			dx = left and -0.1 or 0.1
			dz = -1
		elseif party.facing == 1 then
			dz = left and 0.1 or -0.1
			dx = -1
		elseif party.facing == 2 then
			dx = left and 0.1 or -0.1
			dz = 1
		else -- party.facing == 3
			dz = left and -0.1 or 0.1
			dx = 1
		end

		ion:setWorldPosition(vec(wpos[1]+dx,wpos[2]+1.35,wpos[3]+dz))
		end,  <---EXTRA END
		----
		end,  <---EXTRA END
	end,
end
----
},
On a side note, the above code will crash the game if the champion is not holding an item in both hands because ammo1 or ammo2 will be nil and nil doesn't have a .go component. You should wrap that whole block in:
if ammo1 and ammo 2 then
...
end
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: how to spawn particle effect?

Post by bongobeat »

humm this don't work without the 2 extra ends, I got "item.lua:73: unexpected symbol near ',' :cry:
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Eburt
Posts: 69
Joined: Thu Feb 05, 2015 5:44 am

Re: how to spawn particle effect?

Post by Eburt »

bongobeat wrote:humm this don't work without the 2 extra ends, I got "item.lua:73: unexpected symbol near ',' :cry:
Probably because the last end that is there doesn't have a comma after it. If there's anything following the onAttack hook and no comma after the end closing it you will get that error. However, you can check by finding line 73 - thats what the error message is telling you (sometimes it will be on the next line with code after the number in the error as well).
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: how to spawn particle effect?

Post by minmay »

Fields in tables are separated by commas. You have several commas in the middle of your function code, which doesn't make any sense, and no comma after the onAttack field assignment. You might find the Lua Reference Manual useful; it specifies the syntax of the language so that you will not have this problem again.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Post Reply