Page 1 of 1
[5.30] Runtime variable creation
Posted: Wed Jul 23, 2014 3:26 pm
by djes
(moved this from announcement)
The runtime() feature showed by the dialog example shows what is possible with this terrific new feature !
By the way, is there already the possibility to create such runtime variables and structures (not already defined) on the fly, for example let the user define a new named variable from a GUI ? Maybe it already exists for xml or json files, I just saw ways to populate already existing ones.
I imagine it would be a must to create databases or UI on the fly, without to struggle with dynamic memory allocations and variable tracking.
Re: [5.30] Runtime variable creation
Posted: Wed Jul 23, 2014 10:40 pm
by idle
I haven't really looked into the runtime features but as far as I understand I don't think it facilitates
creating new variables during runtime, it only appears to provide a map of constants variables and procedures
which are specified with the runtime keyword.
It's easy enough to create a full runtime engine but it's quite difficult to integrate it syntactically.
I've tried various methods but never really found a great solution, trying to keep it lean and fast plus easy to use
is difficult without some compiler helpers.
Re: [5.30] Runtime variable creation
Posted: Thu Jul 24, 2014 6:15 am
by Danilo
What about creating it by yourself?
Code: Select all
Structure rtv
type.i
*value
EndStructure
Global NewMap runtimeVariables.rtv()
Procedure SetRuntimeDouble_(name.s, value.d)
runtimeVariables(name)\type = #PB_Float
runtimeVariables()\value = AllocateMemory(SizeOf(Float),#PB_Memory_NoClear)
If runtimeVariables()\value
PokeD(runtimeVariables()\value,value)
EndIf
EndProcedure
Procedure SetRuntimeQuad_(name.s, value.q)
runtimeVariables(name)\type = #PB_Quad
runtimeVariables()\value = AllocateMemory(SizeOf(Quad),#PB_Memory_NoClear)
If runtimeVariables()\value
PokeQ(runtimeVariables()\value, value)
EndIf
EndProcedure
Procedure.d GetRuntimeDouble_(name.s)
If FindMapElement(runtimeVariables(),name) And runtimeVariables(name)\type = #PB_Float
ProcedureReturn PeekD( runtimeVariables(name)\value )
EndIf
EndProcedure
Procedure.q GetRuntimeQuad_(name.s)
If FindMapElement(runtimeVariables(),name) And runtimeVariables(name)\type = #PB_Quad
ProcedureReturn PeekQ( runtimeVariables(name)\value )
EndIf
EndProcedure
SetRuntimeDouble_("doubleVar",1.2345678)
Debug GetRuntimeDouble_("doubleVar")
SetRuntimeQuad_("q",123456123456123456)
Debug GetRuntimeQuad_("q")
Re: [5.30] Runtime variable creation
Posted: Thu Jul 24, 2014 8:27 am
by djes
Thank you both for your answer, idle for your private message and for the past work on OPB (objective PB), Danilo for this nice (^2) snippet. As usual, my question was about a native way to do it (really close to yours, Danilo), to have a standard way of doing things. I imagine how it would be easy to open an XML/JSON file and populate variables in just one command. Even if I'm not fan of this "loosing control" way of doing things, it opens a way, and make me dreams about pure, clean and fast database creation, or simple and extensible load/save features never seen as standard commands in languages. We're not far from object, true, nor it's dynamic code creation. Not far from nature and information exchange/mix, neither...
Re: [5.30] Runtime variable creation
Posted: Fri Mar 18, 2016 4:13 pm
by grabiller
Sorry to revive this thread from 2 years ago,
But looking at some user code and examples, it seems few peoples realize the "real use" (imho) of the Runtime feature: to get access to variables, procedures, modules, etc.. defined/included *after* they are needed.
To give an example of what I am talking about, the code from my project is split into libraries of "objects" (custom classes as modules) and "services" (modules). In this configuration, "services" use defined "objects" and these objects sometimes need functions defined in some "services" (for instance an image "object" may need to access the resources "service", a window "object" may need to access the gui "service", etc..). Normally, you can't access both ways, because either the objects are not aware of the services if they are included first, and vice versa. There are several solutions to this, one using events that are sent from "objects" and observed by "services" and interpreted. That's one approach.
The Runtime feature gives you another one. In my case the "objects" are included first and the "services" last, but they are "runtime" modules, so any "object" can access any "service" directly, and any "service" can create/factor any "object".
I would be interested to hear how others handle this issue and if they use the Runtime feature specifically for this issue.
Cheers,
Guy.
Re: [5.30] Runtime variable creation
Posted: Sun Mar 20, 2016 11:01 am
by grabiller
Perhaps I should have said the "life saver use" of the Runtime feature.
In fact I could have summarized my previous message by saying that:
"The Runtime feature is a (life saver) workaround to the files inclusion order issue".
I'm not sure everyone understand it this way, that's why I'm talking about it, because I have the feeling it could be of great benefit and a life saver for some other projects too, if understood correctly.
Cheers,
Guy.