How to call set_applied_force from a RigidBody2D child

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mateus Felipe C. C.

I’ve made an Scene called WorldBlock, which will be the basis for a series of blocks with common behaviour. This Scene structure is simply a RigidBody2D with the following attached script:

extends Node2D

enum BlockMaterial {
	Wood,
	Metal,
}

export (BlockMaterial) var blockMaterial
export (int) var initialResistance

signal damage

Now, I want to create a block based on this one. I created a block called Bomb, and the strucure is as follows:

- Link to WorldBlock
    - Sprite
    - Explosion Area (Area2D)
        - Area (CircleShape2D)
    - Explosion (Particles2D)
    - CollisionShape2D
    - Countdown (Timer)

And the script is, currently, as follows:

 extends "res://WorldBlock.gd"

const WorldBlock = preload("res://WorldBlock.gd")

var blowing = false

func _ready():
	$Countdown.connect("timeout", self, "_blow")
	connect("damage", self, "_blow")
	
func _blow():
	if blowing:
		return
	
	blowing = true
	
	$Sprite.hide()
	$CollisionShape2D.queue_free()
	$Explosion.emitting = true
	
	$Countdown.disconnect("timeout", self, "_blow")
	$Countdown.connect("timeout", self, "queue_free")
	$Countdown.wait_time = 2
	$Countdown.start()
	
	var within = $ExplosionArea.get_overlapping_bodies()
	
	for item in within:
		if item is WorldBlock and item != self:
			item.emit_signal("damage")
			
func _integrate_forces(state):
	set_applied_force(Vector2(1.0, 0.0))

However, I get the following error: error(26,38): Method 'set_applied_force' is not declared in the current class.

Why I get this if WorldBlock is a RigidBody2D? How can I apply the force within my block?

:bust_in_silhouette: Reply From: guppy42

your first script does not extend RigidBody2D but extends Node2D changing that will most likely fix your error

Well, I didn’t notice that. Why does Godot allow for a script to extend a class different of what it is? Does this makes sense in any scenario?

Mateus Felipe C. C. | 2018-10-26 12:38

Not sure if it makes sense to do so, my guess was just that you changed nodetype after creating the script

guppy42 | 2018-10-26 12:49

It can be really useful. See this KCC tutorial for an example.

Ruckus T-Boom | 2018-10-26 18:41