Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
ioan
Level 7

Converting C code to BrightScript question

I have some libraries that I want to translate from C and I have a question. What would be the best way to convert a function that has pointer as argument? Something like this:


void someFunction(int *num1, int *num2)
{
  *num1 = *num1 + 1;
  *num2 = *num2 + 2;
}


Here is what I came up with, but I wonder if there is a better solution:


function someFunction(num1, num2)
 return {
   num1: num1 + 1
   num2: num2 + 2
  }
end function


and then call it in code something like this:

num1 = 1
num2 = 2
a = someFunction(num1, num2)
num1 = a.num1
num2 = a.num2


Any better way to do this?
https://github.com/e1ioan/
http://rokucam.com
0 Kudos
2 Replies
renojim
Level 8

Re: Converting C code to BrightScript question

If you use the roInt object type and the ifInt interface you can modify the num1 and num2 in place:
Sub someSub(num1 as Object, num2 as Object)
   num1.setInt(num1 + 1)
   num2.setInt(num2 + 2)
End Sub

To use it:
num1 = CreateObject("roInt")
num2 = CreateObject("roInt")
num1.setInt(1)
num2.setInt(2)
someSub(num1,num2)
print num1,num2

Produces:
2         4

I'm not saying this is really a better way to do it since the first time you forget to use the setInt interface (e.g., num1 += 1 or just initializing it without setInt) you turn it into an Integer and it's no longer an roInt object and someSub will no longer modify it in place.

-JT
0 Kudos
ioan
Level 7

Re: Converting C code to BrightScript question

"renojim" wrote:
If you use the roInt object type and the ifInt interface you can modify the num1 and num2 in place:
Sub someSub(num1 as Object, num2 as Object)
   num1.setInt(num1 + 1)
   num2.setInt(num2 + 2)
End Sub

To use it:
num1 = CreateObject("roInt")
num2 = CreateObject("roInt")
num1.setInt(1)
num2.setInt(2)
someSub(num1,num2)
print num1,num2

Produces:
2         4

I'm not saying this is really a better way to do it since the first time you forget to use the setInt interface (e.g., num1 += 1 or just initializing it without setInt) you turn it into an Integer and it's no longer an roInt object and someSub will no longer modify it in place.

-JT

Thank you! This is very good to know.
https://github.com/e1ioan/
http://rokucam.com
0 Kudos