• 🏆 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!

Convert string to trigger?

Status
Not open for further replies.
Level 7
Joined
Jul 4, 2007
Messages
249
Hello again.
Might someone answer to how (if possible) I could convert a string to a trigger? Something that would works like this:

set udg_string = "gg_trg_Mask_of_Death"

set udg_Trigger = udg_string
 
Last edited:
Level 7
Joined
Jul 4, 2007
Messages
249
Might as well ask why you specifically need to do that, for what purpose.
I have an item-type array that I want triggers (pick up and drop) to be automatically defined by the name of the item, for example
  • Set Items_General[1] = |cFF0070ddMask of Madness|r
These are automatically set:
  • Set Items_General_PickUp[1] = Mask of Madness pick up <gen>
  • Set Items_General_Drop[1] = Mask of Madness drop <gen>
If I could've used a string I could've just made a for loop with Items_General[X] and set trigger to item name + pickup/drop.
It would just be easier not having to add those all the time. I need to have them to check if anyone still has that item-type when it's dropped and turns off trigger if false.
 
Level 39
Joined
Feb 27, 2007
Messages
5,023
Why do you need separate triggers instead of just having a series of if blocks in a single pick up/drop trigger? What do you even need the triggers specifically for anyway? They should just work on their own.


Hashtable or Table object with the item’s rawcode as a key to store and retrieve the trigger is the best option here.
 
Level 7
Joined
Jul 4, 2007
Messages
249
Why do you need separate triggers instead of just having a series of if blocks in a single pick up/drop trigger? What do you even need the triggers specifically for anyway? They should just work on their own.


Hashtable or Table object with the item’s rawcode as a key to store and retrieve the trigger is the best option here.
I do run them from one single pick up and drop trigger and that's why I need them, to know which trigger to run. The "problem" is that I have to set the triggers manually as variables, instead of having a system that does that for me. It's just because I'm lazy.

So it looks like this in the acquired item trigger
  • For each (Integer A1) from 1 to Items_AMOUNT_General, do (Actions)
    • Loop - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Item-type of item) Equal to Items_General[A1]
        • Then - Actions
          • Trigger - Run Items_General_PickUp[A1] (ignoring conditions)
        • Else - Actions
 
Last edited:
Level 39
Joined
Feb 27, 2007
Messages
5,023
You're presumably doing something like this:
  • Actions
    • Set TrigRun = <Some way to get the trigger from the item name>
    • Trigger - Run TrigRun (checking conditions)
But you could just do:
  • Actions
    • Set IType = (Item type of (Item being manipulated))
    • If (All conditions are true) then do (Then actions) else do (Else actions)
      • If - Conditions
        • IType equal to Mask of Madness
      • Then - Actions
        • -------- do all of the things in the mask of madness trigger here --------
      • Else - Actions
    • If (All conditions are true) then do (Then actions) else do (Else actions)
      • If - Conditions
        • IType equal to Other Item 1
      • Then - Actions
        • -------- do all of the things in the other item 1 trigger here --------
      • Else - Actions
    • If (All conditions are true) then do (Then actions) else do (Else actions)
      • If - Conditions
        • IType equal to Other Item 2
      • Then - Actions
        • -------- do all of the things in the other item 2 trigger here --------
      • Else - Actions
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
A string is not a trigger. What you are asking is to dynamically resolve the value of a trigger variable from a variable name string.

Yes this is possible. If one uses Lua. One can look up the string as a key inside the root/global table.

This is not possible with JASS since one cannot dynamically resolve trigger variables.
 
Level 10
Joined
Aug 19, 2008
Messages
491
A string is not a trigger. What you are asking is to dynamically resolve the value of a trigger variable from a variable name string.

Yes this is possible. If one uses Lua. One can look up the string as a key inside the root/global table.

This is not possible with JASS since one cannot dynamically resolve trigger variables.

What's the difference between this and @IcemanBo s suggestion of a trigger hashtable?
 
Level 7
Joined
Jul 4, 2007
Messages
249
A string is not a trigger. What you are asking is to dynamically resolve the value of a trigger variable from a variable name string.

Yes this is possible. If one uses Lua. One can look up the string as a key inside the root/global table.

This is not possible with JASS since one cannot dynamically resolve trigger variables.
Thanks, this is exactly what I was wondering.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
What's the difference between this and @IcemanBo s suggestion of a trigger hashtable?
Infinitely more reliable, faster and requires less work. Key from string might produce collisions due to the nature of hashes which would result in a hard to diagnose bug. However for JASS this is probably the best one can do, especially if one adds mitigations for collisions.

Internally Lua uses hash tables (hence "table" type). Hence if using Lua one could access the variables directly from the root (global) table by using the string as a key/index.
 
You would have to manually populate a JASS hashtable with data but Lua accessing the root table would already have that data in it.
The difference would be to how to get to the string key for a trigger. In JASS, one defines a string, in Lua DSG said, it could be looked up in the root table. Filling a (hash)table needs to be done in both cases.

How does the string key for a trigger from the Lua root table looks like exactly? Anyways, it's just important the user is align with it, since the string key input would need to be defined statically.

@Sk0gsHu[GG]arN the concept seems a bit off, honestly. I believe an other solution can be found. Running a trigger by an item's type would be superious, but even this should not be needed.
 
Level 7
Joined
Jul 4, 2007
Messages
249
@Sk0gsHu[GG]arN the concept seems a bit off, honestly. I believe an other solution can be found. Running a trigger by an item's type would be superious, but even this should not be needed.
Well I need to know which the matching trigger is, since:

Pick up
  • If ((Clarity Lamp ability <gen> is on) Equal to False) then do (Trigger - Turn on Clarity Lamp ability <gen>) else do (Do nothing)
Drop (Check if anyone has item of type)
  • If (Items_TYPE_Equipped[Item_TypeNumber[value]] Less than or equal to 0) then do (Trigger - Turn off Clarity Lamp ability <gen>) else do (Do nothing)
But this is not all, all the pick up and drops doesn't look like this, just most of them. I sometimes add unit to a unit group when picking up an item for example.
 
Last edited:

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,891
You're overcomplicating something fairly trivial for no reason. All of that can be done in 2 triggers. With or without a system, you have to do it manually. You can match the item type to the trigger by matching the array: e.g. MyItemType[1], ItemTrigger[1].
 
Last edited:
Level 9
Joined
Jul 30, 2018
Messages
445
You don't need to convert any strings or triggers to strings or triggers. Simple If-statement will do just fine.

In your Map Initialization trigger just pair up the trigger and the item-type:
  • Set ItemType[0] = Crown of Kings +5
  • Set ItemTrigger[0] = YourTrigger <gen>
Then make a trigger to check which item is being picked up:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • (Item-type of (Item being manipulated)) Equal to ItemType[0]
    • Then - Actions
      • Trigger - Turn on ItemTrigger[0]
    • Else - Actions
 
Level 7
Joined
Jul 4, 2007
Messages
249
You don't need to convert any strings or triggers to strings or triggers. Simple If-statement will do just fine.

In your Map Initialization trigger just pair up the trigger and the item-type:
  • Set ItemType[0] = Crown of Kings +5
  • Set ItemTrigger[0] = YourTrigger <gen>
Then make a trigger to check which item is being picked up:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • (Item-type of (Item being manipulated)) Equal to ItemType[0]
    • Then - Actions
      • Trigger - Turn on ItemTrigger[0]
    • Else - Actions
You're overcomplicating something fairly trivial for no reason. All of that can be done in 2 triggers. With or without a system, you have to do it manually. You can match the item type to the trigger by matching the array: e.g. MyItemType[1], ItemTrigger[1].
Yes that's what I'm doing already, I just wanted to know if I could convert a string to a trigger because it would simplify, not having to add it manually. As I said before it's not a problem, it's already working.

But I'm not even sure why there needs to be a sperate trigger for each item type... maybe the intention could be elaborated some more.
This is it, nothing else to it. I just want to reduce the amount of variables needed to be set manually.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
The difference would be to how to get to the string key for a trigger. In JASS, one defines a string, in Lua DSG said, it could be looked up in the root table. Filling a (hash)table needs to be done in both cases.
Both JASS and Lua use a hash table structure internally to resolve variables dynamically at run time. The difference is Lua allows one access to this global hash table as a normal table and perform normal table operations on it while JASS only allows access to it via staticly compiled code.
How does the string key for a trigger from the Lua root table looks like exactly?
The same as the trigger variable name always looks. The only difference is that one can create it and resolve it dynamically.

For example a trigger called "Test Trigger" will automatically create a trigger variable called "gg_trg_Test_Trigger". One could convert the trigger name to its resulting variable by prefixing with "gg_trg_" and then substituting all white spaces ' ' with '_'. One could then look this up in the global root table and resolve the trigger object, from a string. No manual assignment required.

Yes the global root table still has the value assigned explicitly. However that is done with code automatically generated by World Editor, and not manually entered.
You're overcomplicating something fairly trivial for no reason. All of that can be done in 2 triggers. With or without a system, you have to do it manually. You can match the item type to the trigger by matching the array: e.g. MyItemType[1], ItemTrigger[1].
Except in Lua where it can be done from a string. As long as naming is consistent (another source of error) it will work.
You don't need to convert any strings or triggers to strings or triggers. Simple If-statement will do just fine.
If statements are also a lot slower. O(n) complexity as opposed to O(1).
But I'm not even sure why there needs to be a sperate trigger for each item type... maybe the intention could be elaborated some more.
For maintainability. If an item needs triggered code then a trigger can be made, the trigger will be found, the trigger will run and effectively it will automatically be fed into the system for use. If the item does not need a trigger then no trigger will be made, no trigger will be found and no trigger will be run. Each trigger is functionally separate from each other so modifying the mechanics of 1 item will not touch the code of any other item.

This would be especially useful in GUI where disabling triggers causing all actions that reference the trigger, such as setting variables, to also be disabled and not automatically enabled again with the trigger.

It does open up another source of error. If the item name and trigger names do not match then the item system will fail to find the trigger and so not run it.
 
Level 39
Joined
Feb 27, 2007
Messages
5,023
For example a trigger called "Test Trigger" will automatically create a trigger variable called "gg_trg_Test_Trigger". One could convert the trigger name to its resulting variable by prefixing with "gg_trg_" and then substituting all white spaces ' ' with '_'. One could then look this up in the global root table and resolve the trigger object, from a string. No manual assignment required.
I knew I was right.
 
Status
Not open for further replies.
Top