Snippet Typecasting

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
//////////////////////////////////////////
//       Typecasting 2.0.1
//       by kingking
//  
//  This library provides some type
//  converting functions.
//
//  ====================================
//   Functions provided :
//  ====================================
//  Agent2Widget(agent) -> widget
//  Agent2Group(agent) -> group
//  Agent2Trigger(agent) -> trigger
//  Agent2Timer(agent) -> timer
//  Agent2Location(agent) -> location
//  Agent2Effect(agent) -> effect
//  Agent2Unit(agent) -> unit
//  Agent2Item(agent) -> item
//  Widget2Unit(widget) -> unit
//  Widget2Destructable(widget) -> destructable
//  Widget2Item(widget) -> item
//
//  Due to the usage of CovertFogState in hashtable, I2X
//  is available again.
//  
//  Int2Widget(integer) -> widget
//  Int2Destructable(integer) -> destructable
//  Int2Item(integer) -> item
//  Int2Unit(integer) -> unit
//  Int2Ability(integer) -> ability
//  Int2Timer(integer) -> timer
//  Int2Trigger(integer) -> trigger
//  Int2TriggerCondition(integer) -> triggercondition
//  Int2TriggerAction(integer) -> triggeraction
//  Int2Force(integer) -> force
//  Int2Group(integer) -> group
//  Int2Location(integer) -> location
//  Int2Rect(integer) -> rect
//  Int2Sound(integer) -> sound
//  Int2Effect(integer) -> effect
//  Int2UnitPool(integer) -> unitpool
//  Int2ItemPool(integer) -> itempool
//  Int2Quest(integer) -> quest
//  Int2QuestItem(integer) -> questitem
//  Int2DefeatCondition(integer) -> defeatcondition
//  Int2TimerDialog(integer) -> timerdialog
//  Int2Leaderboard(integer) -> leaderboard
//  Int2Multiboard(integer) -> multiboard
//  Int2MultiboardItem(integer) -> multiboarditem
//  Int2Trackable(integer) -> trackable
//  Int2Dialog(integer) -> dialog
//  Int2Button(integer) -> button
//  Int2TextTag(integer) -> texttag
//  Int2Ubersplat(integer) -> ubersplat
//  Int2Region(integer) -> region
//  Int2FogState(integer) -> fogstate
//  Int2FogModifier(integer) -> fogmodifier
//
//  Requirement :
//  Wc3 1.24b or newer
//  Jasshelper 0.A.2.9 or newer
///////////////////////////////////////
library Typecasting

    globals
        private hashtable Data = InitHashtable()
    endglobals

    //! textmacro Typecasting takes ParentName, parenttype, TypeName, type
    function $ParentName$2$TypeName$ takes $parenttype$ object returns $type$
        call Save$ParentName$Handle(Data,0,0,object)
        return Load$TypeName$Handle(Data,0,0)
    endfunction
    //! endtextmacro
    //! runtextmacro Typecasting ("Agent","agent","Widget","widget")
    //! runtextmacro Typecasting ("Agent","agent","Group","group")
    //! runtextmacro Typecasting ("Agent","agent","Trigger","trigger")
    //! runtextmacro Typecasting ("Agent","agent","Timer","timer")
    //! runtextmacro Typecasting ("Agent","agent","Location","location")
    //! runtextmacro Typecasting ("Agent","agent","Effect","effect")
    //! runtextmacro Typecasting ("Agent","agent","Unit","unit")
    //! runtextmacro Typecasting ("Agent","agent","Item","item")
    //! runtextmacro Typecasting ("Widget","widget","Unit","unit")
    //! runtextmacro Typecasting ("Widget","widget","Destructable","destructable")
    //! runtextmacro Typecasting ("Widget","widget","Item","item")

    //! textmacro Typecasting_I2X takes TypeName, type
    function Int2$TypeName$ takes integer id returns $type$
        call SaveFogStateHandle(Data,0,0,ConvertFogState(id))
        return Load$TypeName$Handle(Data,0,0)
    endfunction
    //! endtextmacro
    //! runtextmacro Typecasting_I2X("Unit", "unit")
    //! runtextmacro Typecasting_I2X("Effect", "effect")
    //! runtextmacro Typecasting_I2X("Trigger", "trigger")
    //! runtextmacro Typecasting_I2X("Timer", "timer")
    //! runtextmacro Typecasting_I2X("Widget", "widget")
    //! runtextmacro Typecasting_I2X("Group", "group")
    //! runtextmacro Typecasting_I2X("Location", "location")
    //! runtextmacro Typecasting_I2X("Item", "item")
    //! runtextmacro Typecasting_I2X("Destructable", "destructable")
    //! runtextmacro Typecasting_I2X("Ability", "ability")
    //! runtextmacro Typecasting_I2X("TriggerCondition", "triggercondition")
    //! runtextmacro Typecasting_I2X("TriggerAction", "triggeraction")
    //! runtextmacro Typecasting_I2X("Force", "force")
    //! runtextmacro Typecasting_I2X("Rect", "rect")
    //! runtextmacro Typecasting_I2X("Sound", "sound")
    //! runtextmacro Typecasting_I2X("UnitPool", "unitpool")
    //! runtextmacro Typecasting_I2X("ItemPool", "itempool")
    //! runtextmacro Typecasting_I2X("Quest", "quest")
    //! runtextmacro Typecasting_I2X("QuestItem", "questitem")
    //! runtextmacro Typecasting_I2X("DefeatCondition", "defeatcondition")
    //! runtextmacro Typecasting_I2X("TimerDialog", "timerdialog")
    //! runtextmacro Typecasting_I2X("Leaderboard", "leaderboard")
    //! runtextmacro Typecasting_I2X("Multiboard", "multiboard")
    //! runtextmacro Typecasting_I2X("MultiboardItem", "multiboarditem")
    //! runtextmacro Typecasting_I2X("Trackable", "trackable")
    //! runtextmacro Typecasting_I2X("Dialog", "dialog")
    //! runtextmacro Typecasting_I2X("Button", "button")
    //! runtextmacro Typecasting_I2X("TextTag", "texttag")
    //! runtextmacro Typecasting_I2X("Image", "image")
    //! runtextmacro Typecasting_I2X("Ubersplat", "ubersplat")
    //! runtextmacro Typecasting_I2X("Region", "region")
    //! runtextmacro Typecasting_I2X("FogState", "fogstate")
    //! runtextmacro Typecasting_I2X("FogModifier", "fogmodifier")
endlibrary


Zinc version :
JASS:
//////////////////////////////////////////
//       Typecasting(Zinc) 2.0.1
//       by kingking
//  
//  This library provides some type
//  converting functions.
//
//  ====================================
//   Functions provided :
//  ====================================
//  Agent2Widget(agent) -> widget
//  Agent2Group(agent) -> group
//  Agent2Trigger(agent) -> trigger
//  Agent2Timer(agent) -> timer
//  Agent2Location(agent) -> location
//  Agent2Effect(agent) -> effect
//  Agent2Unit(agent) -> unit
//  Agent2Item(agent) -> item
//  Widget2Unit(widget) -> unit
//  Widget2Destructable(widget) -> destructable
//  Widget2Item(widget) -> item
//
//  Due to the usage of CovertFogState in hashtable, I2X
//  is available again.
//  
//  Int2Widget(integer) -> widget
//  Int2Destructable(integer) -> destructable
//  Int2Item(integer) -> item
//  Int2Unit(integer) -> unit
//  Int2Ability(integer) -> ability
//  Int2Timer(integer) -> timer
//  Int2Trigger(integer) -> trigger
//  Int2TriggerCondition(integer) -> triggercondition
//  Int2TriggerAction(integer) -> triggeraction
//  Int2Force(integer) -> force
//  Int2Group(integer) -> group
//  Int2Location(integer) -> location
//  Int2Rect(integer) -> rect
//  Int2Sound(integer) -> sound
//  Int2Effect(integer) -> effect
//  Int2UnitPool(integer) -> unitpool
//  Int2ItemPool(integer) -> itempool
//  Int2Quest(integer) -> quest
//  Int2QuestItem(integer) -> questitem
//  Int2DefeatCondition(integer) -> defeatcondition
//  Int2TimerDialog(integer) -> timerdialog
//  Int2Leaderboard(integer) -> leaderboard
//  Int2Multiboard(integer) -> multiboard
//  Int2MultiboardItem(integer) -> multiboarditem
//  Int2Trackable(integer) -> trackable
//  Int2Dialog(integer) -> dialog
//  Int2Button(integer) -> button
//  Int2TextTag(integer) -> texttag
//  Int2Ubersplat(integer) -> ubersplat
//  Int2Region(integer) -> region
//  Int2FogState(integer) -> fogstate
//  Int2FogModifier(integer) -> fogmodifier
//
//  Requirement :
//  Wc3 1.24b or newer
//  Jasshelper 0.A.2.9 or newer
///////////////////////////////////////
//! zinc
library Typecasting
{
    private hashtable Data = InitHashtable();

    //! textmacro Typecasting takes ParentName, parenttype, TypeName, type
    public function $ParentName$2$TypeName$ ($parenttype$ object) -> $type$
    {
        Save$ParentName$Handle(Data,0,0,object);
        return Load$TypeName$Handle(Data,0,0);
    }
    //! endtextmacro
    //! runtextmacro Typecasting ("Agent","agent","Widget","widget")
    //! runtextmacro Typecasting ("Agent","agent","Group","group")
    //! runtextmacro Typecasting ("Agent","agent","Trigger","trigger")
    //! runtextmacro Typecasting ("Agent","agent","Timer","timer")
    //! runtextmacro Typecasting ("Agent","agent","Location","location")
    //! runtextmacro Typecasting ("Agent","agent","Effect","effect")
    //! runtextmacro Typecasting ("Agent","agent","Unit","unit")
    //! runtextmacro Typecasting ("Agent","agent","Item","item")
    //! runtextmacro Typecasting ("Widget","widget","Unit","unit")
    //! runtextmacro Typecasting ("Widget","widget","Destructable","destructable")
    //! runtextmacro Typecasting ("Widget","widget","Item","item")

    //! textmacro Typecasting_I2X takes TypeName, type
    public function Int2$TypeName$ (integer id) -> $type$
    {
        SaveFogStateHandle(Data,0,0,ConvertFogState(id));
        return Load$TypeName$Handle(Data,0,0);
    }
    //! endtextmacro
    //! runtextmacro Typecasting_I2X("Unit", "unit")
    //! runtextmacro Typecasting_I2X("Effect", "effect")
    //! runtextmacro Typecasting_I2X("Trigger", "trigger")
    //! runtextmacro Typecasting_I2X("Timer", "timer")
    //! runtextmacro Typecasting_I2X("Widget", "widget")
    //! runtextmacro Typecasting_I2X("Group", "group")
    //! runtextmacro Typecasting_I2X("Location", "location")
    //! runtextmacro Typecasting_I2X("Item", "item")
    //! runtextmacro Typecasting_I2X("Destructable", "destructable")
    //! runtextmacro Typecasting_I2X("Ability", "ability")
    //! runtextmacro Typecasting_I2X("TriggerCondition", "triggercondition")
    //! runtextmacro Typecasting_I2X("TriggerAction", "triggeraction")
    //! runtextmacro Typecasting_I2X("Force", "force")
    //! runtextmacro Typecasting_I2X("Rect", "rect")
    //! runtextmacro Typecasting_I2X("Sound", "sound")
    //! runtextmacro Typecasting_I2X("UnitPool", "unitpool")
    //! runtextmacro Typecasting_I2X("ItemPool", "itempool")
    //! runtextmacro Typecasting_I2X("Quest", "quest")
    //! runtextmacro Typecasting_I2X("QuestItem", "questitem")
    //! runtextmacro Typecasting_I2X("DefeatCondition", "defeatcondition")
    //! runtextmacro Typecasting_I2X("TimerDialog", "timerdialog")
    //! runtextmacro Typecasting_I2X("Leaderboard", "leaderboard")
    //! runtextmacro Typecasting_I2X("Multiboard", "multiboard")
    //! runtextmacro Typecasting_I2X("MultiboardItem", "multiboarditem")
    //! runtextmacro Typecasting_I2X("Trackable", "trackable")
    //! runtextmacro Typecasting_I2X("Dialog", "dialog")
    //! runtextmacro Typecasting_I2X("Button", "button")
    //! runtextmacro Typecasting_I2X("TextTag", "texttag")
    //! runtextmacro Typecasting_I2X("Image", "image")
    //! runtextmacro Typecasting_I2X("Ubersplat", "ubersplat")
    //! runtextmacro Typecasting_I2X("Region", "region")
    //! runtextmacro Typecasting_I2X("FogState", "fogstate")
    //! runtextmacro Typecasting_I2X("FogModifier", "fogmodifier")
}
//! endzinc
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Text Macros:
Helping the lazy since... Wait, when was vJASS made? o_O
:p

I'd test it, if I knew what typecasting was. >.<
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
In 1.24 or higher, typecasting like this is not allowed :

JASS:
function H2I takes handle h returns integer
    return h
    return 0
endfunction


Textmacro... :D
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
JASS:

function $ParentName$2$TypeName$ takes $parenttype$ object returns $type$
    local integer key = GetHandleId(object)
    call Save$ParentName$Handle(Data,key,0,object)
    return Load$TypeName$Handle(Data,key,0)
endfunction

Wouldn't it make more sense, for efficiency purposes, to just do:
JASS:

function $ParentName$2$TypeName$ takes $parenttype$ object returns $type$
    call Save$ParentName$Handle(Data,0,0,object)
    return Load$TypeName$Handle(Data,0,0)
endfunction

I mean, what's the point of the key? Does it really matter where an object is in a private hashtable that isn't being used for storage purposes anyway?




I can't really see the point of this though. Typecasting in previous versions was done more or less exclusively for attachment purposes.
 

Azlier

Old World Ghost
Reaction score
461
The only typecasting I've ever actually needed was W2D and W2I. Not having a GetTriggerItem native is annoying.
 

Romek

Super Moderator
Reaction score
963
GetManipulatedItem is basically an oddly named GetTriggerItem.
GetTriggerDesuctructable was added recently.

I don't see much purpose in typecasting anymore.

Uberfoop speaks the truth about the key. I was going to mention it myself.
 

Azlier

Old World Ghost
Reaction score
461
GetManipulatedItem doesn't exactly get an item that died.

And, yes, GetTriggerDestructable was a good move by Blizzard.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:

function $ParentName$2$TypeName$ takes $parenttype$ object returns $type$
    local integer key = GetHandleId(object)
    call Save$ParentName$Handle(Data,key,0,object)
    return Load$TypeName$Handle(Data,key,0)
endfunction

Wouldn't it make more sense, for efficiency purposes, to just do:
JASS:

function $ParentName$2$TypeName$ takes $parenttype$ object returns $type$
    call Save$ParentName$Handle(Data,0,0,object)
    return Load$TypeName$Handle(Data,0,0)
endfunction

I mean, what's the point of the key? Does it really matter where an object is in a private hashtable that isn't being used for storage purposes anyway?




I can't really see the point of this though. Typecasting in previous versions was done more or less exclusively for attachment purposes.

Ops, sorry, my bad.
 

Jesus4Lyf

Good Idea™
Reaction score
397
GetManipulatedItem doesn't exactly get an item that died.
Or what if you wanted to write a DestroyAgent API? But yes, downcasting widgets is the main use of this, with GetTriggerWidget (there's still no GetTriggerItem native, right?).

This seems like a practical thing to have. If there's no objections, I'll approve it.

It seems to be perfectly written.
 

Tru_Power22

You can change this now in User CP.
Reaction score
144
The only typecasting I've ever actually needed was W2D and W2I. Not having a GetTriggerItem native is annoying.

What about getting entering unit (for a unit comes within range event)? Don't you need typecasting to get that unit?
 

Jesus4Lyf

Good Idea™
Reaction score
397
What about getting entering unit (for a unit comes within range event)? Don't you need typecasting to get that unit?

There is no native from which to get the center unit, typecasted result or not...

(...that I know of. Wonder if typecasting GetTriggerWidget returns anything interesting? XD)
 

Azlier

Old World Ghost
Reaction score
461
>Approved!

Yes. Especially after the return bug is suddenly usable again. :rolleyes:

This is a 'safe' alternative, I guess.
 

saw792

Is known to say things. That is all.
Reaction score
280
Zinc version is totally useless. It just changed about 3 lines, and you still have a private keyword in there (not necessary in zinc, everything is automatically private).
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
In this case, it is same either private or just leave it there. lol

When complied, it is same.
 

Jesus4Lyf

Good Idea™
Reaction score
397
As long as the vJass doesn't disappear, Zinc doesn't hurt...

Just want to say that Zinc is not standard here yet either, even though it compiles with JassHelper. Vex can change it substancially at any moment, it is still young (more a warning for you in your own maps, that you should keep vJass for now in case Vex breaks backwards compatability on Zinc). :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top