All Projects → TeWu → krpc-rb

TeWu / krpc-rb

Licence: GPL-3.0 license
Ruby client library for kRPC

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to krpc-rb

Kerbalism
Hundreds of Kerbals were killed in the making of this mod.
Stars: ✭ 142 (+446.15%)
Mutual labels:  kerbal-space-program
SDHI ServiceModuleSystem
Parts pack for Kerbal Space Program that consists of a stockalike Service Module and accessories inspired by NASA’s Orion MPCV, and designed specifically for use with the stock Mk1-3 Command Pod.
Stars: ✭ 24 (-7.69%)
Mutual labels:  kerbal-space-program
Custom-Asteroids
A KSP mod that lets users control where asteroids appear
Stars: ✭ 13 (-50%)
Mutual labels:  kerbal-space-program
AvionicsSystems
MOARdV's Avionics Systems for Kerbal Space Program - a new generation of IVA enhancement.
Stars: ✭ 40 (+53.85%)
Mutual labels:  kerbal-space-program
launch-pad
A Mod Manager for Kerbal Space Program on macOS
Stars: ✭ 19 (-26.92%)
Mutual labels:  kerbal-space-program
KSP Contract Window
Contract Display Window For KSP
Stars: ✭ 13 (-50%)
Mutual labels:  kerbal-space-program
Astrogator
A space-navigational aide for Kerbal Space Program
Stars: ✭ 25 (-3.85%)
Mutual labels:  kerbal-space-program
Orbital-Science
DMagic Orbital Science: KSP science parts and modules.
Stars: ✭ 39 (+50%)
Mutual labels:  kerbal-space-program
KML
Kerbal Markup Lister - A persistence file editor for Kerbal Space Program
Stars: ✭ 26 (+0%)
Mutual labels:  kerbal-space-program
KSP-Recall
Recall for KSP blunders, screw ups and borks.
Stars: ✭ 19 (-26.92%)
Mutual labels:  kerbal-space-program
KerbalHealth
KSP mod that adds health management.
Stars: ✭ 13 (-50%)
Mutual labels:  kerbal-space-program

kRPC-rb Gem Version Build Status

kRPC-rb is a Ruby client library for kRPC, a Kerbal Space Program mod that allows you to control KSP from external scripts running outside of the game.

kRPC-rb image

Installation

gem install krpc

or install the latest pre-release version (if available): gem install krpc --pre

Basic usage

require 'krpc'
client = KRPC.connect(name: "client name here")
vessel = client.space_center.active_vessel
ctrl = vessel.control
ctrl.sas = true
ctrl.sas_mode = :stability_assist
ctrl.throttle = 1
puts "Launching #{vessel.name}!"
ctrl.activate_next_stage
client.close

Most of the API is very similar to what can be found in (official) Python client library. So official documentation at http://krpc.github.io/krpc/ is definitely a good read. The rest of this file describes few differences there are between Ruby and Python client libraries.

Connecting and disconnecting

When you are in REPL, you can connect to kRPC server in this way:

client = KRPC.connect(name: {name for the client}, host: {kRPC server host}, rpc_port: {kRPC server rpc port}, stream_port: {kRPC server stream port})
# use client here...
client.close

All of the KRPC.connect's arguments are optional, so client = KRPC.connect might be enough for you. Alternatively you can be more explicit (yet still obtain the same result):

client = KRPC::Client.new( {The same argument list as KRPC.connect has} ).connect!
# use client here...
client.close

If you are writing a script, you can pass a block into KRPC.connect, Client#connect or Client#connect! methods. Connection to kRPC server is closed at the end of the block.

KRPC.connect do |client|
# do something with client here...
end # closes connection

Data structures mapping

kRPC server (and KSP itself) is written in C#. This means that during communication with the server there must be some data structure mapping being done. Most of the mappings are pretty obvious: numbers are mapped to Floats and Integers, Strings to Strings, Lists to Arrays, Dictionaries to Hashes etc.

It may be less obvious that Enum values are mapped to Symbols:

client.space_center.active_vessel.situation # => :orbiting
client.space_center.active_vessel.control.sas_mode = :prograde

To see all values for enum, you can call method that expects enum argument or returns enum value with _doc suffix. Alternatively you can print the hash that represents given enum:

puts KRPC::Gen::SpaceCenter::SASMode # => {:stability_assist=>0, :maneuver=>1, :prograde=>2, :retrograde=>3, :normal=>4, :anti_normal=>5, :radial=>6, :anti_radial=>7, :target=>8, :anti_target=>9}

Tuples are mapped to Arrays:

client.space_center.active_vessel.flight.center_of_mass # => [-0.0015846538639403215, 0.0005474663704413168, 0.000849766220449432]

Get your fingers dirty

The best way to explore the API is to run REPL and try what each method does for yourself. I highly recommend using Pry as REPL. This way you can ls any object you receive and see what methods you can call on it. When you want to know more about specific method, then just stuck _doc at the end of it's name and press enter:

[29] pry(main)> client.space_center.transform_position_doc
SpaceCenter.transform_position(
	position :Array[Float, Float, Float], - Position vector in reference frame from.
	from :ReferenceFrame, - The reference frame that the position vector is in.
	to :ReferenceFrame - The reference frame to covert the position vector to.
) :Array[Float, Float, Float] - The corresponding position vector in reference frame to.

 Converts a position vector from one reference frame to another.
=> nil

I recommend ending the line with ; to suppress printing return value (the => nil line at the end). If you want doc for method whose name ends with a = sign, you can put _doc before the =. Alternatively use Object#send, like in: client.space_center.send "active_vessel=_doc".

Combination of lss and _docs should teach you API in no time (also don't be surprised if you have a lot of fun with it too :))

kRPC-rb in-REPL documentation example

Streaming

A stream repeatedly executes a function on the server, with a fixed set of argument values. It provides a more efficient way of repeatedly getting the result of calling function on the server, without having to invoke it directly – which incurs communication overheads.

To create a stream, call a method with _stream suffix. This will return KRPC::Streaming::Stream instance. You can call get (or value) on the Stream instance to get the recent value received by this stream. To deactivate the stream call remove (or close) on the Stream instance.

Example without streaming:

vessel = client.space_center.active_vessel
refframe = vessel.orbit.body.reference_frame
loop do
  puts vessel.position(refframe)
end

Equivalent example with streaming:

vessel = client.space_center.active_vessel
refframe = vessel.orbit.body.reference_frame
pos_stream = vessel.position_stream(refframe)
loop do
  puts pos_stream.get
end
pos_stream.remove #note: dead code - just as an example

Want to know more?

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].