SketchyPhysicsWiki
(Added 'Rule to Keep in Mind')
Tag: Visual edit
(Made some improvements)
Tag: Visual edit
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 43: Line 43:
   
 
==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) needs to ensure that it is using Fiddle in 2.0 and Ruby DL for Ruby 1.8. In Ruby 2.0.0 Ruby DL was deprecated and replaced with Fiddle. Here is a snippet of code to replace in LazyScript: Change body pointer creators to use DL and Fiddle, depending on Ruby version.
 
Here is a brief summary of what to change for compatibility:
 
===1. Upgrade script for compatibility with Ruby 2.0.0 (see ruby changes).===
 
===2. 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 54: Line 50:
   
 
==Rule to Keep in Mind==
 
==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 functions.
+
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 feature SP versions. SP 3.5 has functions and events that should satisfy the user. To get cursor position, use ''$sketcyPhysucsToolInstance.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 methods in SP3.5 is no longer required because there are events and functions that can give you what you want.
+
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, ''$sketcyPhysucsToolInstance.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 ;)
 
That's about it ;)

Revision as of 19:30, 20 September 2014

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 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) needs to ensure that it is using Fiddle in 2.0 and Ruby DL for Ruby 1.8. In Ruby 2.0.0 Ruby DL was deprecated and replaced with Fiddle. Here is a snippet of code to replace in LazyScript: Change body pointer creators to use DL and 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, $sketcyPhysucsToolInstance.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 ;)