Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Komag
Roku Guru

trying to print a simple array value (string), not working

This should be pretty simple:
	testArray = [X, Y, Z]
print testArray[0] ' this only prints <bsTypedValue: >

How can I get it to print out the X? What exactly is going on?
I tried:
	print stri(testArray[0]) ' error, Type Mismatch
myString = testArray[0]
print stri(myString) ' error, Type Mismatch

I also tried various versions of ToStr() such as ToStr(testArray[0]) or testArray[0].ToStr() with no luck, got dot operator error.

If my array has quotation marks it works just fine, prints X just like I want, but for some other reasons I REALLY don't want to add all the " marks to the original array
	testArray = ["X", "Y", "Z"]
print testArray[0] ' this prints X

What I really want to do is check the value, not necessarily print it, such as:
	testArray = [X, Y, Z]
if testArray[0] = X then dothisdothat

but it doesn't work. It works only like this:
	testArray = ["X", "Y", "Z"]
if testArray[0] = "X" then dothisdothat

Is there some array command to just set all the values to be strings? Or is there a way to make ToStr() or Stri() work for me?

Or if I could just know what X actually currently is (since it's apparently not a string when I don't use the "), so I can properly check for it.
0 Kudos
12 REPLIES 12
Komag
Roku Guru

Re: trying to print a simple array value (string), not worki

What is X? This gives me nothing:
	testArray = [X, Y, Z]
print type(testArray) ' prints "roArray"
print type(testArray[0]) ' prints nothing, blank
myString = testArray[0]
print type(myString) ' prints nothing, blank
print myString ' prints "<bsTypedValue: >", what is that?
print "Hi Garrett" ' prints "Hi Garrett"


EDIT - I made a workaround, by assigning variables beforehand:
	X = "Xena"
Y = "Yuma"
Z = "Zelda"
testArray = [X, Y, Z]
print type(testArray) ' prints "roArray"
print type(testArray[0]) ' "roString"
myString = testArray[0]
print type(myString) ' prints "roString"
print myString ' prints "Xena"
print "Hi Garrett" ' prints "Hi Garrett"
if testArray[0] = "Xena" then print "Yes, it's Xena" ' prints "Yes, it's Xena"


SOLVED (sort of)
0 Kudos
EnTerr
Roku Guru

Re: trying to print a simple array value (string), not worki

You can use "var" in console to get better understanding of the situation:
BrightScript Debugger>  testArray = [X, Y, Z]
BrightScript Debugger> var
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=2
x &h0000 <uninitialized> val:Uninitialized
testarray &h0010 bsc:roArray, refcnt=1
y &h0000 <uninitialized> val:Uninitialized
z &h0000 <uninitialized> val:Uninitialized

Apparently "<bsTypedValue: >" is the output for uninitialized variable and all kinds of weird things come with that (for example "print type(y)" returns empty string). It's not array related, same would happen if you do "var1 = unknownVar2".
0 Kudos
destruk
Binge Watcher

Re: trying to print a simple array value (string), not worki

Correct - initially X is invalid unless you assign a value to it. You will also run into many problems if you initialize it as a string (code expects it to be a string) and then you decide to give it an integer value, if you try to use an integer as a url variable (http.addparam), or other various type casting problems. As each value/index/member of an array can be anything or any type, there isn't an easy way I know of to assure it's the desired type without using a for each or looping through and converting everything in it. Brightscript is one of the more stricter languages when it comes to types.
ie
For each item in array
item=item.toStr()
next

That might do it for you before accessing the array elements as the required string, or convert them to a string before 'push'ing them into the array to begin with.
0 Kudos
Komag
Roku Guru

Re: trying to print a simple array value (string), not worki

Ah, that helps, I understand it better now.

So without the quotes, even something like HELLO is just a variable that hasn't been assigned a value, and only "HELLO" is a string right off the bat. Thanks to you both 🙂
0 Kudos
EnTerr
Roku Guru

Re: trying to print a simple array value (string), not worki

"Komag" wrote:
So without the quotes, even something like HELLO is just a variable that hasn't been assigned a value, and only "HELLO" is a string right off the bat.

Yes indeed.
To get slightly formal, what you were using is called "array literal" (think of it as saying "here is the array i want, literally - right here"). It is a way to construct (specify) an array (list) of values and is of the form
, expr2 , ... , exprN ]
- where expr1 ... exprN are expressions that will be calculated each first and then the array will be created from that. That has the benefits of being able to do things like:
BrightScript Debugger> ? [string(5, "*"), atn(1) * 4]
*****
3.14159
0 Kudos
Komag
Roku Guru

Re: trying to print a simple array value (string), not worki

Related question, say I have a string "map" and I want to convert to variable map, is there a way to do this?

Basically I want the opposite of ToStr(), I want to "unstring" a string!
0 Kudos
RokuMarkn
Visitor

Re: trying to print a simple array value (string), not worki

"Komag" wrote:
Related question, say I have a string "map" and I want to convert to variable map, is there a way to do this?


No, that doesn't really make sense. Variable names are arbitrary and have no intrinsic meaning.

"Komag" wrote:

Basically I want the opposite of ToStr(), I want to "unstring" a string!


That's a different question. ToStr() converts and integer to a string. ToInt() or the Stri() global function do the opposite conversion, from string to integer.

--Mark
0 Kudos
Komag
Roku Guru

Re: trying to print a simple array value (string), not worki

But removing the quotes could be very useful for me. I'd like to store an array of sprites, such as:
	IF item = "wall"
item = {
name: "wall",
blocker: TRUE,
sprite: bitmapset.animations.sprite_brickwall2
}
ELSE IF item = "pillar"
item = {
name: "pillar",
blocker: TRUE,
sprite: bitmapset.animations.sprite_pillar1
}
ELSE IF item = "door"
item = {
name: "door",
blocker: TRUE,
openable: TRUE,
sprite: bitmapset.animations.sprite_door1
}

(part of a buildRoom function) - later, a function calls:
compositor.NewAnimatedSprite(j*64, i*64, theCurrentRoom[i][j].sprite)

but this doesn't work. If I could save the sprite info as:
sprite: "bitmapset.animations.sprite_brickwall2"

and then change the string to just be not a string (just remove the quotes in essence) later on, it would work great and be really useful.

Is there some other way I could save and refer to the sprite information "bitmapset.animations.sprite_door1"? According to the "ifCompositor" sdkdocs, that parameter is the region/region array, which is an roRegion or array of roRegions (for animated sprites). Maybe I could define those somewhere else with variables, and refer to that in my code? But my bitmapset and animations are in an xml file, totally different syntax from
CreateObject("roRegion", Object bitmap, Integer x, Integer y,Integer width, Integer height)
0 Kudos
RokuMarkn
Visitor

Re: trying to print a simple array value (string), not worki

"Komag" wrote:
later, a function calls:
compositor.NewAnimatedSprite(j*64, i*64, theCurrentRoom[i][j].sprite)

but this doesn't work.


If theCurrentRoom was set equal to item after the if statement you quoted, then it should work. Can you expand on what doesn't work? The approach you're pursuing, of removing quotes from a string, is not the right direction.

--Mark
0 Kudos