0 votes

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

code:

func onConnect_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 onConnect_pressed():
print("Start")
client = StreamPeerTCP.new()
print(client.connect("127.0.0.1",2003))

is there any different?

in Engine by (12 points)

1 Answer

0 votes

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.

by (56 points)

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.

It is certain perplexing o.O

What error is it giving you?

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

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 Frequently asked questions and 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 [email protected] with your username.