SketchyPhysicsWiki
(Added compatibility page.)
Tag: Visual edit
 
(Fixed typo)
Tags: apiedit, Visual edit
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
If you haven't noticed a lot of scripted models will operate in SU 2013 and below only. Thats because SU2013 and SU2014 use different Ruby versions. SU2013 and lower use Ruby 1.8.x, while SU2014 uses Ruby 2.0.0. There were Ruby implementation changes since 1.8.x. Follow steps below for upgrading instructions:
+
If you haven't noticed, many scripted models will operate in SU 2013 and below. This is due to SU2013 and SU2014 using different Ruby versions. SU2013 and lower use Ruby 1.8.x, while SU2014 uses Ruby 2.0.0. There were Ruby implementation changes since 1.8.x. Make sure your script is updated to be compatible with the following changes:
   
 
==Ruby Changes==
 
==Ruby Changes==
Line 24: Line 24:
 
puts 'hello'
 
puts 'hello'
 
when 2
 
when 2
puts puts 'goodbye'
+
puts 'goodbye'
 
end
 
end
   
Line 33: Line 33:
 
* Bad (.key works in SU2014 only)
 
* Bad (.key works in SU2014 only)
 
count = my_hash.key('hello')
 
count = my_hash.key('hello')
* Good (Works in both versions)
+
* Good (works in both versions)
 
if RUBY_VERSION =~ /1.8/
 
if RUBY_VERSION =~ /1.8/
 
count = my_hash.index('hello')
 
count = my_hash.index('hello')
Line 39: Line 39:
 
count = my_hash.key('hello')
 
count = my_hash.key('hello')
 
end
 
end
* Also good (One liner):
+
* Also good (one liner):
 
count = RUBY_VERSION =~ /1.8/ ? my_hash.index('hello') : my_hash.key('hello')
 
count = RUBY_VERSION =~ /1.8/ ? my_hash.index('hello') : my_hash.key('hello')
   
 
==Making LazyScript Compatible==
 
==Making LazyScript Compatible==
For some advanced scripted models that use LazyScript making their code compatible is not enough because (1) LazyScript needs to be made compatible too, and (2) LazyScript uses Ruby DL which is only available in Ruby 1.8.x categories. In Ruby 2.0.0 Ruby DL was deprecated and replaced with Fiddle.
+
For some advanced scripted models that use LazyScript making their code compatible for SU2014 is not enough because (1) LazyScript itself needs to be updated with Ruby changes, and (2) LazyScript needs to ensure that it is using Fiddle for Ruby 2.0 and DL for Ruby 1.8. In Ruby 2.0.0 Ruby DL was deprecated and replaced with Fiddle. Change body pointer creators to use DL or Fiddle, depending on Ruby version:
 
Here is a brief summary of what to change for compatibility:
 
===Upgrade script for compatibility using new Ruby scripting rules.===
 
===Change body pointer creators to use DL and Fiddle, depending on Ruby version.===
 
 
* From this
 
* From this
 
body_ptr = DL::PtrData.new(address)
 
body_ptr = DL::PtrData.new(address)
Line 53: Line 49:
 
body_ptr = RUBY_VERSION =~ /1.8/ ? DL::PtrData.new(address) : Fiddle::Pointer.new(address)
 
body_ptr = RUBY_VERSION =~ /1.8/ ? DL::PtrData.new(address) : Fiddle::Pointer.new(address)
   
  +
==Rule to Keep in Mind==
Thats about it ;)
 
  +
In SP3.2 many advanced scripted models relied on modifying SP content to achieve something that wasn't achievable via the standard SP API. For instance, many would modify ''onMouseMove(x, y, view, flags)'' function to get cursor position, and some would modify the ''draw(view)'' for a better drawing features.
  +
  +
Modifying SP content is a bad practice because it may break compatibility with future SP versions. SP 3.5 has functions and events that should give a developer what he/she wants. For instance, to get cursor position use, ''$sketchyPhysicsToolInstance.cursorPos''. If you want to improve the draw tool, simply use the ''onDraw { |view, bb| }'' event rather than modifying the ''draw(view)'' function. If you want something to be done in the nextFrame, don't modify the nextFrame function, use ''onPreFrame'', ''onTick'', or ''onPostFrame'' events. Same goes to start and reset physics; use ''onStart'', and ''onEnd'' events. Modifying SP content in SP3.3+ is no longer required because there are events and functions that should give you what you want.
  +
 
That's about it ;)

Latest revision as of 05:44, 12 April 2015

If you haven't noticed, many scripted models will operate in SU 2013 and below. This is due to SU2013 and SU2014 using different Ruby versions. SU2013 and lower use Ruby 1.8.x, while SU2014 uses Ruby 2.0.0. There were Ruby implementation changes since 1.8.x. Make sure your script is updated to be compatible with the following changes:

Ruby Changes[]

Prohibited use of spaces between method name and the parentheses[]

  • Bad
 Geom::Point3d.new (1,2,3)
  • Good
 Geom::Point3d.new(1,2,3)

Prohibited use of colons (:) in case-when statement. Use semicolons (;) or new line.[]

  • Bad
 case count
 when 1: puts 'hello'
 when 2: puts 'goodbye'
 end
  • Good
 case count
 when 1; puts 'hello'
 when 2; puts 'goodbye'
 end
  • Also good
 case count
 when 1
   puts 'hello'
 when 2
   puts 'goodbye'
 end

Replaced Hash.#index with Hash.#key[]

 my_hash = { 1 => 'hello', 2 => 'goodbye' }
  • Bad (.index works in SU2013 and below only)
 count = my_hash.index('hello')
  • Bad (.key works in SU2014 only)
 count = my_hash.key('hello')
  • Good (works in both versions)
 if RUBY_VERSION =~ /1.8/
   count = my_hash.index('hello')
 else
   count = my_hash.key('hello')
 end
  • Also good (one liner):
 count = RUBY_VERSION =~ /1.8/ ? my_hash.index('hello') : my_hash.key('hello')

Making LazyScript Compatible[]

For some advanced scripted models that use LazyScript making their code compatible for SU2014 is not enough because (1) LazyScript itself needs to be updated with Ruby changes, and (2) LazyScript needs to ensure that it is using Fiddle for Ruby 2.0 and DL for Ruby 1.8. In Ruby 2.0.0 Ruby DL was deprecated and replaced with Fiddle. Change body pointer creators to use DL or Fiddle, depending on Ruby version:

  • From this
 body_ptr = DL::PtrData.new(address)
  • To this
 body_ptr = RUBY_VERSION =~ /1.8/ ? DL::PtrData.new(address) : Fiddle::Pointer.new(address)

Rule to Keep in Mind[]

In SP3.2 many advanced scripted models relied on modifying SP content to achieve something that wasn't achievable via the standard SP API. For instance, many would modify onMouseMove(x, y, view, flags) function to get cursor position, and some would modify the draw(view) for a better drawing features.

Modifying SP content is a bad practice because it may break compatibility with future SP versions. SP 3.5 has functions and events that should give a developer what he/she wants. For instance, to get cursor position use, $sketchyPhysicsToolInstance.cursorPos. If you want to improve the draw tool, simply use the onDraw { |view, bb| } event rather than modifying the draw(view) function. If you want something to be done in the nextFrame, don't modify the nextFrame function, use onPreFrame, onTick, or onPostFrame events. Same goes to start and reset physics; use onStart, and onEnd events. Modifying SP content in SP3.3+ is no longer required because there are events and functions that should give you what you want.

That's about it ;)