Good question. Like you said, this allows setting hostname to localhost when the script is unmodified (useful for sourcing from install without modification, like you may have been trying). If the script specifies ip or hostname, we will use that instead. That part is necessary if running locally, but your custom node needs to communicate with the outside world (node running on client machine).

Comm:
If on robot and only want to talk to the robot - localhost is fine.
if on robot and want to talk to both robot and client machine - must specify ros_ip or ros_hostname.