best way to know if an unitid is valid or not ?

Troll-Brain

You can change this now in User CP.
Reaction score
85
Because of this bug it isn't so easy.

JASS:
library IsUnitIdValid

globals
   private unit U
endglobals

function IsUnitIdValid takes integer i returns boolean
    if IsGameLoaded() then // ofc i plan to store the result, to avoid the creation of the unit all the time, when it was already checked
                           // but i'm waiting for hashtables available on the official patch
        set U = CreateUnit(Player(0),i,0.,0.,0.) // yeah i should use a dummy player and constants reals X/Y instead
        
        if GetWidgetLife(U) != 0 then
            call RemoveUnit(U)
            return true
        else
            return false
        endif
        
    else
    
        return UnitId2String(i) != null
    endif
endfunction

endlibrary


Better ideas ?

PS : I don't want to store a boolean manually for each unitid possible on the map

EDIT : It seems that IssueBuildOrderById() always returns true while the arguments are "valids" (existent unit and order).
I mean it returns true for any existent order, "move" for example ...
 

Tom Jones

N/A
Reaction score
437
I've read the entire thread from the hyperlink, and I still don't understand the bug? Anyways:
JASS:
function ... takes integer id returns boolean
     return GetObjectName(id) == null
endfunction
You can't input null strings as object names as far as I remember.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
I've read the entire thread from the hyperlink, and I still don't understand the bug? Anyways:
JASS:
function ... takes integer id returns boolean
     return GetObjectName(id) == null
endfunction
You can't input null strings as object names as far as I remember.

Erf.
I hope you will understand :
http://www.wc3c.net/showpost.php?p=1060727&postcount=38
Donwload the test map, give orders, save the game, load it , give orders.

And GetObjectName is not bugged but using it, you can't know if an order is a build order or not.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
@Faust :
Sorry, but where is the better idea ?
IsUnit maybe ? I don't have the editor now, but if this exist, sure i will use it instead of checking the life.
But still the worst thing is the unit creation, maybe there is not any other way.

@Jesus4Lyf :
If it's a sarcasm, the point is to know if an order is a training one or not.
If you're serious then explain more ...
 
Reaction score
341
Well if you worried about speed of the unit creation, you can do something like this;

JASS:
globals
    private unit u
    private hashtable hash = InitHashtable()
endglobals

function ValidId takes integer id returns boolean
    local boolean ret
    if not HaveSavedBoolean(hash, id, 0) then
        set u = CreateUnit(....)
        set ret = (GetWidgetLife(u) < 0.405) // IsUnit takes two arguments?
        call RemoveUnit(u)
        call SaveBoolean(hash, id, 0, ret)
        return ret
    endif
    return LoadBoolean(hash, id, 0)
endfunction


Wrote that in the quick reply box, so sorry if theres any errors.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Well if you worried about speed of the unit creation, you can do something like this;

JASS:

globals
    private unit u
    private hashtable hash = InitHashtable()
endglobals

function ValidId takes integer id returns boolean
    local boolean ret
    if not HaveSavedBoolean(hash, id, 0) then
        set u = CreateUnit(....)
        set ret = (GetWidgetLife(u) < 0.405) // IsUnit takes two arguments?
        call RemoveUnit(u)
        call SaveBoolean(hash, id, 0, ret)
        return ret
    endif
    return LoadBoolean(hash, id, 0)
endfunction


Wrote that in the quick reply box, so sorry if theres any errors.

If you had read the comments inside the code i've posted you would know that i plan to use something like that.
Speed is not the only one issue, an event "unit enter in ..." can fire for nothing and more other events.
I would prefer to avoid totally the unit creation, but maybe it's impossible unless you store at the map init all booleans for each unitid possible.
 

Jesus4Lyf

Good Idea™
Reaction score
397
the point is to know if an order is a training one or not.
Ah, I see. But a train order isn't about to happen while the map is loading, is it? So does the bug really matter?

(Obviously for a train order to be issued on map init, it would need to be triggered, which means you don't need to detect it because, well, you already made it happen yourself and could just script the effect to happen there.)
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Ah, I see. But a train order isn't about to happen while the map is loading, is it? So does the bug really matter?
Because it's a public resource, yes it does matter :p

(Obviously for a train order to be issued on map init, it would need to be triggered, which means you don't need to detect it because, well, you already made it happen yourself and could just script the effect to happen there.)
I plan to finish GetWorker (the unit which is making the build).
And in the process i need to catch building orders, but don't interact with them.
I don't understand why you are talking about map init. :confused:
 

jwallstone

New Member
Reaction score
33
Actually one way to tell if it's valid is to just look at the integer itself. All unit id's begin with h, o, u, e, or n. Now, I've seen a post somewhere that talks about this, but all unit id's are essentially just base 256 representations of the integer, so you could just do a simple mathematical test in this case.

EDIT: Base 256, with 94 valid chars apparently.
http://www.wc3c.net/showthread.php?t=106982

So, one simple solution is to just write code to display the following:
JASS:
call BJDebugMsg(I2S('h000'))
call BJDebugMsg(I2S('o000'))
call BJDebugMsg(I2S('e000'))
call BJDebugMsg(I2S('u000'))
call BJDebugMsg(I2S('n000'))


Then, to test if it's a valid unit id, just mod out all the lower digits (modulo the int you're testing with whatever '1000' is, and subtract that remainder from the original int). Then, if the remaining integer is one of the 5 values you displayed above, it's a valid unit id.
 
Reaction score
341
Actually one way to tell if it's valid is to just look at the integer itself. All unit id's begin with h, o, u, e, or n. Now, I've seen a post somewhere that talks about this, but all unit id's are essentially just base 256 representations of the integer, so you could just do a simple mathematical test in this case.

People make custom units :nuts:
 

jwallstone

New Member
Reaction score
33
Yeah, and all custom units also begin with one of those letters. Or have you never looked at the id's of custom units yourself?

Think before you make sarcastic comments.
 

Jesus4Lyf

Good Idea™
Reaction score
397
to test if it's a valid unit id, just mod out all the lower digits (modulo the int you're testing with whatever '1000' is, and subtract that remainder from the original int).
Just divide? By '1000' or whatever? :p

Also, heroes are H, O, U, E or N I believe.

There's a good chance this would work. But I believe rawids can use anything, even v for example ('v000'). In which case there's no chance this would work. :p

I would opt for caching the success of CreateUnit unless you can use some of those natives to test attributes of unittypes. Actually, could you test for the "dead" attribute of a unit type using IsUnit or whatever it is? Because if that worked... You could inline to a native. XD
 

saw792

Is known to say things. That is all.
Reaction score
280
Just to clear this up for both Jesus and Troll, the post Troll linked to was testing whether the map had been saved and reloaded, not whether the map was currently at the loading screen.

Also, any 4 digit code should be valid for an object id when set through Newgen.
 
Reaction score
341
Also, any 4 digit code should be valid for an object id when set through Newgen.

Yea but I think he wants to know if the entered id is a unit. For example, the id 'ratf' is Claws of attack (item). If someone inputs that value in the function it will return false because it's not a unit id.
 

Jesus4Lyf

Good Idea™
Reaction score
397
use some of those natives to test attributes of unittypes. Actually, could you test for the "dead" attribute of a unit type using IsUnit or whatever it is? Because if that worked... You could inline to a native. XD
Try something like this...
JASS:
function IsRawcodeUnitType takes integer rawcode returns boolean
    return IsUnitIdType(rawcode,UNIT_TYPE_FLYING)!=IsUnitIdType(rawcode,UNIT_TYPE_GROUND)
endfunction

Edit: Doesn't work yet, I'm still trying.
Edit: Gosh, it's hopeless. None of the classifications apply to statis wards. :(

Edit: MEH...
JASS:
function IsUnitTypeId takes integer id returns boolean
    set bj_lastCreatedUnit=CreateUnit(Player(15),id,0,0,0)
    if bj_lastCreatedUnit==null then
        return false
    endif
    call RemoveUnit(bj_lastCreatedUnit)
    return true
endfunction

Tested and works. <_<
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Well, so i guess i will keep the code like that. (already posted in the first post of this thread).
And do the little optimisation suggested by kingkingyyk3
Anyway thx for tryings.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top