Confused About Tables

Find quick help here to get you started with Hollywood
ocean77
Posts: 111
Joined: Mon Jan 28, 2019 8:34 pm

Confused About Tables

Post by ocean77 »

In an exercise on how to work with arrays/ tables I thought I'd be able to store these constants:

Code: Select all

Const #TEXT_COL = $000020
Const #DECO_COL = $F08000
Const #PAPER_COL = $F0F0F0
Const #OPTS_LOCOL = $0050A0
Const #OPTS_HICOL = $D02020
More conveniently in a table like this:

Code: Select all

Dim Const #PALETTE = {$000020, $F08000, $F0F0F0, $0050A0, $D02020}
And call the color I need with something like:

Code: Select all

SetFontColor(#PALETTE[3])
Apparently this is not the way to do it. Hollywood complains about a missing opening square bracket in the the line where the array is declared. But reading the documentation I can't understand how to properly write the code I need.
jalih
Posts: 280
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Confused About Tables

Post by jalih »

I think Hollywood constants are just values, so you cannot do that and need to use something like:

Code: Select all

Const #TEXT_COL = $000020
Const #DECO_COL = $F08000
Const #PAPER_COL = $F0F0F0
Const #OPTS_LOCOL = $0050A0
Const #OPTS_HICOL = $D02020

PALETTE = {#TEXT_COL, #DECO_COL, #PAPER_COL, #OPTS_LOCOL, #OPTS_HICOL}
ocean77
Posts: 111
Joined: Mon Jan 28, 2019 8:34 pm

Re: Confused About Tables

Post by ocean77 »

I've probably misunderstood the concept... again. Oh, well...
:roll:
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Confused About Tables

Post by Allanon »

Hi, tables are very powerful, why don't you do something like this?

Your code

Code: Select all

Const #TEXT_COL = $000020
Const #DECO_COL = $F08000
Const #PAPER_COL = $F0F0F0
Const #OPTS_LOCOL = $0050A0
Const #OPTS_HICOL = $D02020
Table alternative:

Code: Select all

palette =
{ text_col = $000020,
  deco_col = $F08000,
  paper_col = $F0F0F0,
  opts_locol = $0050A0,
  opts_hicol = $D02020 }
  
And access those values with:

Code: Select all

DebugPrint(palette.text_col)
No more constants but the readibility is improved and you can process all the colors iterating the palette table, for example if you need to darken all the colors or you can also load/save the entire table from/to files :)
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
ocean77
Posts: 111
Joined: Mon Jan 28, 2019 8:34 pm

Re: Confused About Tables

Post by ocean77 »

Allanon wrote: Fri Jul 07, 2023 12:22 pm No more constants but the readibility is improved and you can process all the colors iterating the palette table, for example if you need to darken all the colors or you can also load/save the entire table from/to files :)
Oh... So, the problem was the constant part?
I was just somehow under the impression that constants would conserve memory and also dimension-ing the array would do the same thing as long as you don't need to expand it dynamically.
But I easily misunderstand things so...

Thanks for your input. :)
ocean77
Posts: 111
Joined: Mon Jan 28, 2019 8:34 pm

Re: Confused About Tables

Post by ocean77 »

Check out this simple Hello World script:

Code: Select all

@DISPLAY{WIDTH = 960, HEIGHT = 540, MODE = "WINDOWED", TITLE = "Title", COLOR = #BLACK}

SetFont(#SANS, 45)
SetFontStyle(#ANTIALIAS)
SetFontColor(#WHITE)

_data = {30, 60, "One", 100, 250, "Two", 270, 400, "Three"}

TextOut(_data[0], _data[1], _data[2])
Wait(50)
TextOut(_data[3], _data[4], _data[5])
Wait(50)
TextOut(_data[6], _data[7], _data[8])
Wait(50)
Cls(#BLACK)

CreateTextObject(1, "Hello World!")
DisplayTextObjectFX(1, #CENTER, #CENTER, {TYPE = #DISSOLVE, SPEED = 2})
Wait(50)


EscapeQuit(True)

Repeat
    WaitEvent
Forever
This works as expected. But why can't I nest the TextOut instructions in a for-next loop like this:

Code: Select all

For x = 0 to 9

TextOut(_data[x], _data[x], _data[x])
Wait(50)
TextOut(_data[x], _data[x], _data[x])
Wait(50)
TextOut(_data[x], _data[x], _data[x])
Wait(50)

Next x
It works until _data[x] is a string, but I thought the x would just indicate the position in the array.
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Confused About Tables

Post by Flinx »

ocean77 wrote: Mon Jul 10, 2023 1:22 am

Code: Select all

_data = {30, 60, "One", 100, 250, "Two", 270, 400, "Three"}

For x = 0 to 9

TextOut(_data[x], _data[x], _data[x])
Wait(50)
TextOut(_data[x], _data[x], _data[x])
Wait(50)
TextOut(_data[x], _data[x], _data[x])
Wait(50)

Next x
It works until _data[x] is a string, but I thought the x would just indicate the position in the array.
First, the last table element has index 8, not 9.
Second, Next x ist no Hollywood syntax. This x will give you a "Function x() not found!" after the loop.
Then look what the loop does.
x=0: TextOut(30, 30, 30)
x=1: TextOut(60, 60, 60)
x=2: TextOut("One", "One", "One") --> Number expected in argument 1!

Solution 1:

Code: Select all

For x = 0 To 6 Step 3
	TextOut(_data[x], _data[x+1], _data[x+2])
	Wait(50)
Next 
Solution 2:

Code: Select all

_data = {{30, 60, "One"}, {100, 250, "Two"}, {270, 400, "Three"}}
For x = 0 To 2
	TextOut(_data[x][0], _data[x][1], _data[x][2])
	Wait(50)
Next 
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Confused About Tables

Post by Flinx »

I would suggest that you use the Debugprint statement to find such bugs. In this case you only have to insert a Debugprint with the TextOut arguments and you see one of the problems.

Code: Select all

Debugprint(_data[x], _data[x], _data[x])
TextOut(_data[x], _data[x], _data[x])
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Confused About Tables

Post by Allanon »

ocean77 wrote: Fri Jul 07, 2023 10:13 pm
Allanon wrote: Fri Jul 07, 2023 12:22 pm No more constants but the readibility is improved and you can process all the colors iterating the palette table, for example if you need to darken all the colors or you can also load/save the entire table from/to files :)
Oh... So, the problem was the constant part?
I was just somehow under the impression that constants would conserve memory and also dimension-ing the array would do the same thing as long as you don't need to expand it dynamically.
But I easily misunderstand things so...

Thanks for your input. :)
Well, constants DO conserve memory (and should have a faster access) but how many bytes you are able to save?
Try to build a simple script using constants and a table of variables and you can compare:
- What's faster: constants or tables, and by what degree
- Check the compiled programs size and see how many bytes you saved on disk
- Check the loaded programs how much memory they take

I prefer flexibility instead of gaining 10ms or instead of saving some Kb of free memory but I understand that depends on your target machine, if it is low, very low on memory, yes, you should do all you can do to save memory, in this case every millisecond gained in the execution code counts :)
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Confused About Tables

Post by Allanon »

ocean77 wrote: Mon Jul 10, 2023 1:22 am Check out this simple Hello World script:

Code: Select all

@DISPLAY{WIDTH = 960, HEIGHT = 540, MODE = "WINDOWED", TITLE = "Title", COLOR = #BLACK}

SetFont(#SANS, 45)
SetFontStyle(#ANTIALIAS)
SetFontColor(#WHITE)

_data = {30, 60, "One", 100, 250, "Two", 270, 400, "Three"}

TextOut(_data[0], _data[1], _data[2])
Wait(50)
TextOut(_data[3], _data[4], _data[5])
Wait(50)
TextOut(_data[6], _data[7], _data[8])
Wait(50)
Cls(#BLACK)

CreateTextObject(1, "Hello World!")
DisplayTextObjectFX(1, #CENTER, #CENTER, {TYPE = #DISSOLVE, SPEED = 2})
Wait(50)


EscapeQuit(True)

Repeat
    WaitEvent
Forever
This works as expected. But why can't I nest the TextOut instructions in a for-next loop like this:

Code: Select all

For x = 0 to 9

TextOut(_data[x], _data[x], _data[x])
Wait(50)
TextOut(_data[x], _data[x], _data[x])
Wait(50)
TextOut(_data[x], _data[x], _data[x])
Wait(50)

Next x
It works until _data[x] is a string, but I thought the x would just indicate the position in the array.
@Flinx is right but you should try, whenever possible, to take advantage of the tables power :)
Why not try this way?

Code: Select all

_data = { { x=30, y=60, txt="One" }, {x=100, y=250, txt="Two"}, {x=270, y=400, txt="Three"}}

For Local i = 0 To 2
  TextOut(_data[i].x, _data[i].y, _data[i].txt)
Next
As you can see named entries are easier to remember and to use, and less prone to errors since you have to specify the table field you need.
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
Post Reply