script : return throwing : bad object error

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Damonya
Posts: 134
Joined: Thu Feb 28, 2013 1:16 pm
Location: France
Contact:

script : return throwing : bad object error

Post by Damonya »

I try to create a script that return throwing weapons on the player when it is thrown on a monster.

I am inspired by viewtopic.php?f=14&t=3796

monster.lua

Code: Select all

cloneObject{
	name = "crowern_red",
	baseObject = "crowern",
	onProjectileHit = function(monster, weapon)
	return return_throw_script.Projectile(monster,weapon)
    end,
}
With only rock, no problem

Code: Select all

thrower_ord = 1

function getDistance(obj1,obj2)
  local distance = 0
  local XNumber = math.max(obj1.x,obj2.x) - math.min(obj1.x,obj2.x)
  local YNumber = math.max(obj1.y,obj2.y) - math.min(obj1.y,obj2.y)
  distance = XNumber + YNumber
  return distance
end

function Projectile(monster,weapon)

if weapon.name == "rock" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("rock", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
	end
end
end

But I have a "bad object" error when I add more throwing weapon in the Projectile function:

Code: Select all

function Projectile(monster,weapon)

if weapon.name == "rock" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("rock", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
	end
end
if weapon.name == "shuriken" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("shuriken", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
	end
end
if weapon.name == "throwing_axe" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("throwing_axe", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
	end
end
if weapon.name == "throwing_knife" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("throwing_knife", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
	end
end
end
With "elseif", no error but the script doesn't run.

And I have try to simplify the script with a

Code: Select all

local itemList = {"rock", "throwing_knife", "throwing_axe", "shuriken"}
for i= 1,4 do
if weapon.name == itemList [i]
...
end
end
but it is worse.
Last edited by Damonya on Sun Apr 21, 2013 8:36 pm, edited 1 time in total.
Orwak - MOD - Beta version 1.0
User avatar
Damonya
Posts: 134
Joined: Thu Feb 28, 2013 1:16 pm
Location: France
Contact:

Re: script : return throwing : bad object error

Post by Damonya »

Even more strange.

If I replace only "rock" by "shuriken" the editor bug and close

TRUE If I throw a rock:

Code: Select all

thrower_ord = 1

function getDistance(obj1,obj2)
  local distance = 0
  local XNumber = math.max(obj1.x,obj2.x) - math.min(obj1.x,obj2.x)
  local YNumber = math.max(obj1.y,obj2.y) - math.min(obj1.y,obj2.y)
  distance = XNumber + YNumber
  return distance
end

function Projectile(monster,weapon)

if weapon.name == "rock" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("rock", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
   end
end
end
FALSE if I trhow a shuriken

Code: Select all

thrower_ord = 1

function getDistance(obj1,obj2)
  local distance = 0
  local XNumber = math.max(obj1.x,obj2.x) - math.min(obj1.x,obj2.x)
  local YNumber = math.max(obj1.y,obj2.y) - math.min(obj1.y,obj2.y)
  distance = XNumber + YNumber
  return distance
end

function Projectile(monster,weapon)

if weapon.name == "shuriken" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("shuriken", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
   end
end
end
[string "Item.lua"]:0: attempt to index field 'map' (a nil value)
stack traceback: :?
Orwak - MOD - Beta version 1.0
alois
Posts: 112
Joined: Mon Feb 18, 2013 7:29 am

Re: script : return throwing : bad object error

Post by alois »

Maybe I'm mistaken, but the object 'weapon' is destroyed once one of the objects is detected (i.e., if you throw a rock, then weapon is 'nil' after destroying it) so that the other lines ("weapon.name == etc") may yield an error because 'weapon' does not exist anymore. Try to put a 'return' (or return false, since you are canceling the 'throwing action') every time the condition is satisfied:

Code: Select all

if weapon.name == "rock" then
    if findEntity(weapon.id) ~= nil then
      weapon:destroy()
      local distance = getDistance(party,monster)
      local facing = (party.facing + 2)%4
      local dx,dy = getForward(facing)
      shootProjectile("rock", party.level, monster.x+dx, monster.y+dy, facing, distance, 1, 1, 0, 0, 0, 0, false, false)
      return false
   end
end
alois :)
User avatar
Damonya
Posts: 134
Joined: Thu Feb 28, 2013 1:16 pm
Location: France
Contact:

Re: script : return throwing : bad object error

Post by Damonya »

Thanks a lot alois. It was that :)

Sometimes, I waste time on details that escape me
Orwak - MOD - Beta version 1.0
Post Reply