Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Assigning values to variables through a string

Author
Message
Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 23rd Nov 2010 23:32
I was wondering if it is possible to what the title says, assign a value to a variable through a string of that variable's name?
Such as

And that would make MyVariable=2.

My computer surpasses all the technologies of the day. What computer do I have?

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 24th Nov 2010 04:44
You could write from some functions to look up the value of or change the certain varibles easy enough. But you might as well just store them in an array or memory block and use constants as references.

bitJericho
23
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 24th Nov 2010 07:04 Edited at: 24th Nov 2010 07:06
You might want to look up pointers. If you're not sure what to do, let us know what you're trying to do and we can offer some insight.

Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 24th Nov 2010 14:43
Well it would be a workaround to not being able to define an array in a type.
So instead of

It would be


The point of this would be when programming a game engine. You could have a player type an object type and a npc(enemies) type.
In these types you can have arrays of animation frames, arrays of object numbers, ect...
This would be the case when the type is applied to individual objects such as the player where you want to have a player type store the animation frames but also have the player's position. You wouldn't want to have 2 separate types, just one to cover all of the player variables and arrays.

My computer surpasses all the technologies of the day. What computer do I have?

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 24th Nov 2010 17:50 Edited at: 24th Nov 2010 17:51
If all you to do is attach some array like structure to a type, then allocate some memory then peek/poke into it for field access.

Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 24th Nov 2010 18:23
Would that be by using the Make Memory commands? Is so how, I don't understand how to use them.

My computer surpasses all the technologies of the day. What computer do I have?

function g b as string
15
Years of Service
User Offline
Joined: 17th Dec 2010
Location:
Posted: 17th Dec 2010 12:31
great, this topic already exists. I'm having the same issue. Basically I've got a function which edits a global array, the global array is an object instantiated from a type I've set up.
What I want to be able to do is pass the function a string that tells it the name of my array so it knows which global array it needs to modify. I've tried a number of things, but so far it seems dbpro just cannot do the whole "variable variable" thing (like you can in php and some other languages).
Any ideas for a way around this?
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 17th Dec 2010 14:52
Something you could do:


I personally wouldn't do it like this, probably use IanM's plugin for this, there's stuff in there for this sort of thing... I think.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
function g b as string
15
Years of Service
User Offline
Joined: 17th Dec 2010
Location:
Posted: 17th Dec 2010 15:24
Thanks
I skimmed through the snippet - looks alot like what I'd planned as a last resort (i.e plan C if writing a DLL in c++ [plan B] fails )
We need something simpler - more concise and efficient
Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 17th Dec 2010 17:49 Edited at: 17th Dec 2010 17:59
Exactly
Here's an algorithm for a possible way for it to work :
1. Get the strings and assign each one an index position.
2. Allocate memory at each index.
3. Declare the original variables with a pointer to the memory locations.
4. Then call up a function that:
a. Has a string parameter and a variable as the type you want to assign the string variable
b. Translates that parameter into the corresponding index
c. Stores the variable data at the memory location stored by the array at the index location
5. Have another function that can access the memory location for reading data

Notes: The pointer is so that the original variable value will be changed as the data at the memory location changes.

My computer surpasses all the technologies of the day. What computer do I have?

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Dec 2010 22:30
In addition to the ways already discussed, there are several additional ways that I can think of:
1. if the arrays are identical, then pass the relevant array pointers to your functions instead of the name.


2. use my in-memory lookup commands to simulate an array.


3. if the arrays are different, then call different functions via function pointers


... and given a couple of hours, I could probably come up with several other methods too.

function g b as string
15
Years of Service
User Offline
Joined: 17th Dec 2010
Location:
Posted: 18th Dec 2010 10:33
In other words, there's no way to do it without your plugin (IanM)?
As far as I can see none of those code snippets you posted will work with the stock dbpro command set.
(My personal preference would be to explore further with stock command set and write my own extensions if it proves futile)

By the way, this might be slightly off topic, but does anyone know anything about how dbpro handles pointers (or of any resources that explain it)? The help docs speak of the dereference/indirection operator (*) but no reference/address of operator (& for example)...
bitJericho
23
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 18th Dec 2010 13:11 Edited at: 18th Dec 2010 13:12
I'd like to point out that Ian's plugin might as well be part of the core command set. I wouldn't think about writing a DBP app without the matrix1utils and I think you'll find most people here agree. Definately get them!! No use in reinventing the wheel.

[center]
Join the TGC Group!
http://tehcodez.groups.live.com
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 18th Dec 2010 15:35
Quote: "In other words, there's no way to do it without your plugin (IanM)?"

Nonsense. One of the early pieces of code I wrote for my own use in DBPro was for a value store of almost exactly the same type as you are after.

You need an UDT of Key and Value, held in a 1D array. Then you use binary search to locate items for lookup, deletion or insertion (using the 1D array manipulation commands to do insertion/deletion). All that's needed on top of a basic mechanism like that is for you to format the key properly, which can be done similarly to my second code snippet (writing your own PADLEFT$ of course).


As I said, I can easily come up with several different methods to do this, and they certainly wouldn't all need my plug-ins to carry them out - I use my plug-ins because it's easier (after I've coded them in the first place), and far more efficient.

Quote: "No use in reinventing the wheel."

... and I'd agree in almost every case, the exception being if you want to learn and understand how to do it yourself. But once I've learned how, I'd pick the most efficient way to continue (which if it's a straight comparison between equivalent code for DBPro and a plug-in, would almost always be the plug-in).

Login to post a reply

Server time is: 2026-07-21 07:03:13
Your offset time is: 2026-07-21 07:03:13