step with a file read variable

Blitz3D Forums/Blitz3D Programming/step with a file read variable

Hey there. I can't seem to get blitz to assign a variable read from a file to a const so that I can use it with a for-step-next loop.

a$ = Input$("Specify Source:")
Global img=LoadImage(a$+".jpg") 
Global file$ = a$ + ".txt"
Global linkfile = ReadFile(file$)

Const steps = ReadInt (linkfile)

For y = 0 To ImageHeight(img) Step steps
	WriteLine( fileout, "..."  )
	For x = 0 To ImageWidth(img) Step steps
		If y <= ImageHeight(img)
			If x <= ImageWidth(img)
				WriteLine( fileout, "...stuff..."  )
			EndIf
		EndIf	
	Next
	WriteLine( fileout, "...."  )
Next


It keeps saying "expression must be constant"
Is there any way I can use a file variable to change the step amount?

Firstly, you shouldn't be looking outside the image buffer ... it goes from 0...(width-1) etc.. If you start reading the RGB values outwith you could get a mav.

There is no way to assign a constant from a variable read from a file. This should work though ..

For y = 0 To ImageHeight(img)-1 
       if ( y mod steps ) = 0
        	WriteLine( fileout, "..."  )
 	        For x = 0 To ImageWidth(img)-1 
                     if ( x mod steps ) = 0		        
  		         WriteLine( fileout, "...stuff..."  )
		     EndIf
  	        Next
	        WriteLine( fileout, "...."  )
        endif
Next


Thanks ill try it.

The constant step value is a pain, but it's understandable why it's like that.

The most flexible way is to use a While loop instead of For-Next.

If you know the step size is positive then the equivalent loops look like this:

; Set values for xFirst and xLast here. xStep must be constant.

For x = xFirst To xLast Step xStep
	; do something
Next



; Set values for xFirst, xLast and xStep here.

x = xFirst

While x <= xLast
	; do something
	x = x + xStep
Wend

If xStep is negative then you would use "While x >= xLast".

If you don't know the sign of xStep in advance then you must write code to handle both cases.
This complication is the reason For-Next insists on a constant step value.

Is it really important to have "Steps" as a constant?
You can use a global and that will work too.

A constant is assigned a specific value before compiling, and when the compiler sees that constant anywhere in your code, it is replaced with the value assigned to it.
A constant cannot be changed during execution of your program, so you can't store a value in it that you've read from a file.

The compiler replaces the label with the value when compiling, so no matter what, you won't be able to change the step value.

You could also avoid using step by multiplying the For variable by a step value:
stp = 5
For i = 0 To 50/stp
  x = i * stp
  Print x
Next