Hp tutorial?

Hi all;

trying to get into using lua and defold. I have intermediate programming knowledge with other languages but lua is new to me. I’m slowly coming around. I learn better by looking through code and figuring out how it works. Anyone know of a project i can look at that shows a very basic “battle system” im just looking to see the process as were what code goes for. basically id like to see an example of a player sprite and an enemy sprite they would both have to variables hp and attack when you hit the attack button you check for a min distance and if it passes subtract attack to the hp of the enemy. very basic but im a little confused when it comes to scripting what codes get nested where.

im guessing you would declare the variables for the enemy and player in there own scripts and create an additional script that contains your battle system method?

Someone probably has a much more dedicated example for you, but for now here’s my Ludum Dare project.

The files you’d specifically be looking for would be Gatling.script, 44mm.go, and enemy.go.

Gatling.script) lines 184-190

Most important in 44mm.go is the collision object. It has a mask of “enemy”, so that’s what it’s going to provide collision_response messages for (to itself, and to the other object). It is also of the group “ammo”.

In enemy.go, you have the collision object and the script which are very important. The collision object is going to detect if anything of one of the listed layers intersects with itself, and if so, it sends a collision_response to the script. The script inside enemy.go will process the collision_response message. At the start of the enemy’s life, it has 6 health. Each collision_response lowers it’s health by 2.

For your case, a get_distance function would be a good bet. It’s more taxing (I think, but don’t worry about it unless you have 1000 instances running it at the same time), but it’s a bit easier than getting a distance system set up with collision objects. Here’s that function:

function get_distance(point1, point2)
	local distance = ((point1.y - point2.y) * (point1.y - point2.y)) + ((point2.x - point1.x) * (point2.x - point1.x))
	distance = math.sqrt(distance)

	return distance
end

You would use it like so:

my_distance = get_distance(my_obj_pos1, my_obj_pos2)

LUDUM DARE - OPERATOR.zip (8.0 MB)

1 Like

thanks for the input! I tried unzipping your zip file but my mac is saying its corrupted… Very possible its on my end as its super late. I will try to check it out again tomorrow. Appreciate the quick response!

Sorry, I messed with the filetype. It was .rar, but I thought it worked as .zip as well.

… The forum isn’t allowing me to upload the .zip file now, sorry. If you can switch it to .rar and open it, that’s all I can do now >:(.