Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for Area monitor callback (e.g. wrong arity) #59628

Open
akien-mga opened this issue Mar 28, 2022 · 1 comment
Open

Improve error handling for Area monitor callback (e.g. wrong arity) #59628

akien-mga opened this issue Mar 28, 2022 · 1 comment

Comments

@akien-mga
Copy link
Member

@akien-mga akien-mga commented Mar 28, 2022

Godot version

3.x (e80a8be)

System information

Linux, Mageia 9 x86_64

Issue description

As discussed on chat with @RPicster and @rburing, the "area monitor callback" which can be defined via PhysicsServer for Area2D/3D doesn't seem to do proper error reporting/validation of the callback defined by the user.

So it can make it hard to know that e.g. one passed a callback with the wrong arity (number of parameter).

Object::emit_signal properly flags such errors for example (albeit with a not super-friendly error, which could be improved too (#12450)):

E 0:00:00.401   emit_signal: Error calling method from signal 'mysignal': 'Spatial(Spatial.gd)::_on_my_signal': Method expected 2 arguments, but called with 1..
  <Source C++>  core/object.cpp:1236 @ emit_signal()
  <Call Stack>Spatial.gd:13 @ _ready()

From a quick look at AreaSW (GodotPhysics) in 3.x, it does seem to skip reporting call errors indeed:

Variant::CallError ce;
obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);

Assuming that this is the proper location to handle such check, it likely needs to be done for GodotPhysics 2D and 3D as well as Bullet.

Didn't check master, might have the same issue.

Steps to reproduce

Code from @RPicster, not a minimal project per se but making one shouldn't be too difficult:

extends Spatial
 
var balls = []
 
var space_rid: RID
var shape_rid: RID
 
onready var multi: MultiMesh = $MultiMeshInstance.multimesh
 
class Ball:
    var area: RID
    var dir: Vector3 = Vector3.UP
    var speed: float = 30.0
    var transform: Transform
    
    func init(_dir, _speed, _transform, _space_rid, _shape_rid, _connect_to):
        area = PhysicsServer.area_create()
        dir = _dir
        speed = _speed
        transform = _transform
        PhysicsServer.area_set_space(area, _space_rid)
        PhysicsServer.area_add_shape(area, _shape_rid)
        PhysicsServer.area_set_collision_layer(area, 1)
        PhysicsServer.area_set_collision_mask(area, 1)
        PhysicsServer.area_set_monitorable(area, true)
        PhysicsServer.area_set_area_monitor_callback(area, _connect_to, "_on_ball_area_enter")
 
 
func _ready():
    space_rid = get_world().space
    shape_rid = PhysicsServer.shape_create(PhysicsServer.SHAPE_SPHERE)
    PhysicsServer.shape_set_data(shape_rid, 1)
 
 
func _physics_process(delta):
    if Input.is_action_just_released("ui_accept"):
        spawn_ball(Vector3(0,1,rand_range(-0.3, 0.3)).normalized())
    
    multi.visible_instance_count = balls.size()
    
    var b_id:int = 0
    for ball in balls:
        ball.transform.origin += ball.dir * (ball.speed * delta)
        PhysicsServer.area_set_transform(ball.area, ball.transform)
        multi.set_instance_transform(b_id, ball.transform)
        b_id+= 1
 
 
func spawn_ball(_dir):
    var new_ball = Ball.new()
    new_ball.init(_dir, rand_range(10, 15), 
        Transform(Basis.IDENTITY, Vector3(0, 0, 0)),
        space_rid, shape_rid, self)
    balls.append(new_ball)
 
 
func _on_ball_area_enter(area):
    print("works")

Minimal reproduction project

No response

@Akylzhan
Copy link

@Akylzhan Akylzhan commented Apr 1, 2022

I would like to work on this issue
Should I call ERR_FAIL_COND here?

Variant::CallError ce;
obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);

And what message to show?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants