StreamPeerTCP instance created problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By huluhou
:warning: Old Version Published before Godot 3 was released.

when created a StreamPeerTCP inside a function it will not woking?

code:

func _on_Connect_pressed():
print(“Start”)
var client = StreamPeerTCP.new()
print(client.connect(“127.0.0.1”,2003))

#this code is not working when i press the button

but if write like this, put a var out side the func then sign to it, it works.\

code:

var client

func _on_Connect_pressed():
print(“Start”)
client = StreamPeerTCP.new()
print(client.connect(“127.0.0.1”,2003))

is there any different?

:bust_in_silhouette: Reply From: jelly

I didn’t test the code so I’m not sure what is up but it is a really good idea to make your client variable outside of the function. That way it doesn’t cease to exist after your onConnect_pressed() function is all wrapped up. If that were to occur you wouldn’t be able to address your new StreamPeerTCP class and thus not be able to communicate with the server/client at all.

hi jelly

thanks for the answer. the StreamPeerTCP instance will store outside the function thats for sure when we write the real code.

but this just interested why is not working when inside function but if i make some other instance it works just fine.

is not a big deal, maybe have to get the answer from source code.

huluhou | 2016-08-30 06:12

It is certain perplexing o.O

What error is it giving you?

jelly | 2016-08-30 07:25

no error.

the code just run though but i doesn’t connect to the server

print(“Start”)
//print start
var client = StreamPeerTCP.new()
//client instance created
print(client.connect(“127.0.0.1”,2003))
//return 0

then if put “var client” outside of function

it will connect to the server

huluhou | 2016-08-30 07:30