Why does my if-statement not work when I have (x-100) instead of x?

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
  • #1
Darkmisc
204
27
TL;DR Summary
I'd like enemies to fire a shot once they get behind the player. I have an if-statement to fire a shot if position<x. This works. However, the if-statement doesn't work if I write "if position<x-100".
Hi everyone

I'm making a shoot 'em up in which enemies travel right to left towards the player. If they get behind the player, they are supposed to fire a shot at the player.

The code below for the enemy works, but I'd like a slight delay before the shot is fired.

enemy:
func _physics_process(delta):
 

    position+=  speed*movement_vector*delta

    if position.x <=Global.player_pos.x and shot_fired==false:
        launch_proximity()

shoot_laser:
func launch_proximity():
    shot_fired=true
    player_pos=Global.player_pos.x
    shoot_pos = player_pos

    if position.x<shoot_pos:
        print("bam")
        var child_node = $EnemyBarrel/Muzzle
        Global.rot = $EnemyBarrel.rotation
        var l = LASER.instance()

        get_parent().add_child(laser_parent)
        laser_parent.add_child(l)
        l.global_position = child_node.global_position
        l.rotation = $EnemyBarrel.rotation
        add_child(l)

However, the enemy won't fire if I have this at line 6
line 6:
    if position.x<shoot_pos -100:
Does anyone know why?

Thanks

EDIT: I have the player in the middle of the screen when the enemies pass, so I don't think it's because the enemies are off-screen by the time they are 100 pixels behind the player.

Also, I've used position.x<shoot_pos - rand_range(0, 200) and they've never fired with that code either.
 
Last edited:
Technology news on Phys.org
  • #2
To debug this kind of problem, insert a line print(position.x, shoot_pos) before the test that is not doing what you expect. Edit: If it is still doing something strange, try print(type(position.x), type(shoot_pos)).

When you post code here, make sure you select an appropriate language: GDScript is based on Python, so use Python.

You will find that it is much easier to debug code if you use consistent spacing, so for instance
shoot_laser:
func launch_proximity():
    shot_fired = true
    player_pos = Global.player_pos.x
    shoot_pos = player_pos

    if position.x < shoot_pos:
        print("bam")
        var child_node = $EnemyBarrel / Muzzle
        Global.rot = $EnemyBarrel.rotation
        var l = LASER.instance()

        get_parent().add_child(laser_parent)
        laser_parent.add_child(l)
        l.global_position = child_node.global_position
        l.rotation = $EnemyBarrel.rotation
        add_child(l)

It is a good idea to follow the Python style guide https://peps.python.org/pep-0008/.
 
  • Like
Likes berkeman, FactChecker and Darkmisc
  • #3
I think some statements about GDscript operator precedence and short-circuit evaluation are a little vague.
This might be over-cautious and make no difference, but if things are acting strange, I would use more parentheses to force the operation order as you want it:
Python:
if ( position.x < (shoot_pos -100) ):
 
Last edited:
  • Like
Likes Darkmisc and phinds
  • #4
Some general debugging advice:

1.State the language you are using - don't make us guess. This looks like a precedence problem, which can be language-specific.

2. Give us the shortest possible piece of code that shows the problem. Often the process of finding this also provides a strong hint as to the answer.
 
  • Like
Likes Darkmisc and FactChecker
  • #5
Darkmisc said:
Also, I've used position.x<shoot_pos - rand_range(0, 200) and they've never fired with that code either.
Making it more complicated is not likely to help. A lot of things could go wrong. Is position.x global?
I see a recent edit. Have you tried any of our suggestions? Especially try @pbuk 's suggestion in post #2 to print the values of interest. When things are behaving strangely, it is often because the variable values are not what you expected.
 
  • Like
Likes Darkmisc
  • #6
Thanks. It works now.

This is the code I'm using

func _physics_process(delta)::
func _physics_process(delta): 
    position+=  speed*movement_vector*delta   
    if position.x + rand <=Global.player_pos.x  and shot_fired==false:
        launch_proximity()

shoot_laser:
func launch_proximity():
    shot_fired=true
    var child_node = $EnemyBarrel/Muzzle
    Global.rot = $EnemyBarrel.rotation   
    var l = LASER.instance()

    get_parent().add_child(laser_parent)
    laser_parent.add_child(l)
    l.global_position = child_node.global_position
    l.rotation = $EnemyBarrel.rotation
    add_child(l)

I think I needed to have my if-statement in the physics process.
 
Last edited by a moderator:

1. Why is my if-statement not working when I have (x-100) instead of x?

The if-statement is not working because the condition you have set is not being met. By using (x-100) instead of just x, you are changing the value that the if-statement is checking for. Make sure that the condition you have set is correct and that the value of x is being properly evaluated.

2. How does using (x-100) instead of x affect the if-statement?

Using (x-100) instead of x changes the value that the if-statement is checking for. This means that the if-statement will only execute if the value of x is exactly 100 less than the value you are expecting. If this is not the case, the if-statement will not be executed.

3. Is there a preferred way to write if-statements in terms of using variables?

There is no preferred way to write if-statements in terms of using variables. It depends on the specific situation and what you are trying to achieve. If you are using variables in your if-statement, make sure that the values are being correctly evaluated and that the condition is being met.

4. Can using (x-100) instead of x cause any errors in my code?

Using (x-100) instead of x can cause errors in your code if it is not used correctly. If the value of x is not exactly 100 less than the value you are expecting, the if-statement will not be executed. Make sure to test your code thoroughly and verify that the if-statement is working as intended.

5. How can I troubleshoot my if-statement when using (x-100) instead of x?

To troubleshoot your if-statement when using (x-100) instead of x, you can start by checking the values of your variables and verifying that the condition is being met. You can also use console.log() statements to track the flow of your code and see if the if-statement is being executed. Additionally, you can try using a different condition or rewriting your if-statement to see if that solves the issue.

Similar threads

  • Programming and Computer Science
Replies
4
Views
948
Back
Top