Array porting from SmallBASIC

New to FreeBASIC? Post your questions here.
Post Reply
misterc
Posts: 15
Joined: Sep 04, 2018 18:41

Array porting from SmallBASIC

Post by misterc »

I'm looking to port this code:

Code: Select all

' Palette as array
tomorrownight = [
rgb(67, 35, 146) ,
rgb(68, 115, 246),
rgb(236, 70, 195),
rgb(255, 179, 28),
rgb(255, 87, 80) ,
rgb(129, 40, 214)
]

' Single-line function to get a random array item
def randitem(myarray) = int(rnd*ubound(myarray))+1

' Get a random palette color
myColor = randitem(tomorrownight)

' Make art
drawWithColor(myColor)
So far I haven't had much luck as a beginner with FreeBasic.

Code: Select all

' Let's see, are these supposed to be returning Ulongs? 
dim paletteAarray as Ulong => 
{
    rgb(255,255,0),
    rgb(23,23,0),
    rgb(12,13,0)
}
Result:

Code: Select all

Expected expression in 'dim paletteAarray as Ulong =>'
Expected End-of-Line, found '{' in '{'
Invalid data types, found ',' in 'rgb(255,255,0),'
Invalid data types, found ',' in 'rgb(23,23,0),'
Invalid data types in 'rgb(12,13,0)'
Expected End-of-Line, found '}' in '}'
OK, I could use some pointers here, seems like maybe more than one thing is wrong :wink:
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Array porting from SmallBASIC

Post by srvaldez »

you need line continuation if you want to split an expression

Code: Select all

dim as Ulong paletteAarray(...) => _
{ _
    rgb(255,255,0), _
    rgb(23,23,0), _
    rgb(12,13,0) _
}
misterc
Posts: 15
Joined: Sep 04, 2018 18:41

Re: Array porting from SmallBASIC

Post by misterc »

Thanks, that helped. I'm not having any luck passing the array to a function though. I'm not sure what I'm doing wrong with this first line:

Code: Select all

function randitem ( myarray() ) as integer
  return 2 ' Just to test
end function
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Array porting from SmallBASIC

Post by srvaldez »

you need to specify the type of the array

Code: Select all

function randitem ( myarray() as ulong) as integer
misterc
Posts: 15
Joined: Sep 04, 2018 18:41

Re: Array porting from SmallBASIC

Post by misterc »

Argument count mismatch in the first "function randitem..." line?

Code: Select all

' Create array of rgb colors
dim as Ulong paletteArray(...) => _
{ _
    rgb(255,255,0), _
    rgb(23,23,0), _
    rgb(12,13,0)  _
} _

' Randomly pick out an array value
function randitem (myarray() as ulong) as integer
    dim item as integer
    dim myarray() as ulong
    item = int(rnd*ubound(myarray))
    item = item + 1
    return item
end function

Code: Select all

graph01.fba(121) error 1: Argument count mismatch in 'function randitem (myarray() as ulong) as integer'
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Array porting from SmallBASIC

Post by srvaldez »

why don't you post the small basic code that you are trying to convert to FB?
misterc
Posts: 15
Joined: Sep 04, 2018 18:41

Re: Array porting from SmallBASIC

Post by misterc »

It was posted at the start of the thread...
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Array porting from SmallBASIC

Post by grindstone »

My suggestion:

Code: Select all

' Palette as array
Dim As ULong tomorrownight(1 To ...) = { _
RGB(67, 35, 146) , _
RGB(68, 115, 246), _
RGB(236, 70, 195), _
RGB(255, 179, 28), _
RGB(255, 87, 80) , _
RGB(129, 40, 214)}

' Get a random palette color
Dim As ULong myColor = tomorrownight(Int(Rnd*UBound(tomorrownight))+1)

' Make art
drawWithColor(myColor)
misterc
Posts: 15
Joined: Sep 04, 2018 18:41

Re: Array porting from SmallBASIC

Post by misterc »

Cool, that code makes sense and it's helpful to see as I get started. Thank you.
Post Reply