• 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.

Simple Save Demo

Just a demonstration of simple save using a variety of small resources.

Using snippets is easier than using a save/load system, more dynamic than using a save/load system, and has more power than using a save/load system. All save/load systems are officially deprecated to save/load snippets.

JASS:
struct Demo extends array
    private static Base encryptionKey
    
    private static method save takes nothing returns boolean
        local NumberStack stack = NumberStack.create(encryptionKey)     //create a number stack
        local string encrypted
        
        //push numbers on to stack
        call stack.push(10,64)      //10
        call stack.push(20,31)      //20,10
        
        //display numbers in stack
        call DisplayTimedTextToPlayer(GetTriggerPlayer(),0,0,60,"Saving: 10,20")
        
        //encrypt number as a string
        set encrypted=EncryptNumber(stack,1000000,3,GetPlayerId(GetTriggerPlayer()),"salt value",.85)
        
        //add dashes
        set encrypted=AddRepeatedString(encrypted,"-",4)
        
        //color
        set encrypted=ColorCodeString(encrypted, "40e0d0", "ff69b4", "00AA00", "ffff00")
        
        //display code
        call DisplayTimedTextToPlayer(GetTriggerPlayer(),0,0,60,encrypted)
        
        //destroy stack
        call stack.destroy()
        
        return false
    endmethod
    
    private static method load takes nothing returns boolean
        local string s=GetEventPlayerChatString()       //retrieve string
        local NumberStack stack
        
        set s=RemoveString(s,GetEventPlayerChatStringMatched(),1)       //remove "-load "
        set s=RemoveString(s," ",0)                                     //remove " "
        set s=RemoveString(s,"-",0)                                     //remove "-"
        
        if (IsStringLengthInRange(s,5,9)) then
            //retrieve stack of numbers
            set stack = DecryptNumber(s,encryptionKey,1000000,3,GetPlayerId(GetTriggerPlayer()),"salt value",.85)
            
            //if stack isn't 0, code is valid
            if (0!=stack) then
                //display values stored in stack (reverse order)
                call DisplayTimedTextToPlayer(GetTriggerPlayer(),0,0,60,"Loaded: "+I2S(stack.pop(31))+","+I2S(stack.pop(64)))
            else
                call DisplayTimedTextToPlayer(GetTriggerPlayer(),0,0,60,"Invalid Code")
            endif
        else
            call DisplayTimedTextToPlayer(GetTriggerPlayer(),0,0,60,"Invalid Code")
        endif

        return false
    endmethod
    private static method onInit takes nothing returns nothing
        //register save/load commands
        local integer i=11
        local trigger t=CreateTrigger()     //save
        local trigger t2=CreateTrigger()    //load
        call TriggerAddCondition(t,Condition(function thistype.save))
        call TriggerAddCondition(t2,Condition(function thistype.load))
        loop
            call TriggerRegisterPlayerChatEvent(t,Player(i),"-save",true)
            call TriggerRegisterPlayerChatEvent(t2,Player(i),"-load ",false)
            exitwhen 0==i
            set i=i-1
        endloop
        
        //this is the encryption key
        set encryptionKey=Base["0123456789abcdefghijklmnopqrstuvwxyz"]
    endmethod
endstruct
 

Attachments

  • Simple Save.w3x
    60.4 KB · Views: 60
Last edited:
You can also save things in the same fashion as Encoder using this.

For example
JASS:
if (GetHeroLevel(unit)<max) then
    call stack.push(xpPercent,99)
endif
call stack.push(GetHeroLevel(unit),max)

JASS:
local integer level = stack.pop(max)
local integer xpPercent
if (level < max) then
    set xpPercent=stack.pop(99)
endif

For that dynamic stuff, it is easier to use Encoder than to do it yourself ;P, but if you are only doing simple dynamic stuff like the one above, then you could still stick with this. If you wanted to save something like hero specific items with item charges, then you'll want to move to Encoder because the code will look horrible and chaotic in this =).

edit
added more save/load functions to it
->SaveInventory
->SaveItemCharges
 
Last edited:
Added 5 detailed sample catalogs


General Items
Hero Specific Items
Hero Specific Items with Slots (chest -> chest slot, boots -> boots slot)
Item Catalogs Across Versions (adding new items to the map in a new release?)
Hero Specific Items with Slots Across Versions


And believe me, backwards compatibility has never been easier!


Sample of loading an item from the Hero Specific Items with Slots Across Versions Catalog
local integer itemTypeId = Items.version[heroTypeId][Items.TYPE].raw(stack.pop(Items.version[heroTypeId][Items.TYPE].count))
 
Top