December 04, 2020, 02:55:29 PM

### Author Topic: Timing in BB  (Read 1019 times)

#### windman

• Jr. Member
• Posts: 30
##### Timing in BB
« on: January 30, 2019, 11:13:21 PM »
Look at this code
Code: [Select]
`T=1:For X=1 To 5:For Y=1 To 5:ST(T)=MilliSecs():TI(T)=Rand(18000,50000):T=T+1:Next:Next:T=1While Not KeyDown(1).ag For X=1 To 5:For Y=1 To 5If MilliSecs()-ST(T)>=TI(T) NC(T)=Rand(0,2):FD(T)=(NC(T)>0):ST(T)=MilliSecs():TI(T)=Rand(18000,50000):;Goto agEndIfIf NC(T)=0 Then Color 0,0,0:Rect XX+(X-1)*(SQD+SSP),YY+(Y-1)*(SQD+SSP),SQD,SQD,1Color 255*(NC(T)=0)+250*(NC(T)=1)+20*(NC(T)=2),255*(NC(T)=0)+20*(NC(T)=1)+250*(NC(T)=2),255*(NC(T)=0)Rect XX+(X-1)*(SQD+SSP),YY+(Y-1)*(SQD+SSP),SQD,SQD,FD(T)T=T+1:If T>25 Then T=1Next:Next;VWaitWend`
I'm trying to get the dots / squares to change color at a random interval in milliseconds.
I'm using as much as 18 secs but when the color changes come, the interval is not happening according to the programmed code.
Am I coding incorrectly or am I missing some nuance about millisecs()

An 18 second programmed wait is not what I wanted to use.

#### RemiD

• Hero Member
• Posts: 1081
##### Re: Timing in BB
« Reply #1 on: February 01, 2019, 03:26:54 PM »
I give you a prize : either for obfuscated code or for badly indented code, your choice

(you can use debuglog("here!")  to see how your code is flowing and debuglog(value) to see how each value is altered depending on your instructions, step by step...)
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

• Hero Member
• Posts: 517
##### Re: Timing in BB
« Reply #2 on: February 01, 2019, 05:31:07 PM »
This is the perfect example of when you shouldn't use goto. The moment the If statement becomes true, you end up in an endless loop. Ditch the goto, use If/Else/Endif structure instead. Probably other errors, but I'm not at home right now to check.
------------------------------------------------
8 rabbits equals 1 rabbyte.

#### 3DzForMe

• Hero Member
• Posts: 1136
##### Re: Timing in BB
« Reply #3 on: February 02, 2019, 10:29:36 AM »
Yep, brilliant obfuscation there. Too hard to analyze. Break it up into separate lines.

#### Holzchopf

• Full Member
• Posts: 141
##### Re: Timing in BB
« Reply #4 on: February 03, 2019, 10:44:19 AM »
Look at this code
Ugh... Multistatement lines, no vars longer than two chars - that's some real 70's programming style you've got there

To be honest:
- give your variables good names. You don't want to put a comment to every usage of a var just to explain what's going on in this assignment or comparison. Variable names should be self-explanatory.
- increase readability of your code by doing without the ":" statement concatenation, putting every statement on one line and using correct indentation.*
- and - very important - if you want help, always post a minimal runnable example. Don't omit things like variable initialisation.

the interval is not happening according to the programmed code.

That's never true: A computer always does what you programmed - not what you intended. Keep that in mind, it will save you a lot of nerves and help you focus on your code when debugging oddly behaving routines.

(After some adjustments) your code runs here and I think it does what you want. But you're complaining. Therefore: Please elaborate what you'd like it to do

Code: [Select]
`Const XX=5 ; X_OFFSETConst YY=5 ; Y_OFFSETConst SQD=50 ; TILE_SIZEConst SSP=2 ; TILE_SPACING; use consts instead of magic numbers to keep your code flexibleConst FIELD_WIDTH% = 5Const FIELD_HEIGHT% = 5Const FIELD_SIZE% = FIELD_WIDTH * FIELD_HEIGHTDim ST(FIELD_SIZE)Dim TI(FIELD_SIZE)Dim NC(FIELD_SIZE)Dim FD(FIELD_SIZE)Local T=1Local X%, Y%Graphics(400,300,0,2)SetBuffer BackBuffer(); init tiles; "pro" tip: swap the X and Y for loops (to X being the inner one), so the; tiles are arranged in reading direction - which can come in handy in; many places, especially when debugging.For X=1 To FIELD_WIDTH For Y=1 To FIELD_HEIGHT ; instead of using "now" & "interval" you could directly add them ; and only store "next change time", e.g: ; tchange(T) = MilliSec() + Rand(18000,50000) ST(T)=MilliSecs() ; set that tile's starting time to "now" TI(T)=Rand(18000,50000) ; set that tile's interval T=T+1 NextNextWhile Not KeyDown(1) ; update and draw tiles T=1 For X=1 To FIELD_WIDTH For Y=1 To FIELD_HEIGHT ; check if tile's interval has passed If MilliSecs()-ST(T)>=TI(T) Then ;... and update tile definitions NC(T)=Rand(0,2) FD(T)=(NC(T)>0) ;... and set new interval ST(T)=MilliSecs() TI(T)=Rand(18000,50000) EndIf ; black tile background If NC(T)=0 Then Color 0,0,0 Rect XX+(X-1)*(SQD+SSP),YY+(Y-1)*(SQD+SSP),SQD,SQD,1 EndIf ; a select NC(T) statement wouldn't hurt here Color 255*(NC(T)=0)+250*(NC(T)=1)+20*(NC(T)=2),255*(NC(T)=0)+20*(NC(T)=1)+250*(NC(T)=2),255*(NC(T)=0) ; draw coloured tile foreground Rect XX+(X-1)*(SQD+SSP),YY+(Y-1)*(SQD+SSP),SQD,SQD,FD(T) T=T+1 Next Next Flip(0)Wend`
*If you think your code is readable: remember that others (me included) thought at first glance you were using "Goto"