Page 1 of 1

How to create constants automatically?

Posted: Sun Apr 07, 2024 12:54 pm
by charvista
Hello,
I know that PB cannot define (compose) constants natively by a variable or a function.
But I believe there must be an indirect(?) way (through API, ASM, or via a Macro)?
Who can help me? (if there ever exists a workaround for that)...
Here is where I started to define constants for my colors.

Code: Select all

EnableExplicit
Define.i R,G,B,N

Macro const(cvar,R,G,B)
    N=RGB(R,G,B)
    cvar=N
EndMacro

R=$FF
G=$B2
B=$00

const(#MYCOLOR,R,G,B)   ; should result in #MYCOLOR=$FFB200 (16757248)

Debug #MYCOLOR

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 12:59 pm
by NicTheQuick
There is no other way except with macros. Constants and macros are processed in the pre-compiler. And I don't know what API do you mean. Purebasic has no API you could use. And even Assembler will not help, especially if you use the C-Backend. :wink:

Can you please explain in more detail what you want to achieve?

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 1:35 pm
by AZJIO

Code: Select all

EnableExplicit
Define.i R,G,B,N

#MYCOLOR = $FFB200

R=Red(#MYCOLOR)
G=Green(#MYCOLOR)
B=Blue(#MYCOLOR)
cvar=#MYCOLOR

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 2:00 pm
by charvista
@AZJIO
It is the opposite that I am trying to do: the values R, G and B are normally read from a file, and from there I would like to put them in a constant. It is the automatic creation of the constants that I would like to do. Doing #MYCOLOR=$FFB200 is of course no problem, because there is no variable involved, it is #MYCOLOR=RGB(R,G,B) but that is not possible because constants cannot be created from variables... hence my question :wink:
In other words: not cvar=#MYCOLOR but #MYCOLOR=cvar

@NicTheQuick
If a macro is the only way, that's fine, but I would like to know how to do that. My attempt has failed. :?
I want to achieve #MYCOLOR=RGB(R,G,B)

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 2:15 pm
by mk-soft

Code: Select all

EnableExplicit

Macro const(_cvar,_R,_G,_B)
    _cvar= (_B << 16 | _G << 8 | _R)
EndMacro

#R=$FF
#G=$B2
#B=$00

const(#MYCOLOR,#R,#G,#B)   ; should result in #MYCOLOR=$FFB200 (16757248)

Debug Hex(#MYCOLOR)

Debug Red(#mycolor)

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 2:17 pm
by AZJIO
@charvista
How is a constant different from a variable? As a result, this will be a memory cell, with the only difference being that the variable can be changed.

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 2:48 pm
by charvista
@mk-soft
#R=$FF
#G=$B2
#B=$00
You changed my variables into constants here.
R, G, B are here as variables. (R, G and B are values got from a file, but to make the example shorter, they are "already" in variables...)
Your code is nice, but then you should first do #R=R :mrgreen: which makes it a vicious circle.

@AZJIO
Constants are different from variables as they are global. My software is a "package", so it calls many procedures, but some colors must be equal the same everywhere. Okay, I can also use Global _MyColor = RGB(R,G,B) but I am trying to load them in constants as they cannot be modified later, once defined.

== > The only thing I am trying to accomplish, is to define a constant with the contents of a variable with a Macro.

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 3:13 pm
by Demivec
charvista wrote:== > The only thing I am trying to accomplish, is to define a constant with the contents of a variable with a Macro.
The limitations of using variables and functions in the definition of constants at compile time is clear. Why do you want to define these values at compile time as individual constants? How are the constants intended to be used later in the code? Is the goal to save space, reduce errors, or what exactly?

It seems as if your data may be variable because you would otherwise just use numeric values in defining the constants.
You could define a table or map whose values are indexed and accessed as with constants representing the indexes or map key associations. If using a table you could just include the properly formatted color data from the file into the DataSection and use an accessory function to read the values. I'll work up an example if needed later when i get a moment. It all depends on your more specific needs.

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 4:14 pm
by NicTheQuick
If you read that file with the RGB values at runtime there is no way to define constants. Constants are not there anymore during runtime. They are replaced with their value before compiling.
What you can do however is using Maps.

Code: Select all

; Using Longs here as they are big enough for RGB values
Global NewMap constants.l()

Macro DQ
	"
EndMacro

Macro c(name)
	constants(DQ#name#DQ)
EndMacro

; Main program

c(MYCOLOR) = RGB(1, 2, 3)
Debug c(MYCOLOR)
Maybe that's a solution you like.

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 4:48 pm
by breeze4me
That's not possible with a numeric constant, but it is with a string constant.

Code: Select all

#MyColor$ = "A unique string that is never used in the code."
*p = @"A unique string that is never used in the code."

R=$00
G=$B2
B=$FF

PokeS(*p, Str(RGB(R,G,B)))

color.l = Val(#MyColor$)
a.s = #MyColor$

Debug Hex(color, #PB_Long)    ; FFB200
Debug a          ;16757248

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 6:53 pm
by STARGÅTE
A constant is just a macro with a hashtag. Once defined, it is replace everywhere during the compilation and is not present anymore in the final executable. I think at this point the discussion is closed.

When you want to define a constant from another file (during compilation), you can use IncludeFile.

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 9:45 pm
by charvista
@Demivec, Stargåte, NicTheQuick
Allright, I understand now what is happening under the hood of PB when compiling the program and that they are no longer modifiable afterwards.

@breeze4me
Fascinating :shock:
So I only need to use Val() or ValD() to get its numeric value.
But finally, I think I will use Global variables, it is much easier.

@Demivec (again :) )
Your example with DataSection is very welcome, I am a bit wondering how you would do that using a table.
To make it easy to understand, it is like reading in a file the colors that were chosen in a "Colors settings" program.
For example, the background color, the title color, and other elements colors are configured there.
It is like chosing for a light theme, or for a dark theme.
Then, I want to load them in "constants", and all programs use them accordingly.

Re: How to create constants automatically?

Posted: Sun Apr 07, 2024 10:47 pm
by charvista
@NicTheQuick
I studied your code, it is a very interesting alternative, as it works globally: procedures recognize them. Thank you!