Need some help with damage/hp approach (SOLVED)

Hi, guys! I am not a programmer but decided to make game for CoronaDefold game jam and here is my progress so far: https://denischera.com/reactor_defence2/index.html

The goal is to kill mobs before they reach the reactor. Win/lose states are not implemented yet, but one can upgrade turrets and reactor if there is required amount of money (some images are missing for now :stuck_out_tongue: ). Enabled turrets consume power which reactor generates. :slight_smile: If you need money, just use “space”.

Okay, I’ve implemented some main logic of the game, but now I have some problem with damage dealing to mobs. For now, damage is the same for every state of turret, but I want to make each next state of turret to deal more damage. But how? :slight_smile: Tried some approaches but nothing works. I would be glad to hear some of your ideas! Thanks!

1 Like

What you can do is:

  • Create a collision object on the turret that is enabled when the turret fires.
  • Add a script to the turret.
  • When the script component receives collision messages, pass a message to the enemy that collided (available in message.other_id). Call the message “do_damage” or something and pass the amount of damage as data.
  • When the turret updates, increase the given damage.

You can also pass the turret level and have the enemy work out the amount of damage based on the level. It depends on your design what’s best.

2 Likes

Tried few times to pass some data through messages already but didn’t succeed. I think, I make something wrong.

Here is some of the code of let’s say “laser” which collides with mobs:

if message_id == hash("collision_response") then
 	msg.post(message.other_id, "do_damage", {damage = 100})
end

But how I must declare a variable “damage” in my mob’s script to be able to recieve the updated value?

Now I have something like this:

function init(self)
	self.hp = math.random(200, 500)
	damage = 1
end

function on_message(self, message_id, message, sender)
	if message_id == hash("do_damage") then
		self.hp = self.hp - damage
	end
end

No, the data arrives in the message parameter:

function on_message(self, message_id, message, sender)
	if message_id == hash("do_damage") then
		self.hp = self.hp - message.damage
	end
end
3 Likes

Super! Now it works :slight_smile: Thanks a lot!

2 Likes

No problem! Please post updates to your game so we can try it out!

3 Likes

Sure I will :slight_smile:

3 Likes