[Tutorial] - Memory Leaks and Custom Script

Tom_Kazansky

<b><font color="RoyalBlue"><font face="Lucida Cons
Tham gia ngày
28/12/06
Bài viết
3,454
Reaction score
462
Memory Leaks là gì ? Memory Leaks là sự thất thoát (leak) những "handle object" (tạm dịch là vật có tính chất "handle", hay nói cách khác có type handle). Có rất nhiều type: unit, group (unit group), location (point), region, floating text,... nhưng những type kia đều là handle. Bất kì lúc nào, bạn tạo ra một handle thì bạn đã mất một phần bộ nhớ (trong trường hợp này có thể nói là RAM). Nếu bạn dùng xong handle này mà ko remove đi thì bạn đã bị thất thoát bộ nhớ và nếu thất thoát quá nhiều sẽ gây ra lag, rất nghiêm trọng.

JASS là ngôn ngữ trong Warcraft, dùng để "scripting" (tôi chả dịch nổi, sorry) Map và AI. Trong đó trigger mà mọi người hay sử dụng là GUI (Graphic User Inteface - giao diện đồ họa cho người sử dụng), tức là phần giao diện của JASS.

Custom Script đây là 1 "action" trong GUI cho phép ta viết 1 dòng JASS. Vì sự hạn chế của GUI nên ta phải sử dụng action này để "viết 1 dòng JASS" trong GUI và để xóa hết các "thất thoát bộ nhớ" (Memory Leaks)

----

Các thứ có thể gây ra memory leak:

Special Effects
Groups <- unit group
Points <- Location
Units
Regions
Forces <- player group
Lightning Effects
Floating Text
Countdown Timers
(Còn nữa nhưng những thứ trên là những thứ hay đc dùng)

Bây giờ Tom sẽ lấy ví dụ về các leak trên và làm thế nào để xóa.

Unit Groups
Mã:
Unit Group Bad
    Events
    Conditions
    Actions
        Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
            Loop - Actions
                Unit - Kill (Picked unit)
Đó là leak group. Cách remove:
Mã:
Unit Group Good
    Events
    Conditions
    Actions
        Set Temp_Group = (Units in (Playable map area))
        Unit Group - Pick every unit in Temp_Group and do (Actions)
            Loop - Actions
                Unit - Kill (Picked unit)
        Custom script:   call DestroyGroup (udg_Temp_Group)
Đầu tiên tạo 1 variable có type là group, tên là Temp_Group (cái này thì tùy thôi) sau đó đặt variable này là group cần pick. Pick unit trong group variable đó, chạy action và destroy group variable trên. Các bạn nhớ rằng tên variable khi chuyển sang JASS sẽ phải thêm "udg_" ở đằng trước.

Points

Mã:
Point Bad
    Events
    Conditions
    Actions
        Unit - Create 1 Footman for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
Tạo 1 footman ở "giữa bản đồ", leak point "giữa bản đồ", leak này thật ra ko lớn nhưng nếu nhiều sẽ thành lớn. Mà các bạn biết ko, point đc sử dụng rất nhiều.
"Giải quyết" cái point trên:
Mã:
Point Good
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Unit - Create 1 Footman for Player 1 (Red) at Temp_Point facing Default building facing degrees
        Custom script:   call RemoveLocation (udg_Temp_Point)
Tạo 1 point variable trên là Temp_Point (tên thì tùy), set variable trên là point cần sử dụng, tạo unit ở point đó (sử dụng point này) và rồi remove point này.

Special Effects
Mã:
Special Effect Bad
    Events
    Conditions
    Actions
        Special Effect - Create a special effect at (Center of (Playable map area)) using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl
Có 2 leak, một là leak point, hai là leak special effect. Thật ra trong GUI đã có action để destroy special effect rồi. (Special Effect - Destroy Special Effect)
Mã:
Special Effect Good 1
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl
        Special Effect - Destroy (Last created special effect)
        Custom script:   call RemoveLocation (udg_Temp_Point)
Chú ý là có vài special effect, nếu destroy luôn ngay sau khi đc tạo ra thì sẽ ko có gì cả (Thunder Clap thì ko sao). Vì vậy ta phải đợi 1 chút rồi mới destroy.

Mã:
Special Effect Good 2
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Undead\UnholyAura\UnholyAura.mdl
        Set Temp_SFX[(Integer A)] = (Last created special effect)
        Custom script:   call RemoveLocation (udg_Temp_Point)
        Wait 2.00 seconds
        Special Effect - Destroy Temp_SFX[(Integer A)]
Ta có 1 special effect variable tên là Temp_SFX.
Tạo 1 special effect ở giữa bản đồ, đợi 2 giây rồi destroy effect đó.

Units
Mã:
Units Bad
    Events
    Conditions
    Actions
        Unit - Create 1 Dummy Caster for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees
Có 2 leak, một là point leak, hai là unit leak, dummy unit đc tạo ra xong thì ở đó, ko bị remove -> leak.
Mã:
Units Good
    Events
    Conditions
    Actions
        Set Temp_Point = (Center of (Playable map area))
        Unit - Create 1 Dummy Caster for Player 1 (Red) at Temp_Point facing Default building facing degrees
        Custom script:   call RemoveLocation (udg_Temp_Point)
        Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
Lệnh Unit - Add Timed Life thì đc dùng cho dummy unit, cái này hầu như ai dùng dummy unit cũng biết và đây là remove unit leak rồi.
That pretty much covers all the major leaks problems people have in their maps. Here are a few more custom script functions you can use to remove other leaks that sometimes occur:

Mã:
 Custom script: call DestroyForce( udg_Your_Variable ) //Player Group

Mã:
 Custom script:   call RemoveRect(udg_Your_Variable) //Region

Mã:
 Custom script: call DestroyLightning( udg_Your_Variable ) //Lightning Effect

Mã:
Custom script: call DestroyTextTag( udg_Your_Variable ) //Floating Text

This is one of the only things that can also be easily destroyed in GUI. There ar two way of doing this, each are equally effective.

Mã:
Floating Text - Change the lifespan of (Last created floating text) to 5.00 seconds
Floating Text - Destroy (Last created floating text)

Dòng lệnh 1 cũng tương đương với Unit - Add Timed Life nhg sử dụng với floating text. Sử dụng dòng 2 nếu bạn muốn tự tay xóa floating text.


Mã:
 Custom script: call DestroyTimer( udg_Your_Variable ) //Countdown Timer

Mã:
 Custom script:   call DestroyTrigger( GetTriggeringTrigger() )
Dòng lệnh trên để destroy trigger, nhưng thật ra với GUI user thì nó ko cần thiết.
---
Kết thúc tutorial, ta có trigger sau:
Mã:
Final Good Trigger
    Events
        Unit - A unit Starts the effect of an ability
    Conditions
        (Ability being cast) Equal to Animate Dead
    Actions
        Set Temp_Point = (Position of (Casting unit))
        Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl
        Special Effect - Destroy (Last created special effect)
        Set Temp_Group = (Units within 512.00 of Temp_Point matching (((Matching unit) is A structure) Equal to False))
        Custom script:   call RemoveLocation (udg_Temp_Point)
        Unit Group - Pick every unit in Temp_Group and do (Actions)
            Loop - Actions
                Set Temp_Point = (Position of (Picked unit))
                Unit - Create 1 Dummy Caster for (Owner of (Casting unit)) at Temp_Point facing Default building facing degrees
                Unit - Order (Last created unit) to Human Priest - Inner Fire (Picked unit)
                Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                Custom script:   call RemoveLocation (udg_Temp_Point)
        Custom script:   call DestroyGroup (udg_Temp_Group)

Tutorial đã hết, Tom hi vọng các bạn hiểu :D

Credit bài viết này dựa theo Memory Leaks tutorial của emjlr trên thehelper ( link: Memory Leak Tutorial )
 
CẢm ơn anh nhiều lắm. Từ trước đến giờ em chỉ sài mà vừa lo vừa sợ vì chỉ hiểu sơ sơ. giờ hiểu kỉ rồi>>> À anh ơi. trong theHelper cũng hay nhưng toàn tiếng anh . Hay là anh dịch bài của emjlr về vấn đề Jass đi anh. ông emjlr có nhiều bài hướng dẫn nhưng em đọc ko hiểu hết đc vì vốn tiếng anh còn hạn hẹp. Giúp em:p
 
Ồ thanks anh Tom nhiều, chỉ khoái vụ này nhất :)) chém hết, ko chừa thằng giặc vô dụng nào ở lại trong bộ nhớ :D
 
Ồ thanks anh Tom nhiều, chỉ khoái vụ này nhất :)) chém hết, ko chừa thằng giặc vô dụng nào ở lại trong bộ nhớ :D

Hi hi ;;) nói mạnh miệng wow' ... Vậy là giờ có thế hướng dẫn bài bản cho em rồi nhỉ Anh Sukaraki!!! :D:D:D:D:D
 
anh tom cho hỏi tí

---Khi ta làm mấy đoạn Cinematic( Fan camara , Tranmission from unit , Cinematic mode....) thì nó có gây ra hiện thất thoát bộ nhớ kô , nếu có thì làm thế nào để khắc phục đây :D ( Sao em thấy mỗi lần làm mấy đoạn Cinematic là khi chạy nó lag hơn tí)
 
Cinematic:
Hầu như ko có, nếu có chỉ là leak point thôi:
Ping Minimap
Ping Minimap with Color
Hai lệnh trên đều có dùng point, cho nên có thể leak point.

Camera
Các Pan Camera đều có sử dụng đến point, nên việc leak point là rất có thể.
Cả lệnh Set Spacebar-Point cũng dùng point.
 
Vậy làm sao để xóa chúng đi bây h hã anh
 
Back
Top