Go Back   The PokéCommunity Forums > Creative Discussions > Game Development > Pokémon Essentials
Reload this Page [Question] Internet enabled features crash when theres no connection

Notices
For all updates, view the main page.

Pokémon Essentials All questions and discussion about Pokémon Essentials, the Pokémon game kit for RPG Maker XP, go in here. Also contains links to the latest downloads and the Essentials Wiki.



Reply
 
Thread Tools
  #1    
Old January 6th, 2014 (03:13 AM).
desbrina's Avatar
desbrina desbrina is offline
Lightning Yellow Creator
 
Join Date: Feb 2011
Location: UK
Age: 26
Gender: Female
Nature: Quiet
Posts: 545
Is there a way to check for an internet connection before doing things such as mystery gift. When i try to access such features with no internet connection i get an error

Quote:
Exception: NoMethodError
Message: undefined method `size' for nil:NilClass
Sockets:368:in `connect'
Sockets:510:in `initialize'
Sockets:586:in `new'
Sockets:586:in `pbHttpRequest'
Sockets:562:in `pbPostData'
PokemonLoad * Edited:379:in `pbStartLoadScreen'
DebugIntro:6:in `main'
Main:37:in `mainFunctionDebug'
Mainin `mainFunction'
Mainin `pbCriticalCode'
Ideally i'd like for it to just say something along the lines of an internet connection is required, but at the moment it just crashes
__________________
Creator of


A Pokemon Yellow remake, using HGSS Tiles, and the ability to obtain all Kanto Pokemon as well as their previous/later evolutions.

Reply With Quote
  #2    
Old January 6th, 2014 (04:18 AM).
Stochastic's Avatar
Stochastic Stochastic is offline
 
Join Date: Dec 2013
Gender: Male
Posts: 112
If you take a look at the Sockets script, line 368, you'll see there's a call to SocketError.check in case of an error (e.g., no internet connection). The check method raises the error, and that means that you can catch it when you connect, i.e., when you create a socket.

Here I gave a script to an NPC that connects to a closed port on the local machine. The error should be similar, if not the same, to the one that gets raised when there's no internet connection.

socket = TCPSocket.new("127.0.0.1", 9999) rescue nil
if !socket
p("Cannot connect!")
end

In case of failure, the socket will get the value 'nil'. The "rescue nil" statement at the end will catch the error and assign 'nil' to the socket instead. We test for that and if it's true, we print out a message. p("something") will make a pop-up window with the message you provide. Now you can customize this to suit your needs.
Hope this helps!
__________________


Reply With Quote
  #3    
Old January 6th, 2014 (07:17 AM). Edited January 6th, 2014 by Alexandre.
Alexandre's Avatar
Alexandre Alexandre is offline
 
Join Date: Mar 2005
Age: 20
Nature: Lonely
Posts: 401
Quote originally posted by Stochastic:
If you take a look at the Sockets script, line 368, you'll see there's a call to SocketError.check in case of an error (e.g., no internet connection). The check method raises the error, and that means that you can catch it when you connect, i.e., when you create a socket.

Here I gave a script to an NPC that connects to a closed port on the local machine. The error should be similar, if not the same, to the one that gets raised when there's no internet connection.

socket = TCPSocket.new("127.0.0.1", 9999) rescue nil
if !socket
p("Cannot connect!")
end

In case of failure, the socket will get the value 'nil'. The "rescue nil" statement at the end will catch the error and assign 'nil' to the socket instead. We test for that and if it's true, we print out a message. p("something") will make a pop-up window with the message you provide. Now you can customize this to suit your needs.
Hope this helps!
This works but Ruby provides a better approach that allows you to skip the if block:
Code:
begin
  socket = TCPsocket.new('127.0.0.1','3000')
rescue
  print 'failed to connect to 127.0.0.1:3000'
end
rescue can take arguments which equate to the error being raised. You will have to check at http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/Socket.html for the specific error as its different foreach system. You'll want to use the ones for Windows when using RMXP. You can then print the specific error message:

Code:
begin
  socket = TCPSocket.new('127.0.0.1','3000')
rescue Errno::ECONNREFUSED => e
  print e.message
end
This would print the message "the target sockaddr was not listening for connections refused the connection request". This way of handling errors is great as it allows you to catch certain exceptions yet let others be fatal to the program (i.e allow the program to crash). You wouldn't want the game to crash if the connection was refused but you might want to let the game crash if there is a fatal ruby error that occurs during the creation of the socket (unlikely but you must take it into consideration)
Reply With Quote
  #4    
Old January 6th, 2014 (11:00 AM).
desbrina's Avatar
desbrina desbrina is offline
Lightning Yellow Creator
 
Join Date: Feb 2011
Location: UK
Age: 26
Gender: Female
Nature: Quiet
Posts: 545
thanks both of you, managed to get it to work
__________________
Creator of


A Pokemon Yellow remake, using HGSS Tiles, and the ability to obtain all Kanto Pokemon as well as their previous/later evolutions.

Reply With Quote
Reply
Quick Reply

Sponsored Links
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are UTC -8. The time now is 07:11 AM.