0

onready var _boardView_scene = load("res://UrBoardView.tscn") gets the scene reference. Then I try to instance the scene like this _urBoardViewInstance = _boardView_scene.instance(). The UrBoardView scene has a script of type BoardView like this to its topmost node(meaning the scene is of type BoardView)-

extends Node
class_name BoardView

The _urBoardViewInstance get instanced as a Node here. Casting it to BoardView is not working(giving null) here - var boardViewCast = _urBoardViewInstance as BoardView I need to call a custom init method from BoardView script before sending it to add_child(_urBoardViewInstance). Basically, I want to instance the scene not as a node but as the type of script that it has attached.(or just call like this

_urBoardViewInstance = _boardView_scene.instance().new(board)

which apparently is not possible in Godot right now.) And that casting is not working. This is the how I am casting -

var boardViewCast: BoardView
_urBoardViewInstance = _boardView_scene.instance()
boardViewCast = _urBoardViewInstance as BoardView
boardViewCast.init(gameplay.Board)

the boardViewCast is null after this. It basically does not cast at all.

Krinjon
  • 23
  • 6
  • Casting should work. No idea why it would be failing. Are you not getting errors? What version of Godot? - Anyway, if you need to call a custom method, you may try [`call`](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call), which would not require casting. – Theraot Mar 24 '21 at 20:16
  • @Theraot ``` var boardViewCast: BoardView _urBoardView = _boardView_scene.instance() boardViewCast = _urBoardView as BoardView boardViewCast.init(gameplay.Board) ``` calling init says - Invalid call. Nonexistent function 'init' in base 'Nil'. also the boardViewCast shows null in the debugger. was working with 3.2.3 mono and now tried with just standard normal version of 3.2.3 stable. both same thing. – Krinjon Mar 24 '21 at 21:39
  • @Theraot ok sorry. so in the script I was trying to set a variable by getting node from the scene. onready var board: UrBoard = $UrBoard like this. deleting the variable fixed the issue.Dont know why that would cause the issue. Cause the scene was ok being instance as a node. and it ok was being added to child. The script did not have any error. it was running fine in its own scene. Any way you were right it should have worked. – Krinjon Mar 25 '21 at 07:42

1 Answers1

0

so in the script I was trying to set a variable by getting node from the scene. onready var board: UrBoard = $UrBoard like this. Deleting the variable fixed the issue(actually not setting the var from getNode. Kept the variable).Dont know why that would cause the issue. Cause the scene was ok being instance as a node. and it ok was being added to child. The script did not have any errors. it was running fine in its own scene.

Krinjon
  • 23
  • 6