how to shorten Save/load code

Level 10
Joined
Jan 26, 2019
Messages
90
I write the integer values like this:
1 (first) character indicates the length of this value, the remaining characters are the value itself.
Example: "123" = 4 Symbols
ASDF
A = length
S = 1
D = 2
F = 3

However, if the value doesn't exist/equals 0, it still takes up 1 character (the length character that means 0).
It turns out that if I store 100 empty values, that's 100 extra characters.
In other maps, for example in twrpg, if you have a lot of items, then the loading code will be long, but if there are no items, then the code is short, which means that empty values are not written into the code.

However, if I try not to write empty values into the code, then the following can happen when loading:
Saved gold = 0
Saved lumber = 123
Saved item in #1 slot = 5

At the time of loading, the system does not know that gold = 0 because the code is shorter and lacks the necessary characters, so it immediately reads the next value and puts it in first place. (she thinks that the first meaning is wood and not gold, because there is no information about gold)

Result:
Loaded gold = 123
Loaded lumber = 5
Loaded item in #1 slot = 0

This happens because the system reads the parameters in order.
I probably need to read the code in such a way that it knows where 0 is, but how will I know if I don't write 0 in the code?
I think there is some method, but at the moment I haven't been able to come up with one.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
There's a lot of different save/load systems, so you should specify which one you're using.

Anyway, I'll assume it's TriggerHappy's codeless system, in which case you cannot save "nothing". You save 0 to represent the [0] index in an Array of Item-Types, which points to "No item". Then when you load 0 the game will say "I tried to create the loaded Item but it didn't have a valid Item Id".

But one thing you can do is Save how many Items you have equipped. For example, if your Hero has 6 inventory slots and only 3 are being used then you can save the number 3. Then you can opt out of Saving the empty slots since when it comes time to Load you will know exactly how many you need to Load.
  • Set Variable Item_Count = LoadNext()
  • For each integer X from 1 to Item_Count
    • Loop - Actions
      • Set Varianble Loaded_Item = LoadNext()
      • Item - Create 1 Item_Array[Loaded_Item]

If you use Lua you'll gain access to more elaborate s/l systems which don't have these limitations.
 
Last edited:
Level 10
Joined
Jan 26, 2019
Messages
90
There's a lot of different save/load systems, so you should specify which you're using.
This is my own system, so I have no difficulty editing or understanding anything.
It uses load code and is much better than the similar version from TriggerHappy (if we talk about load code) which I myself had previously used.
Everything works well in it, system also can divide long code into several parts.
That's actually why I decided to try to improve it.

I tested saving the parameters: Hero/Level/Gold/Lumber/x6 hero inventory slots/x600 additional inventory slots from bags.
Result:
Save with full items = 11 codes
Save without items = 4-5 codes.
Ideally, if empty slots are not written into the code, we will be able to get 1 very short code.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
This is my own system, so I have no difficulty editing or understanding anything.
It uses load code and is much better than the similar version from TriggerHappy (if we talk about load code) which I myself had previously used.
Everything works well in it, system also can divide long code into several parts.
That's actually why I decided to try to improve it.

I tested saving the parameters: Hero/Level/Gold/Lumber/x6 hero inventory slots/x600 additional inventory slots from bags.
Result:
Save with full items = 11 codes
Save without items = 4-5 codes.
Ideally, if empty slots are not written into the code, we will be able to get 1 very short code.
Okay, I edited my previous post before you responded and I think it has a solution you'd be interested in -> Save how many of each thing you need to Load.
 
Level 10
Joined
Jan 26, 2019
Messages
90
Okay, I edited my previous post before you responded and I think it has a solution you'd be interested in -> Save how many of each thing you need to Load.
If you mean add total number of saved values in the code, I have been doing this, but it does not help in this case.
I know how many values were saved and I can also record the number of empty values, but the system will still load in order and it cannot determine exactly where each empty value will appear.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
If you mean add total number of saved values in the code, I have been doing this, but it does not help in this case.
I know how many values were saved and I can also record the number of empty values, but the system will still load in order and it cannot determine exactly where each empty value will appear.
The only downside is that the items will lose their original positions in the storage box. It'll effectively sort them, which you could add your own logic to in order to help organize things better.

You have 100/600 items in your storage.

You save the value 100 and the id's of those 100 items.

When it comes time to load and refill the storage with those items you use a For Loop going from 1 to 100.

You've now avoided having to save and load 500 item id's (which would've been saved as 0).

Save items example:
  • Set Variable Total_Items = (Number of items in storage)
  • Custom script: Save(Total_Items)
  • For each integer X from 1 to 600 do (Actions)
    • Loop - Actions
      • Set Variable Item_Id = GetItemFromStorage(X)
      • If (Item_Id Not equal to 0) Then Custom script: Save(Item_Id) Else do nothing
Load items example:
  • Set Variable Total_Items = LoadNext()
  • For each integer X from 1 to Total_Items do (Actions)
    • Loop - Actions
      • Set Variable Item_Id = LoadNext()
      • Item - Create 1 Item_Array[Item_Id]
      • Custom script: AddItemToStorage(GetLastCreatedItem())
Note that you'd reverse the save order if using TriggerHappy's system or one like it.
 
Last edited:
Top