Question about "dynamic variable names"
BlitzMax Forums/BlitzMax Beginners Area/Question about "dynamic variable names"
| ||
Is there a way to name a variable (handle) with a String, when the String can change beforehand? I need to have a hash table (got one) list that variables can be created from, with |
| ||
There's a lot of code to digest in that link, but given it's 3 years old I guess it predates the TMap? Do you want direct access to avoid constant look-ups into the map? You can link variables directly to the TMap since ValueForKey returns the actual instance and not a copy, giving you read/write access to the map values. This example has three types linked to one entry in the map.. Superstrict Graphics 800, 600 Type integer Field i:Int Function Create:integer ( i:Int ) Local in:integer = New integer in.i = i Return in End Function End Type Local map:TMap = New TMap map.Insert( "1", integer.Create( 99 ) ) Local a1:integer = integer( map.ValueForKey( "1" )) Local a2:integer = integer( map.ValueForKey( "1" )) Local a3:integer = integer( map.ValueForKey( "1" )) While Not AppTerminate() cls DrawRect( a1.i, 20, 20, 20 ) DrawRect( a2.i, 60, 20, 20 ) DrawRect( a3.i, 100, 20, 20 ) flip If ( KeyHit( KEY_LEFT ) ) a1.i :- 5 If ( KeyHit( KEY_RIGHT ) ) a1.i :+ 5 Wend If you later wanted to assign, for example, "a3" to a different value in the map, it's just a one-off look up. Am I close? |
| ||
You can probably write some C++ code to get a hash table that stores pointers instead of objects (if that's what you really want, instead of having to deal with objects). I've written an integer-key-based hash table for Max before, it shouldn't be hard to adopt to your problem. |
| ||
Do you want direct access to avoid constant look-ups into the map? (if that's what you really want, instead of having to deal with objects) Yeah, I am worried about the overhead of objects, and I very much don't want to rely on them when there are faster methods, even if only trivially faster. I'm just that kind of person. Also I don't know C or C++, and I wanted to make this project in BlitzMax, and start learning C to do it over again to make it faster while I'm selling the BlitzMax version. Well it boils down to me wanting to store conditional statements in a string array, looping through that never endingly. I am currently working on an "RPG Maker" type program, and I am still in the planning stage. Here is what I have so far (pseudo-code): (the entries in the arrays would be repeated based on how many objects were specified by the user) gObject Array[ object( can a be tile, the player character, an NPC, an enemy, etc.) ] Rem create a hash table from the gObject Array beforehand( making sure that there are NO duplicate entries), store it, and "map" it to the other arrays (i.e. object at position [5] in the array (starting from 0, so the 6th) will reference the "data holder" array starting at position [35] for it's data, so that the object need not be re-referenced( for specific values) within a loop or otherwise, with the object's x and y values only needed to be "read" once( maybe from a loading screen--however short) from the object and stored in the array for future checks/calculations, being referenced by array position in the future instead of using object methods a gObject would store images and other things EndRem String Array[ switch( can be "on" or "off" (will skip ahead to next switch if "off")), type of condition, number of arguments( depends on condition type), arguments ] Rem use a Select...Case block with each Case being a different type of condition, passing values using the "number of arguments" from the appropriate array index also, depending on the type of condition (i.e. whether it takes place before the game really starts), the conditions should be added into the String Array by the editor according to importance EndRem String Array[ variable name, value of variable ] Rem create a hash table of all of the variable names beforehand( so that they can be set up and referenced appropriately) but do not provide for dynamic creation of variables for now (unfortunately, this does not seem to be something I can circumvent) EndRem Int Array[ object type, x position, y position, image handle, drawing layer, degrees of rotation ] Rem so, 6 parameters per object for the "data holder" array when an object is deleted from the gObject Array, you must update the "data holder" array EndRem Eventually I just want to have all this stored as XML, and just read it back as "opening" a "project file" in the RPG-Maker-clone editor( if you haven't used it then, well, let me say that the user specifies almost everything beforehand, then compiles it into an unchanging 'game' (you can see it's help file here)), and I would just build everything off of that. Would I just make different Functions for all of the different types of my game, and, depending on the 'conditional type', feed them values from the 'conditional' string array? Are Functions the answer?? The only thing is, since the user will create everything beforehand (including variables (and name these variables whatever name they will), and tie these variables to different things), it's getting hard to wrap my head around. |
| ||
Oh, also, I am not asking you to do any of that that's in the code box for me. I have done enough testing to know that code can be written to my design (so far). The issue I posted here is the last major hurdle I have in my design. |
| ||
Yeah, I am worried about the overhead of objects, and I very much don't want to rely on them when there are faster methods, even if only trivially faster. I'm just that kind of person. Using objects in the hash table itself shouldn't be much of a speed issue, though (constantly) accessing an object's field might be.. Also I don't know C or C++, and I wanted to make this project in BlitzMax, and start learning C to do it over again to make it faster while I'm selling the BlitzMax version. Ah, righto.How are you planning to handle different data types (int, float, long, double)/types (strings?)? I would say to do what matibee suggests, only when you access the object to make a local variable that points to the object's field (the above question greatly affects this solution). That should work well for using the same variable more than once, but if you're only using it once in an equation (or passing it off) you needn't do that. Also, you'll run into issues if you want to pass that 'variable' off to another function to (potentially) make changes to it.. in that case you would either have to pass the object or a pointer to the object's value. |
| ||
Okay, I think between the hash function I have from the Code Archives, and TMap (which also seems to be for hash functions??), I'm gonna figure out something. How are you planning to handle different data types (int, float, long, double)/types (strings?) String Array[ variable name, variable type, value of variable ]Then they would be converted. No no wrong. Two arrays, one for Longs and one for Strings, in the format shown in the pseudo-code box. Thanks guys (matibee especially for giving a TMap example, had no idea what TMap was for before that). |
| ||
Is there a way to name a variable (handle) with a String, when the String can change beforehand? I think can declare variables with the names of other variables, according to the Lua docs page in the pub.mod folder. Accessing Lua Globals The code lua_pushstring(LuaState, "BMXString") lua_setglobal (LuaState, "luaglobal") defines a global Lua variable (called luaglobal) which contains a string (namely "BMXString"). Lua's weird but I guess it's incredibly flexible. |