onAttack problem

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

onAttack problem

Post by Leki »

Hi, I have this script:

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

   onAttack = function(champion, weapon)
      -- check if it is the ice_sword
      if weapon.name == "ice_sword" then
         print ("hello world")
      end
   end,
}
the probelem is, that game crashes if champion attacks with empty hand.
Empty hand returns nil and nil.name --> crash, I guess.

I solve this with one more if:

Code: Select all

   onAttack = function(champion, weapon)
      if weapon == nil then
           print("empty hand")
           return
      else
           -- check if it is the ice_sword
           if weapon.name == "ice_sword" then
               print ("hello world")
           end
      end
   end,
is this right solution or I am doing somthing wrong?
I'm the Gate I'm the Key.
Dawn of Lore
dasarby
Posts: 28
Joined: Sun Dec 30, 2012 10:46 pm

Re: onAttack problem

Post by dasarby »

That seems right to me. Because the character is not guaranteed to have a weapon, you need to check if there is a weapon before checking the weapon name.

If all you care about is the ice sword, you can do something like:

Code: Select all

   onAttack = function(champion, weapon)
      if weapon ~= nil and weapon.name == "ice_sword" then
           print ("hello world")
      end
   end
}
This will check that the weapon is not nil (~=) and then check if it is the ice sword.
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: onAttack problem

Post by Leki »

OK then. Thanks m8 :)
I'm the Gate I'm the Key.
Dawn of Lore
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: onAttack problem

Post by Komag »

you could always pass it all to a function in your dungeon instead of in that lua file, so you have more info with crashes and also won't crash totally, just stop preview.

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

   onAttack = function(champion, weapon)
      attackScript.doAttack(champion, weapon)
   end,
}
then in the dungeon have a script entity called attackScript and do your code in the function doAttack
Finished Dungeons - complete mods to play
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: onAttack problem

Post by Leki »

Komag wrote:you could always pass it all to a function in your dungeon instead of in that lua file, so you have more info with crashes and also won't crash totally, just stop preview.

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

   onAttack = function(champion, weapon)
      attackScript.doAttack(champion, weapon)
   end,
}
then in the dungeon have a script entity called attackScript and do your code in the function doAttack
...but in that way I cannot public sources as a independent plugin I guess --> code must be part of "mod_objects.lua" --> I have no idea what method is better. Full crashes or moving "final" scripts to the external lua file :?
I'm the Gate I'm the Key.
Dawn of Lore
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: onAttack problem

Post by Diarmuid »

Just a quick tip, I recently discovered that in lua non-nil values return true, so you can write:

Code: Select all

if weapon and weapon == "sword" then
instead of

Code: Select all

if weapon ~= nil and weapon == "sword" then
cleans up code. :)
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: onAttack problem

Post by Komag »

people can just copy paste the text, doesn't have to be actual files they copy/install
Finished Dungeons - complete mods to play
Post Reply