• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Detect Unit/Item Type from code in string

Status
Not open for further replies.
Level 14
Joined
Mar 4, 2009
Messages
1,156
  • Trigger
  • Events
    • Player - Player 1 (Red) types a message containing -item as a Substring
  • Conditions
  • Actions
    • Set String = ((Entered chat string), 7, 10)
    • Set Unit = Your unit
    • Custom script: call CreateItem (S2I(udg_String), GetUnitX(udg_Unit), GetUnitY(udg_Unit))
    • Custom script: call UnitAddItem (udg_Unit, GetLastCreatedItem())
tried everything but it didn't work :confused:

can you do this please?

this is the list of items

I005,I002,I004,I003,I001

set item type variable = first code

this is the list of units

hwt3,Hblm,hpea

set unit type variable = first code



 
I remember a script on WC3C that was doing this, but unfortunately, I can't find it. This trigger should work, but when I display the String variable, it returns 0. Either I can't notice what I'm doing wrong or the conversion is false.
  • Item
    • Events
      • Player - Player 1 (Red) types a chat message containing - as A substring
    • Conditions
    • Actions
      • Set String = (Integer((Substring((Entered chat string), 2, 5))))
      • For each (Integer A) from 1 to 5, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Integer((Substring((Entered chat string), 5, 5)))) Equal to (Integer A)
            • Then - Actions
              • Custom script: call UnitAddItemById(udg_Unit, udg_String)
            • Else - Actions
P.S. "String" is an Integer variable.
 
Level 14
Joined
Mar 4, 2009
Messages
1,156
I remember a script on WC3C that was doing this, but unfortunately, I can't find it. This trigger should work, but when I display the String variable, it returns 0. Either I can't notice what I'm doing wrong or the conversion is false.
P.S. "String" is an Integer variable.
Custom script: call UnitAddItemById(udg_Unit, udg_String)
i think custom script works on order id code that has 14 digits
I005 is not integer and its returns 0
none of this things work :(



if anybody can do this it would be enough

set String = I005
set Item_Type = String
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Well if I remember correctly the old code that you're referring to is from pre-patch 1.24b --I believe it used a function like this for converting from a string to an integer:

JASS:
function s2i takes string s returns integer
    return s
    return 0
endfunction

Since then we have lost this kind of functionality, though nobody here has mentioned anything about StringHash, which is also used to convert a string into an integer. There is also AbilityId which I haven't been able to get working properly.
 
Well if I remember correctly the old code that you're referring to is from pre-patch 1.24b --I believe it used a function like this for converting from a string to an integer:

JASS:
function s2i takes string s returns integer
    return s
    return 0
endfunction

Since then we have lost this kind of functionality, though nobody here has mentioned anything about StringHash, which is also used to convert a string into an integer. There is also AbilityId which I haven't been able to get working properly.

Out of curiosity, what is wrong with S2I()?
JASS:
native S2I  takes string s returns integer
 
Level 14
Joined
Mar 4, 2009
Messages
1,156
i only found this.... but it sucks because it can work ONLY on CUSTOM units

  • Unit - Create 1 (Unit-type((custom_ + CODE))) for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
 
Last edited:
Level 18
Joined
Jan 21, 2006
Messages
2,552
Nothing is wrong with S2I, it does exactly what it is supposed to do. The problem is that if you have a string "A000" and you pass it to the S2I function, it will return 0 because "A" is not a number. The same happens for "'A000'" (notice the extra ' mark).

If you use S2I( I2S( 'A000' ) ) then it will return a string that represents the integer that is represented by 'A000'.

I believe with the old return-bug you could make it return a type-casted value, which I believe is what we are looking for here.
 
Level 14
Joined
Mar 4, 2009
Messages
1,156
Nothing is wrong with S2I, it does exactly what it is supposed to do. The problem is that if you have a string "A000" and you pass it to the S2I function, it will return 0 because "A" is not a number. The same happens for "'A000'" (notice the extra ' mark).

If you use S2I( I2S( 'A000' ) ) then it will return a string that represents the integer that is represented by 'A000'.

I believe with the old return-bug you could make it return a type-casted value, which I believe is what we are looking for here.

tried but i cant make it work :(

help :D
 
Requires ASCII.
JASS:
    function String2Rawcode takes string s returns integer
        local integer i = StringLength(s)
        local integer j = 1
        local integer k = 0
        loop
            exitwhen i == 0
            set i = i-1
            set k = k+Char2Ascii(SubString(s, i, i+1))*j
            set j = j*256
        endloop
        return k
    endfunction
    function blah takes nothing returns nothing
        local string test = "'hpea'"
        local integer id = String2Rawcode(test)
        call BJDebugMsg(I2S(id))
    endfunction
 
Inputting the integer represented by the rawcode works just fine.

As long as you don't need to actually display the values (which in that case you'd need the ASCII lib) then it will be fine.

Example:
JASS:
    if S2I(I2S('hfoo')) == 'hfoo' then
        call BJDebugMsg("hello")
    endif

Will display hello.

So I don't understand why there is a need for longer methods when this method is fully functional. (Though, I don't doubt the uses of those systems =) )
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
Because 'hfoo' is not a string but an integer.

S2I(I2S('hfoo')) == 'hfoo' simplifies to true as
S2I(I2S('hfoo')) becomes 'hfoo' (pointless converting to string and back to integer).
'hfoo' == 'hfoo' becomes 0 == 0 as both values are constants and the same.
0 == 0 becomes true as it is always true.

Again, 'hfoo' is an integer and not a string.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
The problem is not hex, WC3 can easilly support hex via simple type cast functions.

The problem is '0000' is a 32 it integer consisting of 4 bytes (chars). These chars are full 256 bit ascII numbers.

Thus you can and I have even made a unit with type 'H00@' and it is perfectly valid via JNGP.

In the end it is most efficent to store them in a list via arrays. Direct index axcess is hundreds of times faster than type casting can ever be for such a situation with the tools available. Ideally what you would do in C is map the 4 char long string directly to an integer via binary.
 
Status
Not open for further replies.
Top