Invalid call. Nonexistent function '_init' in base 'KinematicBody'.

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

I’m trying to code a 3d fps game with a multiplayer and used the project of GDQuest to try out the multiplayer API and it worked well in 2d but not in 3d. The problem here is that I created a “_init” function in my Player script which

extends KinematicBody

func _init(nickname, start_position, is_slave):
label.text = nickname
velocity = start_position
if is_slave:
	print("is slave: " + is_slave)

then in the World.gd I call it in the _ready() function to paste the info of each player for preloading

extends Spatial

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	get_tree().connect('network_peer_disconnected', self, '_on_player_disconnected')
	get_tree().connect('server_disconnected', self, '_on_server_disconnected')
	
	var new_player = preload('res://Player.tscn').instance()
	new_player.name = str(get_tree().get_network_unique_id())
	new_player.set_network_master(get_tree().get_network_unique_id())
	add_child(new_player)
	var info = Network.self_data
	new_player._init(info.name, info.position, false)

the network self_data looks like this:

var self_data = { name = '', position = Vector3(0, 1, 0) }

when I start it shows a normal menu for hosting/ joining but when it tries to instance the Player it fails with the above error message.

The question is, why does it not work? I have the function in my Players’ script and it shows the function when i type in ‘new_player.’
https://imgur.com/a/j1VQlNL

:bust_in_silhouette: Reply From: Eric Ellingson

I don’t think you’re meant to call _init() directly. I believe that is used for overriding parent class variables. You’ll want to create your own method for initializing.

func _init():
     parent_variable = "new value for child class"

func init_for_real(nickname, start_position, is_slave):
    label.text = nickname
    velocity = start_position
    if is_slave:
        print("is slave: " + is_slave)

So then you’d just call init_for_real() instead of _init()

Thanks for the quick answer, it worked in the way that the game does not crash anymore but I did not have time yet to implement the sync, puppet and master functions so the game still does not work.

TotallySober | 2019-02-20 19:05