Knock Back 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!
Post Reply
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Knock Back Effect

Post by Mysterious »

Hello all.

I know that the hand cannon knocks the party back one square (knockback = true,). What I am trying to do with the (below object) is when it's destroyed the party is knocked back. I really don't have a clue how to do this and was wondering if someone could help me with the code please.

Thank you foe the help on this.

Code: Select all

defineObject {
   name = "barrel_block_exploding",
   baseObject = "my_barrel_crate_block",
   tags = { "my_stuff_break" },
   components = {
	{
		class = "Health",
		health = 30,

		spawnOnDeath = "barrel_crate_block_broken",
		onDie = function(c)
		hudPrint("Well that was a dumb idea destroying the barrel.")
            local i, j;
            for i = -1, 1 do
               for j = -1, 1 do
                  if i ~= 0 or j ~= 0 then
                     spawn("fireball_large", c.go.level, c.go.x + i, c.go.y + j, 0, c.go.elevation)
                  end
               end
            end
         end
      }
	  }
	  }
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Knock Back Effect

Post by minmay »

This isn't C, there's no need to declare i and j at the top - IIRC the for loops will actually just shadow them if you do that.
I would expect that you want to knock back monsters too. Here is an onDie hook that will knock back the party and any monsters in the 4 adjacent squares. (If one of the 4 adjacent squares contains a ScriptComponent that contains a function named "knockback", that function will also be called, but you probably don't care about that.)

Code: Select all

onDie = function(c)
    hudPrint("Well that was a dumb idea destroying the barrel.")
    for i = -1, 1 do
       for j = -1, 1 do
          if i ~= 0 or j ~= 0 then
             spawn("fireball_large", c.go.level, c.go.x + i, c.go.y + j, 0, c.go.elevation)
          end
       end
    end
    for dir=0,3 do
      local dx,dy = getForward(dir)
      for e in c.go.map:entitiesAt(c.go.x+dx,c.go.y+dy) do
        for _,comp in e:componentIterator() do
          if type(comp.knockback) == "function" then
            comp:knockback(dir)
          end
        end
      end
    end
end
(Actually knocking back the party, or a monster, is one line; "party.party:knockback(direction)". See the scripting reference.)
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.
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Re: Knock Back Effect

Post by Mysterious »

Thank you kindly for this Minmay. Will try it out now. Thxs again for your time.
Post Reply