System The Shortest Save/Load Code Ever

Reaction score
341
I Like your codes length and all. But it bugs once i gather 6 items in your demo map.

It loads either no items or a bunch of different items. How can you not be getting this bug, I change nothing in the code.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,497
It uses "call SetRandomSeed(seed)" in several places... with a fixed value.
This would also be the only save / load code doing so.

Well, I'm sure there's a valid reason... :p

Poor cleeeeez though was working out a "bug" in his map for about a week.
As such, the description should probably mention this.
Or make sure it can't happen.
 

Korolen

New User (Why do I keep getting those red bars?)
Reaction score
69
It uses "call SetRandomSeed(seed)" in several places... with a fixed value.
This would also be the only save / load code doing so.

Well, I'm sure there's a valid reason... :p

Poor cleeeeez though was working out a "bug" in his map for about a week.
As such, the description should probably mention this.
Or make sure it can't happen.

Kode seeds the random generator based on a player's name, and then uses the "random" results to start changing the code around to encrypt it. To decrypt, it calculates the same seed based on the player's name and thus the "random" numbers it gets when decrypting turn out to be the same ones it got when encrypting. I thought it was pretty ingenious...

Anyway, it does nothing to reset the random number generator afterwords, so it pretty much hijacks any randomness you happened to be doing at the time.

If I had time I'd release an update to Kode with lots of improvements, but right now, I don't really feel the temptation – I haven't opened the World Editor for more than a few minutes at a time in many, many months.

At this point, Kode gets the job done. It makes codes. I leave it up to its users to integrate it into their maps. Kode is a technology; my original intent in publishing it was just to share what I had just spent days making. Anyone expecting something other than that will be disappointed.

By the way, the issue with saving six items: the item_count variable should have a maximum value of 7, not 6, as 0 means "no items." Just change the sixes to sevens and it'll work fine. (Acehart, please refrain from harping on me about how if there wasn't a fixed maximum value for saving numbers, then there wouldn't be that bug. It was seriously easy to find, especially when you make Kode complain when you try to save a number beyond its maximum.)

EDIT: Just to clarify, what I mean is change push_int(item_count, 6) to push_int(item_count, 7) and set item_count = pop_int(6) to set item_count = pop_int(7). This is all done in the JASS save and load triggers for Demo 3.
 
Reaction score
341
I am obviously doing something wrong. I Tried saving and loading but the values just got all messed up.... :rolleyes:

JASS:
library Kode initializer InitTrig uses KodeBase

private function Item2Int takes integer t returns integer
    local integer i = 0
    loop
        exitwhen i >= MAX_ITEMS
        if udg_Items<i> == t then
            return i
        endif
        set i = i + 1
    endloop
    return -1
endfunction

function Save takes player p returns nothing
    local integer i = GetPlayerId(p)
    local integer l = 0
    call clear_bits()
    call push_int(GetHeroLevel(Hero<i>), 50)
    loop
        exitwhen l &gt; 5
        call push_int(Item2Int(GetItemTypeId(UnitItemInSlot(Hero<i>, i))), MAX_ITEMS)
        set l = l + 1
    endloop
    call push_int(GetHeroStr(Hero<i>, false), 300)
    call push_int(GetHeroInt(Hero<i>, false), 300)
    call push_int(GetHeroAgi(Hero<i>, false), 300)
    call push_int(Toughness<i>, 300)
    call push_int(Initiative<i>, 300)
    call push_int(Willpower<i>, 300)
    call push_int(Luck<i>, 300)
    call DisplayTextToPlayer(p, 0, 0, format_code(create_code(p, 10), 4))
endfunction

private function LoadHero takes nothing returns nothing
    local integer id = GetPlayerId(GetTriggerPlayer())
    local integer i  = 0
    local integer l  = 0
    set Hero[id]     = CreateUnit(GetTriggerPlayer(), &#039;HERO&#039;, -10569, 9786, 0)
    
    call SetHeroLevel(Hero[id],  pop_int(50), false) 
    loop
        exitwhen i &gt; 5
        set l = pop_int(MAX_ITEMS)
        call UnitAddItemById(Hero[id], udg_Items[l])
        set i = i + 1
    endloop
    call SetHeroStr(Hero<i>, pop_int(300), false)
    call SetHeroAgi(Hero<i>, pop_int(300), false)
    call SetHeroInt(Hero<i>, pop_int(300), false)
    set Toughness<i> = pop_int(300)
    set Initiative<i> = pop_int(300)
    set Willpower<i> = pop_int(300)
    set Luck<i> = pop_int(300)
endfunction

private function Load takes nothing returns nothing
    call LoadHero()
endfunction
//===========================================================================
private function InitTrig takes nothing returns nothing
    local trigger t2 = CreateTrigger()
    local integer i = 0
    loop
        exitwhen i &gt; 11
        call TriggerRegisterPlayerChatEvent(t2, Player(i), &quot;-load&quot;, false)
        set i = i + 1
    endloop
    call TriggerAddAction(t2, function Load)
endfunction

endlibrary

</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>
 

Korolen

New User (Why do I keep getting those red bars?)
Reaction score
69
I am obviously doing something wrong. I Tried saving and loading but the values just got all messed up.... :rolleyes:

JASS:
library Kode initializer InitTrig uses KodeBase

private function Item2Int takes integer t returns integer
    local integer i = 0
    loop
        exitwhen i &gt;= MAX_ITEMS
        if udg_Items<i> == t then
            return i
        endif
        set i = i + 1
    endloop
    return -1
endfunction

function Save takes player p returns nothing
    local integer i = GetPlayerId(p)
    local integer l = 0
    call clear_bits()
    call push_int(GetHeroLevel(Hero<i>), 50)
    loop
        exitwhen l &gt; 5
        call push_int(Item2Int(GetItemTypeId(UnitItemInSlot(Hero<i>, i))), MAX_ITEMS)
        set l = l + 1
    endloop
    call push_int(GetHeroStr(Hero<i>, false), 300)
    call push_int(GetHeroInt(Hero<i>, false), 300)
    call push_int(GetHeroAgi(Hero<i>, false), 300)
    call push_int(Toughness<i>, 300)
    call push_int(Initiative<i>, 300)
    call push_int(Willpower<i>, 300)
    call push_int(Luck<i>, 300)
    call DisplayTextToPlayer(p, 0, 0, format_code(create_code(p, 10), 4))
endfunction

private function LoadHero takes nothing returns nothing
    local integer id = GetPlayerId(GetTriggerPlayer())
    local integer i  = 0
    local integer l  = 0
    set Hero[id]     = CreateUnit(GetTriggerPlayer(), &#039;HERO&#039;, -10569, 9786, 0)
    
    call SetHeroLevel(Hero[id],  pop_int(50), false) 
    loop
        exitwhen i &gt; 5
        set l = pop_int(MAX_ITEMS)
        call UnitAddItemById(Hero[id], udg_Items[l])
        set i = i + 1
    endloop
    call SetHeroStr(Hero<i>, pop_int(300), false)
    call SetHeroAgi(Hero<i>, pop_int(300), false)
    call SetHeroInt(Hero<i>, pop_int(300), false)
    set Toughness<i> = pop_int(300)
    set Initiative<i> = pop_int(300)
    set Willpower<i> = pop_int(300)
    set Luck<i> = pop_int(300)
endfunction

private function Load takes nothing returns nothing
    call LoadHero()
endfunction
//===========================================================================
private function InitTrig takes nothing returns nothing
    local trigger t2 = CreateTrigger()
    local integer i = 0
    loop
        exitwhen i &gt; 11
        call TriggerRegisterPlayerChatEvent(t2, Player(i), &quot;-load&quot;, false)
        set i = i + 1
    endloop
    call TriggerAddAction(t2, function Load)
endfunction

endlibrary

</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>


Not to be rude, but your problem is pretty basic. The Kode guide should help you out.

Korolen said:
You MUST retrieve the numbers in reverse order they were put onto the code, and the maximum values must match exactly what they in the saving. For example, if you store a hero level, then the player's gold and lumber, you would pop off the lumber, then the gold, and then the hero level.
 

Rocker742

New Member
Reaction score
0
uhm I know this may sound stupid, but what do you mean by execute? like go to the trigger? I'm probably overthinking but it got me confused
 

Korolen

New User (Why do I keep getting those red bars?)
Reaction score
69
uhm I know this may sound stupid, but what do you mean by execute? like go to the trigger? I'm probably overthinking but it got me confused

Code:
Trigger - Run (Ignoring Conditions)

Look at the code in the demos for examples.
 

No_exit

Regular User (What is Custom User Title?)
Reaction score
40
Ok I managed to read through Kode's code and get a good idea of how the system works.

The system is indeed extremely efficient and probably the shortest load code made so far in average (sorry had to put it in bold, I don't want AceHart's anger over me :p) but I don't fully agree with the following quote:

It wastes *zero* space when saving normal integers because they have an arbitrary maximum size.

This might be true in theory, but in practice this will not be the case.
A lot of maps (especially big RPGs) release their map which they know is not their final version. This means that the creators generally have no idea for example how many items their final map will have. This will lead to one of the following two scenarios:
- You say to the save code that the max amount of items you can have is the max of the current map. This means that if you add more items in your next version of your map, the previous codes will no longer work or in other words: the players will have to start a new hero which is very painful imo.
- You overestimate the max amount of items (generally by some safe amount to make sure that no code reset ever happens) which will make you lose some space.
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
Whats with this GetRandomInt Problem? I don't really understood this.

I want to use Kode, but I'm not sure how it will affect my map, since I use a lot of random Ints ...

Can someone explain to me what this Seed thingy means and how it works?
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,497
Let's take the following list: 2, 4, 5, 1, 3.

Imagine that's the entire and complete list of all possible random numbers.
The random number generator will give you those, in the mentioned order.

Now, when the map starts, he will decide on where he's going to start in the list.
First time you play, it might be the given order.
Next time, it might be 5, 1, 3, 2, 4.
Still the same numbers, but you start somewhere else.
That somewhere else is the seed.

And, when you get the same seed as previously, the numbers will come in exactly the same order as previously.


Well, strange as it may seem, that's how random numbers work.
Fortunately, the list has about 4 billions(*) of them... :p

Still, with the seed being the same, you get the same numbers every time...


JASS:
set SomeInteger = GetRandomInt(0, 1000000) // get a number while it still works
... do whatever
call SetRandomSeed(SomeInteger) // use it to still get random numbers after





(*) on a 32bit generator. With any luck, they're using a 64bit generator.
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
So basicly most of the time (unless you use a ridicolous amount of random Ints) you don't have to bother about it?
 

R 3 T R O

New Member
Reaction score
12
Is this MPI? Becaouse the Demo_Damage, Demo_Kills and Demo_Mana are just an integra they are not even an array and the trigger only has an event for player red.
 

Korolen

New User (Why do I keep getting those red bars?)
Reaction score
69
Is this MPI? Becaouse the Demo_Damage, Demo_Kills and Demo_Mana are just an integra they are not even an array and the trigger only has an event for player red.

While that particular demo is not MPI, rest assured that the Kode internals can easily generate numerous codes for each player, one right after the other if the need be.
 
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