grodrigues Posted January 4, 2020 Posted January 4, 2020 (edited) If you have a variable1 with the *name* of a variable2, is there a way to get the value of variable2? For the sake of illustration, assume //var2 is itself the name of a variable. SET var1 = ~var2~ How to get at the value of var2? I assume i have to expand var1, surround it with %'s then expand that, but whatever I tried it did not work -- I suspect I am doing the insertion of %'s wrong.Thanks in advance. Edited January 4, 2020 by grodrigues Marked as solved Quote
Jarno Mikkola Posted January 4, 2020 Posted January 4, 2020 (edited) You forgot to tell what this is part of. A .cre copy ? And usually you should try to first start from: BEGIN ~mod component name~ OUTER_SET "var1" = 0 OUTER_SET "var2" = 0 etc. And then set them to actual variables, unless you bring them from an external file and then you just use that as the bridge to do stuff. And if you need to "get" the variables name... you either print it to see it, or use it at a function that says what it can be or it breaks to the beginning, so no harm can be done. PRINT ~%var1%~ ACTION_IF (%var1% IS_AN_INT) THEN BEGIN Edited January 4, 2020 by Jarno Mikkola Quote
argent77 Posted January 4, 2020 Posted January 4, 2020 You have to use the EVAL keyword. Example: OUTER_SET var2 = 1234 // var2 contains a numeric value OUTER_SPRINT var1 ~var2~ // var1 contains name of variable "var2" OUTER_SET var3 = EVAL ~%var1%~ // var1 evaluates to var2, and (numeric) content of var2 is assigned to var3 PRINT ~var3 = %var3%~ // should output "var3 = 1234" Quote
grodrigues Posted January 4, 2020 Author Posted January 4, 2020 (edited) @Jarno Mikkola What you ask is either irrelevant, or I am quite aware of, for my question, but just to humor you: this is in the context of patching a spell. I am adding a projectile via ADD_PROJECTILE except the projectile name is gotten from a variable with value read from a 2da file at mod install time. According to the docs the projectile id is then written to a variable with the *name of the projectile*, so it follows that I got the name of the variable already, but I need to get at its value to write it in the spell. Now that I think of it, I suppose I could open projectile.ids (or whatever it is called) and just look up the id there since I already have the projectile's name, but my question still stands because there were other times where this extra level of indirection would have been helpful -- of course, there is always the chance that I do not know enough WeiDU, or have not structured my code well enough, to not have to resort to such black magic. But coming from languages such as Python or Haskell where such black magic is as easy as pie, that is how I tend to think. Edited January 4, 2020 by grodrigues Adressee Quote
grodrigues Posted January 4, 2020 Author Posted January 4, 2020 1 minute ago, argent77 said: You have to use the EVAL keyword. Ha ha. Thanks argent77. Quote
subtledoctor Posted January 4, 2020 Posted January 4, 2020 (edited) I thought (OUTER_)SET doesn’t need EVAL...? (EDIT - apparently I cannot edit code snippets later to fix the pseudocode. Have I mentioned I dislike this forum software?) While the Imp’s demeanor leaves something to be desired, it is anyway true that more context is always better for questions like this. For instance, you may be complicating things unnecessarily. ADD_PROJECTILE does indeed put its IDS value into a variable, so there’s no need to use a different one - You can just do something like ADD_PROJECTILE new_proj [etc.] COPY ~mod/spell.spl~ ~override~ ALTER_SPELL_HEADER INT_VAR projectile = %new_proj% END Edited January 4, 2020 by subtledoctor Quote
argent77 Posted January 4, 2020 Posted January 4, 2020 EVAL is only needed for dereferencing purposes. Without EVAL the expression in my example above would merely evaluate var1 into the string "var2" which in turn would trigger an error because it isn't a numeric value. Quote
grodrigues Posted January 4, 2020 Author Posted January 4, 2020 2 hours ago, subtledoctor said: While the Imp’s demeanor leaves something to be desired, it is anyway true that more context is always better for questions lime this. I still maintain that there was nothing unclear or any missing context for the question I posed: how to get the value of a variable if you have its name stored in another variable. Whether or not whatever it is I am trying to accomplish with such an extra level of indirection can be accomplished through other, simpler, more robust means is another question altogether -- a genuine one, I will grant Jarno and you that. Quote
subtledoctor Posted January 4, 2020 Posted January 4, 2020 1 hour ago, argent77 said: EVAL is only needed for dereferencing purposes. Without EVAL the expression in my example above would merely evaluate var1 into the string "var2" which in turn would trigger an error because it isn't a numeric value. Yeah but that’s because you put a string in there, isn’t it? In other words, does this not work? OUTER_SET var1 = 5 OUTER_SET var2 = var1 PRINT ~2 + 2 = %var2%~ (Easy enough to check, I know, but I’m AFK) 24 minutes ago, grodrigues said: I still maintain that there was nothing unclear or any missing context for the question I posed: how to get the value of a variable if you have its name stored in another variable. Whether or not whatever it is I am trying to accomplish with such an extra level of indirection can be accomplished through other, simpler, more robust means is another question altogether -- a genuine one, I will grant Jarno and you that. No need to “maintain” anything - I’m mot criticizing or arguing. I’m just saying that given a choice between asking a general question about Weidu, and pairing a general question with a concrete example of what you are trying to do, the latter will almost always net you better/more informative answers. Just something I’ve observed around here. For instance, maybe people who might be able to answer the question don’t fully understand it without the concrete context. Maybe they don’t follow the jargon, or whatever. I confess I couldn't make heads or tails of your first post - didn’t know what you mean by “expanding the variable.” I had to read further posts in the thread to figure out what you were asking. “Nothing in the post is unclear (to me with my background in Python/Haskell)” != “the post was communicated as effectively as possible” Quote
argent77 Posted January 4, 2020 Posted January 4, 2020 31 minutes ago, subtledoctor said: Yeah but that’s because you put a string in there, isn’t it? In other words, does this not work? OUTER_SET var1 = 5 OUTER_SET var2 = var1 PRINT ~2 + 2 = %var2%~ (Easy enough to check, I know, but I’m AFK) Yes, but that's just a standard assignment of one variable to another. Dereferencing contains (at least) one layer of indirection in-between. It's like retrieving a value by using a pointer in C/C++. Quote
grodrigues Posted January 4, 2020 Author Posted January 4, 2020 55 minutes ago, subtledoctor said: For instance, maybe people who might be able to answer the question don’t fully understand it without the concrete context. Maybe they don’t follow the jargon, or whatever. I confess I couldn't make heads or tails of your first post - didn’t know what you mean by “expanding the variable.” Fair enough. Quote
Jarno Mikkola Posted January 5, 2020 Posted January 5, 2020 (edited) 9 hours ago, subtledoctor said: Yea...r instance, maybe people who might be able to answer the question don’t fully understand it without the concrete context. Näh, or you could do the irrational thing and answer it with a cryptic answer yourself and be a genuine genius anyways. 14 hours ago, grodrigues said: @Jarno Mikkola What you ask is either irrelevant, or I am quite aware of, for my question, but just to humor you: this is in the context of patching a spell. Haha. That was not funny laughter, just the absurdity of this situation, as I myself have done for example this: ACTION_IF (IS_AN_INT %timer0% AND %sucfail% = 2) THEN BEGIN COPY_EXISTING_REGEXP GLOB ~^.+\.spl$~ ~override~ PATCH_IF ((SOURCE_SIZE > 0x113) AND (~%ability_list%~ STRING_CONTAINS_REGEXP ~%SOURCE_RES%~) AND (STRING_LENGTH ~%ability_list%~ > 8 )) THEN BEGIN READ_SHORT 0x1c "spl_type" PATCH_IF ("%spl_type%" = "4") THEN BEGIN LPF ~ADD_SPELL_CFEFFECT~ INT_VAR opcode = 172 target = 1 timing = 1 resist_dispel = 2 probability1 = 100 STR_VAR resource = EVAL "%SOURCE_RES%" END LPF ~ADD_SPELL_CFEFFECT~ INT_VAR opcode = 171 target = 1 timing = 4 resist_dispel = 2 duration = ~%timer0% + 6~ probability1 = 100 STR_VAR resource = EVAL "%SOURCE_RES%" END END END BUT_ONLY PRINT ~Success.~ The timer0 and sucfail variables are set in a long previous functions that are not really needed to be protracted here-in. What the above does is, it refreshes innate spells without the need to sleep after the timer, without them needing to complete during casting(the effect takes place instantly after starting to cast them). Now you have a live example of actual code, that features the in questioned EVAL. Be THANKFUL. Edited January 5, 2020 by Jarno Mikkola Quote
Luke Posted January 5, 2020 Posted January 5, 2020 12 hours ago, argent77 said: Dereferencing contains at least one layer of indirection in-between. Precisely! As a matter of fact, you can request multiple levels of evaluation. Example: OUTER_TEXT_SPRINT test1 ~test0~ OUTER_TEXT_SPRINT test2 ~test1~ OUTER_TEXT_SPRINT test3 ~test2~ OUTER_TEXT_SPRINT test4 ~test3~ OUTER_TEXT_SPRINT test5 EVAL EVAL EVAL ~%%%%test4%%%%~ PRINT ~%test5%~ Quote
grodrigues Posted January 5, 2020 Author Posted January 5, 2020 7 hours ago, Jarno Mikkola said: Be THANKFUL. From my post to argent77: "Thanks argent77. " But enough with this useless banter. Next time I pose a question I will try to add as much context as relevant and not respond in a snarky tone to an attempt at being helpful. Quote
subtledoctor Posted January 5, 2020 Posted January 5, 2020 (edited) Bring the snark! It’s okay. Sorry if I sounded combative above, I didn’t mean to. Just meant to help you get the best information, most efficiently. EDIT - give more context is my rule #2. Rule #1 is NEVER let the imp get a rise out of you. Edited January 5, 2020 by subtledoctor Quote
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.