0 votes

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():
    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())
    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.'

in Engine by (12 points)

1 Answer

0 votes

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()

by (1,605 points)

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.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.