Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

I have a programing question that isn't clarified by the app if its OK to ask here: 

In lesson 14 the practice where we make the robot take half damage, the if statement where we make the robot  take damage half damage if their level is above level 2 only works if that if statement is the first thing written in the take_damage function. Why does it only work in that order.


Example:

var level = 3

var health = 100

var max_health = 100

func take_damage(amount):

    health -= amount

    if health < 0:

        health = 0

    if level > 2:

        amount *= 0.5

^ This doesn't work.

========================

var level = 3

var health = 100

var max_health = 100

func take_damage(amount):

    if level > 2:

        amount *= 0.5

    health -= amount

    if health < 0:

        health = 0

^ This does work.

(1 edit) (+1)

I would assume (based on my experience with Python, which GDScript is similar to) that the first example does not work because the order of execution is different. In that example, when "take_damage(amount)" is called, "amount" is first subtracted from "health", then it checks if health is lower than 0, then it checks the level and changes the "amount" after the original "amount" has already been subtracted. I hope that makes sense.

(+1)

To clarify: Assuming it works like in Python, functions are generally processed from top to bottom.

this makes sense to me. it sounds like an order of operations problem. thanks for the response!

Always happy to help!

I checked the app to see when/if it mentions this, it looks like in lesson 4 it's kinda mentioned in passing but it's easy not to remember ten lessons later, haha.