Functions with trigger collisions
enemy (moving to the player)
projectile (moving to the enemy with damage, enemy gets damage and then gets deleted when hp = 0 then error goes off) turned of the go.delete and no errors.
Errors:
ERROR:SCRIPT: scripts/castle_player.script:34: argument #-1 contains one or more values which are not numbers: vmath.vector3(nan, nan, nan)
stack traceback:
[C]:-1: in function create
scripts/castle_player.script:34: in function spawn_projectile
scripts/castle_player.script:57: in function <scripts/castle_player.script:52>
Quick find for the errors:
player script 34 = factory.create(“/factory_parent#projectile_factory”, go.get_position(), nil,
scripts 57 on castle_player (function update) = spawn_projectile(self)
script 52 = function updateturning of the go.delete() in enemy_units.script
local function change_hp(self, amount) shows no errors but game object exists with 0 hp.
castle_player:
function init(self)
self.projectile_int = 1.5
self.projectile_time_spent = 0
self.explode_int = 2.5
self.explode_time_spent = 0
self.enemies_in_range = {}
self.hp = 100
self.max_hp = 100
end
local function get_closest_enemy(self)
local closest = nil
local min_dist = 999999
for _, enemyu_coll in pairs(self.enemies_in_range) do
if go.exists(enemyu_coll) then
local dist = vmath.length(go.get_position(enemyu_coll) - go.get_position())
if dist < min_dist then
min_dist = dist
closest = enemyu_coll
end
end
end
return closest
end
local function spawn_projectile(self)
local closest = get_closest_enemy(self)
local dir = vmath.normalize(go.get_position(closest) - go.get_position())
factory.create("/factory_parent#projectile_factory", go.get_position(), nil,
{
direction = dir
}
, 1)
end
local function spawn_explode(self)
local closest = get_closest_enemy(self)
--local dir = vmath.normalize(go.get_position(closest) - go.get_position())
factory.create("/factory_parent#explode_factory", go.get_position(), nil,
{
goal_point = go.get_position(closest)
}
, 1)
end
function update(self, dt)
self.projectile_time_spent = self.projectile_time_spent + dt
if self.projectile_time_spent >= self.projectile_int and #self.enemies_in_range > 0 then
spawn_projectile(self)
self.projectile_time_spent = 0
end
self.explode_time_spent = self.explode_time_spent + dt
if self.explode_time_spent >= self.explode_int and #self.enemies_in_range > 0 then
spawn_explode(self)
self.explode_time_spent = 0
end
end
local function change_hp(self, amount)
self.hp = self.hp + amount
print("current castle hp: ", self.hp)
end
function on_message(self, message_id, message, sender)
if message_id == hash("trigger_response") then
if message.other_group == hash("enemyu_coll") then
table.insert(self.enemies_in_range, message.other_id)
end
end
if message_id == hash("change_hp") and message.change_amount then
change_hp(self, message.change_amount)
end
end
enemy_units:
go.property("enemy_type", hash(""))
-- maybe need this bellow!
--local game_manager = require "scripts.game_manager"
local stats = {
torch_goblin = {
speed = 100,
hp = 25,
damage = 11,
},
tnt_goblin = {
speed = 100,
hp = 14,
damage = 24,
},
knight_red = {
speed = 34,
hp = 41,
damage = 20,
}
}
-- Shared value between all instances of this script (local hp = 0, local damage = 0, local speed = 0)
function init(self)
local type = ""
if self.enemy_type == hash("torch_goblin") then type = "torch_goblin"
elseif self.enemy_type == hash("tnt_goblin") then type = "tnt_goblin"
elseif self.enemy_type == hash("knight_red") then type = "knight_red" end
self. hp = stats[type].hp
self.damage = stats[type].damage
self.speed = stats[type].speed
self.castle_pos = go.get_position("/castle_player")
self.direction = vmath.normalize(self.castle_pos - go.get_position())
--[[ local angle = vmath.quat_rotation_z(math.atan2(self.direction.y, self.direction.x))
go.set_rotation(angle)
-- ]]
sprite.set_hflip("#sprite", self.direction.x < 0)
end
function final(self)
end
function update(self, dt)
local pos = go.get_position()
local new_pos = pos + self.direction * self.speed * dt
go.set_position(new_pos)
end
local function change_hp(self, amount)
self.hp = self.hp + amount
if self.hp <= 0 then
factory.create("/factory_parent#burst_factory", go.get_position(), nil, nil, 1)
-- with the delete line bellow enable it will cause error???!!!!
go.delete()
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("trigger_response") then
if message.other_group == hash("castlep_coll") then
msg.post(message.other_id, "change_hp", {change_amount = -self.damage})
factory.create("/factory_parent#burst_factory", go.get_position(), nil, nil, 1)
go.delete()
--else message.other_group = hash("firerange_coll")
--print("Fire Now!!!")
-- game_manager.print_table(message)
-- print("I am Deleting Myself")
end
end
if message_id == hash("change_hp") and message.hp_change ~= nil then
change_hp(self, message.hp_change)
end
end
projectile:
go.property("direction", vmath.vector3())
function init(self)
self.speed = 240
self.time_alive = 0
self.damage = 25
local angle = vmath.quat_rotation_z(math.atan2(self.direction.y, self.direction.x))
go.set_rotation(angle)
end
function update(self, dt)
local pos = go.get_position()
local new_pos = pos + self.speed * self.direction * dt
go.set_position(new_pos)
self.time_alive = self.time_alive + dt
if self.time_alive > 4 then go.delete() end
end
function on_message(self, message_id, message, sender)
if message_id == hash("trigger_response") and message.other_group == hash("enemyu_coll") then
msg.post(message.other_id, "change_hp", {hp_change = -self.damage})
go.delete()
end
end

