Graphics

 View Only
  • 1.  Scripting Visibility by Integer

    Posted 02-06-2014 17:43
    We have two scripts running on our stocks full screens, both intending to do the same thing, but written by two different people. As we know, everyone has their own style of writing code, but going through these, they should both work fine.

    However, Version A will return a false 0 when the number is extremely close to zero (-0.24 through 0.24), and it incorrectly changes the visibility of one of our scenes to 0.

    Version B works correctly.

    My questions is not which one is "right," but rather why Version A is not working correctly, as it should be just fine (and in my opinion, is a cleaner code)

    Version A

    dim change as xpTextObject

    dim arrow as xpBaseObject

    dim redmat as xpMaterial

    dim gremat as xpMaterial

    dim changeval as double

    dim i as integer

    Engine.GetMaterialByName("Arrow - Red", redmat)

    Engine.GetMaterialByName("Arrow - Green", gremat)

    for i = 1 to 3

    Self.GetObjectByName("Change " & i, change)

    Self.GetObjectByName("Arrow " & i, arrow)

    changeval=Cint(change.Text)

    if changeval (is less than) 0 then

    arrow.Visible = True

    arrow.SetMaterial(0, redmat)

    arrow.SetMaterial(1, redmat)

    arrow.ScaleY = -(arrow.ScaleY)

    elseif changeval (is greater than) 0 then

    arrow.Visible = True

    arrow.SetMaterial(0, gremat)

    arrow.SetMaterial(1, gremat)

    arrow.ScaleY = Math.Abs(Arrow.ScaleY)

    elseif changeval = 0 then

    arrow.visible = False

    end if

    next i


    Version B

    dim CHNG as xpTextObject

    dim Arrow as xpBaseObject

    dim RedMat as xpMaterial

    dim GreMat as xpMaterial

    dim CHNGVAL as Double

    dim i as Integer

    Engine.GetMaterialByName("Arrow - Red", RedMat)

    Engine.GetMaterialByName("Arrow - Green", GreMat)

    for i = 1 to 4

    Self.GetObjectByName("Change " & i, CHNG)

    Self.GetObjectByName("Arrow " & i, Arrow)

    CHNGVAL = CHNG.Text

    If CHNGVAL (is less than) 0 then

    Arrow.SetMaterial(0, RedMat)

    Arrow.SetMaterial(1, RedMat)

    Arrow.ScaleY = -(Arrow.ScaleY)

    End If

    If CHNGVAL (is greater than) 0 then

    Arrow.SetMaterial(0, GreMat)

    Arrow.SetMaterial(1, GreMat)

    Arrow.ScaleY = Math.Abs(Arrow.ScaleY)

    End If

    If CHNGVAL = 0 then

    Arrow.Alpha = 0

    End If

    next i


    EDIT: replaced the actual greater than and less than symbols with (is greater/less than) in order to not confuse the forum.


  • 2.  RE: Scripting Visibility by Integer

    Posted 02-06-2014 18:00
    Hi Dan,

    The function CInt() will convert a text string to an Integer value. An integer, by definition, is a whole number, and therefore any decimal points will be truncated. It essentially rounds your value to the nearest whole number.

    CSng() will convert to text and preserve the decimal places.

    Try just replacing CInt with CSng

    #XPression


  • 3.  RE: Scripting Visibility by Integer

    Posted 02-06-2014 19:49
    The input values (fyi) are always truncated to two decimal points (x.xx). Is there any particular reason either of these scripts should use doubles, then?

    I get the feeling I should just rewrite this all to singles with CSng.

    EDIT: Also, I like Version A's use of elseif instead of opening up a new if/end if, so I'm planning on re-writing this script anyway. Just couldn't see why A was misbehaving. Cint/Csng makes perfect sense now that you point it out.

    #XPression