#How to differentiate between different projectiles attacks?

68 messages · Page 1 of 1 (latest)

night ingot
#

My character have a few projectile attacks (its a 2d game) and I want to program my enemies reacting to all of them differently (each attack should deal different amount of damage. But I am not sure how to go about this. Should each attack be in a group or should I do something else?

undone timber
#

Is the only difference the amount of damage between the attacks?

undone timber
night ingot
#

mostly the amount of damage (and for one attack, I am planning for a stun, though I probably wont do that now) but for now, the amount of damage. I just wonder how do people normally differentinate between projectils.

undone timber
#

So most would make a default projectile script

#

Where you can store all basic information about a projectile

#

That you create a script for each different attack

#

And give the seperated values.

night ingot
#

I see

#

I um, already scripted them and they dont share a default script...

undone timber
#

Haha how many do you have?

night ingot
#

I just have two (and I guess a lighting attack I have, which is more of a special attack) but mostly focusing on the two projectiles I want to finish up, so two I would say

undone timber
#

Can you share both codes?

night ingot
#

alright

#

magicorb projectile

 extends CharacterBody2D


var speed = 100
@onready var SFX = $SFX

func  _ready() -> void:
    SFX.play()

func _process(delta):
    velocity.x = speed
    
    move_and_slide()
    


func _on_kill_timer_timeout():

    queue_free()
#

the standard projectiles

extends CharacterBody2D

var speed = 0
@onready var sprite = $Sprite2D
@onready var magicContact = $Area2D
var lenght = 50
var point = Vector2()
@onready var trail = $CPUParticles2D
#@onready var particle_reverse = load("res://Startup/Player/Amanda/melee-beam_reverse.png")
#@onready var particle = load("res://Startup/Player/Amanda/melee-beam.png")
func _physics_process(delta: float) -> void:
    
    velocity.x = speed
    
    #get direction
    

    move_and_slide()
    
    for enemiesInBodies in magicContact.get_overlapping_bodies():
        if enemiesInBodies.is_in_group("enemy"):
            queue_free()
    
    
    for enemiesInArea in magicContact.get_overlapping_areas():
        if enemiesInArea.is_in_group("enemy"):
            queue_free()
    
undone timber
#

If you add js to the embedment you get the code in colour

#

“‘JS

“‘

oblique valve
#

https://youtu.be/sZDJJeDNe_M?si=bOBjYA_HyW2VwpSq
Found this tutorial awhile ago that should give you an idea about an approach to different attacks/attack modifiers if you wanna give it a shot

In this video we'll look at an application of the Strategy Pattern to game dev in Godot 4.

The Strategy Pattern is a programming pattern useful for switching how an algorithm works at runtime. We use it a little differently here to apply a variable set of upgrades to a projectile fired by the player. A very closely related term is "polymorphism...

▶ Play video
undone timber
#

Or that, you could indeed watch a video haha

undone timber
#
extends CharacterBody2D


var speed = 100
@onready var SFX = $SFX

func  _ready() -> void:
    SFX.play()

func _process(delta):
    velocity.x = speed
    
    move_and_slide()
    


func _on_kill_timer_timeout():

    queue_free()
#

But I gues not with this code

#

Nevermind

#

Do you want my help or are you gonna watch the video first? 🙂 @night ingot

#

I am more than happy to help

night ingot
#

I will have your help first, sure!

undone timber
night ingot
#

yes

#

they are both seperate projectiles for seperate attacks

#

standard being for the most basic attack

undone timber
#

The most basic attack doesn’t have visuals?

night ingot
#

it does.

#

I will show y ou

#

this beam in front of the player

undone timber
#

I meant the magi corp thing

#

I mixt the names ❤️

night ingot
#

the magicorb also have visuals, yeah

#

it shoot a bunch of orbs in front of the player

undone timber
#

Alright

#

Okay let’s put it this way

#

When you are gonna use something a lot of times

#

It’s easier to make a default

#

For example both are CharacterBody2D’s

#

If that’s gonna be a standard, you could make a default script,

Extends CharacterBody2D
class_name BasicProjectile

#

Than in the other 2 files you can extend BasicProjectile instead of CharacterBody2D

night ingot
#

oooh

undone timber
#

Now you can think of things every projectile will have

#

Speed, sound, etc

#

Put all of that in the default script

#

By extending every new projectile to the default one

#

Everything in default

#

Will be implemented

#

🙂

night ingot
#

I understand now. That makes a lot of sense.

#

Thank you very much!

#

I will do that

undone timber
#

No problem for just a small example what you can do this way is this

#

In the default script you have a function on_hit

And the variable var damage : Int

#

In another script you extend Default and set the damage = 10

#

Now in your enemy script you can handle the on_hit, by calling the default on_hit function, which automatically calls the damage variable from this attack.

#

This way the only thing you have to change for every spell is the damage

#

All functions stay usable and don’t need copy pasting!

night ingot
#

oh wow. yeah, that would be very useful!

#

you really helped me out

#

I am very thankful

undone timber
#

No problem man, happy to help, let me know if there is anything else I can do for you, but this is the commonly used approach when using stuff you are gonna use a lot of differentiates of