4throck
Enthusiast !
- Joined
- Jun 19, 2008
- Messages
- 3,502
- Reaction score
- 1,008
- Points
- 153
- Location
- Lisbon
- Website
- orbiterspaceport.blogspot.com
(updated)
For those beginning with Lua, here's a very basic example.
It will get some parameters and output them on the screen.
Go to scenarios\Cruising above Florida.scn and add "Script lua_test" to Environment.
The Lua example needs your vessels to be named like on that scenario.
Scenario (relevant part):
Then save the Lua code below to script\lua_test.lua. You can create this on a text editor like notepad.
Lua code:
That's it. It should help absolute beginners to get started.
The example above was edited to fix some errors based on a better example by kuddel.
His solution is quite clever, and I'll use it as a base for some more advanced stuff.
The discussion below may not make much sense right now.
For those beginning with Lua, here's a very basic example.
It will get some parameters and output them on the screen.
Go to scenarios\Cruising above Florida.scn and add "Script lua_test" to Environment.
The Lua example needs your vessels to be named like on that scenario.
Scenario (relevant part):
Code:
BEGIN_ENVIRONMENT
System Sol
Date MJD 54723.6753959915
Script lua_test
END_ENVIRONMENT
Then save the Lua code below to script\lua_test.lua. You can create this on a text editor like notepad.
Lua code:
Code:
-- get our vessels, names must match scenario
v1 = vessel.get_interface('GL-01S')
v2 = vessel.get_interface('ISS')
hv = oapi.get_objhandle('GL-01S')
hp = oapi.get_objhandle('ISS')
-- define where we are going to write
noteTop = oapi.create_annotation()
noteTop:set_pos(0.3, 0.05, 1,1)
noteTop:set_colour({r=1, g=0.7, b=0.3})
noteSide = oapi.create_annotation()
noteSide:set_pos(0.1, 0.5, 1,1)
noteSide:set_colour({r=1, g=0.7, b=0.3})
-- dumb infinite loop
i = 1
while i <= 5 do
-- get parameters
alt = v1:get_altitude()
pitch = v1:get_pitch()
bank = v1:get_bank()
yaw = v1:get_yaw()
angvel = 0 -- we will get this latter when get into vectors
dir = 0 -- we will get this latter when get into vectors
range = oapi.get_relativepos(hv,hp)
hvpos = oapi.get_globalpos(hv)
hppos = oapi.get_globalpos(hp)
-- calculate distance to ISS
range = vec.dist(hvpos,hppos)
range2 = math.floor(range/1000)
-- setup output text
intro = "Altitute:" .. alt
data= "Yaw " .. yaw .. "\nPitch " .. pitch .. "\nRotation ".. bank .."\nVel ".. angvel .."\nDir " .. dir .. "\nISS distance ".. range2
-- write to screen
noteTop:set_text (intro)
noteSide:set_text (data)
-- refresh update
proc.wait_sysdt(0.1)
end
That's it. It should help absolute beginners to get started.
The example above was edited to fix some errors based on a better example by kuddel.
His solution is quite clever, and I'll use it as a base for some more advanced stuff.
The discussion below may not make much sense right now.
Last edited: