Page 1 of 2

how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 5:26 pm
by montagneyaya
Hello

I have this object :

Code: Select all

defineObject{
	name = "invisible_blocker_break",
	class = "Blockage",
	model = "mod_assets/models/env/null.fbx",
	brokenModel = "mod_assets/models/env/null.fbx",
	placement = "floor",
	health = 999999,
	evasion = -1000,
	hitSound = "barrel_hit",
	hitEffect = "hit_wood",
	editorIcon = 56,
	onDamage = function (self,damage,damageType)
		return damage
	end,
}
and I will use a lua in editor like this :

Code: Select all

health = 20
if blockage_door_1:onDamage() then
	DamageDoor()
end

function DamageDoor()
	health = health - blockage_door_1:onDamage()
	if health <= 0 then
		print("door is break")
	end
end
the blockage_door_1 is my invisible_blocker_break object.
If I run this, I have the error :

Code: Select all

if blockage_door_1:onDamage() then      attempt to call method 'onDamage' (a nil value)
How call a hook in a lua script ?

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 5:42 pm
by Grimwold
Hooks can only be added to asset definitions, they cannot be added to lua scripts.

What I think you want to do is something like this:

objects.lua

Code: Select all

    defineObject{
       name = "invisible_blocker_break",
       class = "Blockage",
       model = "mod_assets/models/env/null.fbx",
       brokenModel = "mod_assets/models/env/null.fbx",
       placement = "floor",
       health = 999999,
       evasion = -1000,
       hitSound = "barrel_hit",
       hitEffect = "hit_wood",
       editorIcon = 56,
       onDamage = function (self,damage,damageType)
          return damage_door_script.damageDoor(damage)
       end,
    }
Where damage_door_script is the name of your script entity in the dungeon containing the damageDoor() function.

then your script entity would be like this:

Code: Select all

health = 20

function damageDoor(damage)
   health = health - damage
   if health <= 0 then
      print("door is break")
   end
end
So when the blockage is damaged, the onDamage hook in the definition passes the amount of damage to the damageDoor() function in the script entity, which reduces the value of the health variable.

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 5:47 pm
by montagneyaya
Yes, but if I have multiple script (damage_door_script_1, damage_door_script_2, damage_door_script_3), should I create an object for each script ?
Is it not possible to make an association with the id of the object rather than the name/defineObject ?

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 6:11 pm
by Grimwold
OK I see what you mean.. you have a single blocker definition that you want to place at each breakable door.

I think you'd need to make use of the self variable to pass the ID of the blockage to the script.

e.g.

Code: Select all

onDamage = function (self,damage,damageType)
  return damage_door_script.damageDoor(self,damage)
end,
you'd have a single damage_door_script entity, but it would store the health for each door and, depending on the ID of the blockage that was damaged, it would reduce the appropriate health.

Code: Select all

health_door_1 = 20
health_door_2 = 20
health_door_3 = 20

function damageDoor(self,damage)
   local door_health = {health_door_1,health_door_2,health_door_3}
   blockage_number = tonumber(self.id:sub(15,1))
   door_health[blockage_number] = door_health[blockage_number] - damage
   if door_health[blockage_number] <= 0 then
      print("door is break")
   end
end
Assuming blockage iDs are "blockage_door_1" etc. we grab the number off the end (15th character length 1) then we look that up in the table of door health varaibles to see which health value to reduce.

If you want more than 10 of these in your dungeon, you might need to use "blockage_door_01" and grab the last 2 characters off the end. e.g. sub(15,2)

I have not tested this code. It should work... but I'm not 100% sure that pulling a variable from a table will allow it to be incremented/decremented... if it doesn't work we'd need to somehow construct the variable names.

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 6:46 pm
by montagneyaya
Don't work, blockage_number = tonumber(self.id:sub(14,1)) return nil.
But it's a good idea, I look in this direction.

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 6:58 pm
by Grimwold
montagneyaya wrote:Don't work, blockage_number = tonumber(self.id:sub(14,1)) return nil.
But it's a good idea, I look in this direction.
Yeah, just been testing it myself and not having any luck... seems to be with self.id .. perhaps need to do find entity first maybe

Code: Select all

blockage_entity = findEntity(self.id)

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 7:09 pm
by Grimwold
ok my understanding of sub seems to be wrong... self.id:sub(1,4) will return the first 4 characters, but self.id:sub(4,1) will not return the 4th character! I need to investigate this further.

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 7:21 pm
by montagneyaya
Grimwold wrote:ok my understanding of sub seems to be wrong... self.id:sub(1,4) will return the first 4 characters, but self.id:sub(4,1) will not return the 4th character! I need to investigate this further.
ok, it's self.id:sub(first character, last character(optional)), and if character is negative, the beginning of substring is the end of the string.
here you can use self.id:sub(15,15), or self.id:sub(15), or self.id:sub(-1)

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 7:27 pm
by montagneyaya
now it's a new problem, when the onDamage call the script all health are initialized each time in door_health table

Re: how the damage transferred to another entity ?

Posted: Tue Nov 20, 2012 7:31 pm
by montagneyaya
OK, this works

Code: Select all

health_door_1 = 20
health_door_2 = 20
health_door_3 = 20
door_health = {health_door_1,health_door_2,health_door_3}

function damageDoor(self,damage)
	blockage_number = tonumber(self.id:sub(-1))
	door_health[blockage_number] = door_health[blockage_number] - damage
	if door_health[blockage_number] <= 0 then
		print("door is break")
	end
end