0 votes

Hello!

I'm having some problems with script inheritance.
I have one base script Object.gd (only the relevant part) :

extends Node2D
class_name ObjectBase

#Signal when click on the Area2D
func _on_Area_input_event(viewport, event, shape_idx):
if Input.is_action_just_pressed("left_click"):
    print("click") #I get this printed so the signal is received normally
    if GLOBAL.game_mode == 1: #Easy mode
        _show_choices()
    else: #Normal mode
        _choice_click(0)

func _show_choices():
    pass #Not implemented yet

func _choice_click(choice_id):
    if choice_id == 0: #Look
        if have_closeup:
            _closeup() 
        else:
            #Connect this to the Part's root
            emit_signal("show_text", look_text)
    else:
        _choice_logic(choice_id)

func _choice_logic(choice_id): #Abstract, should be implemented in the instance's script
    pass

So now I create an instance of Object.tscn (which contains nodes with connected signals), attach a new empty script inherited from Object.gd.

But it does something weird at runtime:
When I trigger the "Area2Dinputevent" signal in the instanced object, the function _on_Area_input_event() actually triggers too, but it doesn't call _show_choices() or _choice_click() when it should, it just run past them when I debug, which I can't understand.

Is this a bug or is it intentional? In both cases, how can I make the script call the functions I need without rewriting them in each instance's script?

Thanks.

PS: I'm on Godot 3.1 stable.

asked Nov 12, 2019 in Engine by Lulullia (12 points)

It cannot go pass the if/else statement. What happens if you put a print in show_choices and choice_click (at the start of the functions)?

Well that's weird? Now it works for some unknown reason?
It get printed and everything work well... How can this be possible xD
Yesterday I was debugging line by line at runtime and the functions never got called. And I swear I didn't modify any code, I just wrote the prints then ran the game haha.

Although after some more testing, it appears that the print works even if the function where it is located doesn't get called (at least visually when I debug line by line) so maybe it just don't show when Godot call it.

So thank you anyway for helping me!

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.