• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Using a string variable as a variable name?

Status
Not open for further replies.
Level 6
Joined
Jan 8, 2010
Messages
155
if GetHandleId(udg_regions_Player1Frontline[udg_tmpInt]) == GetHandleId(udg_Gen_Player1Region) then

If I want to make this line able to check both Player1Frontline and Player2Frontline (as well as Player1Region and Player2Region) is there a way that I can make a variable that stores the number of player, and then append it to the line? Manipulate a string variable to replace Player1Frontline is what I mean.

I know that maybe my question isn't very clear, for that I apologize :/ I just didn't know how to say it another way :p
 

Jampion

Code Reviewer
Level 15
Joined
Mar 25, 2016
Messages
1,327
The native ExecuteFunc() takes a string and executes the function with that function name. I guess you are looking for a similar feature to get a variable from its name.
I doubt this is possible in JASS.
I am not too familiar with text macros, but they might help generating that kind of code for you.
If you would use a 2d array and use player number as one index, you could make it easier I guess. 2d arrays are not directly possible in JASS, but you can use a hashtable instead and I think vJASS has also a 2d array feature.
Basically whenever you create variables with numbers in them, you should think about using the number as an index in an array.
 
Level 13
Joined
Mar 24, 2013
Messages
1,105
I might be confused, but you want to check when the any of the Player's Frontline regions are the same as another region.

I'm not sure that checking the handle id of them will ever return true though.

But if it were, I think this would be how you would map it out.

JASS:
set myRegion[1] = Player1FrontLine[1]
set myRegion[2] = Player2FrontLine[1]

set myRegion2[1] = Player1FrontLine[2]
set myRegion2[2] = Player2FrontLine[2]

set myRegion3[1] = Player1FrontLine[3]
set myRegion3[2] = Player2FrontLine[3]

etc.

set myOtherRegion[1] = GenPlayer1Region
set myOtherRegion[2] = GenPlayer2Region

set i = 1
loop

exitwhen i > 12

if myRegion[i] or myRegion2[i] or myRegion[3] == myOtherRegion[i] then

do something

else

do something else

endif

set i = i + 1

endloop
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
If I want to make this line able to check both Player1Frontline and Player2Frontline (as well as Player1Region and Player2Region) is there a way that I can make a variable that stores the number of player, and then append it to the line? Manipulate a string variable to replace Player1Frontline is what I mean.
There is no way directly. However in theory one could write a compiler that adds this feature by replacing global variable set/get with hashtable set/get calls and the variable names into appropriate unique keys for the hashtable.

If one wanted to maintain global variable support (for speed) one could also use lambda style functions for each supported global variable that are stored in a hashtable lookup system and can be executed to read/write to some automatically generated global variables. Not very efficient but still dynamic.

The best solution regarding your problem would likely be to add the regions to various collections so you can process them. I believe this is what pOke is suggesting.
 
Accessing variables via strings is often seen as a serious anti-pattern in code structure and even language design and it makes your code REALLY hard to debug. You hardly ever want to do that from a code organisation perspective, because in about 99% of the cases, YOU REALLY DO NOT WANT TO DO THAT.

What you basically want to emulate here are multidimensional arrays, that calculate ids of things based on the player.

So instead of writing

JASS:
if GetHandleId(udg_regions_Player1Frontline[udg_tmpInt]) == GetHandleId(udg_Gen_Player1Region) then
if GetHandleId(udg_regions_Player2Frontline[udg_tmpInt]) == GetHandleId(udg_Gen_Player3Region) then
if GetHandleId(udg_regions_Player3Frontline[udg_tmpInt]) == GetHandleId(udg_Gen_Player3Region) then

You do this:

JASS:
globals
    constant integer FRONTLINE_REGIONS_PER_PLAYER = 500
endglobals

function GetPlayerFrontline takes integer playerId, integer frontlineIndex returns region
    return udg_regions_PlayerFrontline[playerId * FRONTLINE_REGIONS_PER_PLAYER + frontlineIndex]
endfunction

function SetPlayerFrontline takes integer playerId, integer frontlineIndex, region frontlineRegion returns nothing
    set udg_regions_PlayerFrontline[playerId * FRONTLINE_REGIONS_PER_PLAYER + frontlineIndex] = frontlineRegion
endfunction

function FuncWithThisLoop takes nothing returns nothing

    local integer numPlayers = 12
    local integer playerId = 0
    local integer playerFrontlineId
   
    loop
        exitwhen playerId >= numPlayers
        set playerFrontlineId = 0
       
        loop
            if GetHandleId(GetPlayerFrontline(playerId, playerFrontlineId) == GetHandleId(udg_Gen_PlayerRegion[playerId]) then
                ...(your actions)...
                exitwhen GetPlayerFrontline(playerId, playerFrontlineId) == null
            set playerFrontlineId = playerFrontlineId + 1
        endloop
  
        set playerId = playerId + 1
    endloop
endfunction

Basically, you are using one array, but by creating offsets through playerId * FRONTLINE_REGIONS_PER_PLAYER you can emulate the idea of having separate arrays.
 
Status
Not open for further replies.
Top