Jump to content

What is the highest number that can be stored in a SDF?

Recommended Posts

I reckon this is another easy question to which I once again failed to find the answer in the Docs, but I've been beating my head against the wall for probably two hours now.


I went with the assumption that maximum number that can be stored in a SDF would be around 32000, same as the max number that can be used in any script. Yet, I find myself wanting to the store the maximum HP of a creature (1021) in a SDF, and for whatever reason it keeps returning 253 instead. And when I fiddle around with it, it keeps giving different numbers from the actual HP of the creature. So confused. What's going on here? Any help would be appreciated.

Link to post
Share on other sites

Hello Thralni,


Kelandon beat me to this, but here's what I wrote anyway!


The maximum number you can store in an SDF is 255. There's no mention of this in the main documentation, but there's a line about it hidden away in the appendix. It's in the section that looks at functions relating to SDFs.


"void inc_flag(short a,short b,short how_much) - Changes SDF(a,b) by how_much (which can be negative). Note the legal range of values for a flag is 0..255."


This limit is a very common one for small numbers in code. Limits are generally set by how much space is needed to store the number. Since computers store information in binary, that is, in blocks that can take one of two values, these limits are generally powers of 2. 256 is 2^8, and is so common that it gets its own terminology: a byte. That 32000 limit you mention is another power of two: 32768 is 2^15 (although the number is usually stored as 2^16 allowing for a positive and negative range). Most limits you will see are 255, 32767 or 'really big'. 


This means that you won't be able to put a number as big as 1021 into an SDF. However, and I should stress that I'm not very familiar with scripting in Avernum, you might be able to get the same effect by splitting the number up. For instance, you could use four SDFs to store each digit of 1021. You could then recreate the number by doing something like this:

myhealth = sdf(a,1)*1000+sdf(a,2)*100+sdf(a,3)*10+sdf(a,4)


where sdf(a,n) is the nth digit of the creature's health (e.g. the 3rd digit of 1021 is 2). 

Link to post
Share on other sites

Hi kelandon and Ess-Eschas, thanks once again for your help. This definitely makes sense, as 253 would be 1021 - (3 * 256). Thanks Ess for putting this in context, it helps me understand why things are the way they are. Though it doesn't make me any less annoyed :p This will be a huge pain, as I'll have to recode some bits scattered all over the scenario...


Splitting the number up might work. I have one particular example where that would probably be a viable alternative. Sort of. I would take the number and divide by 100, then store in SDF, and won't allow numbers over 25500 to begin with. Cumbersome, but whatever, I'll spin some story... Though  for my specific purpose outlined here (character max health) I was wondering if I could do it as follows, as it might requires less SDFs:


health = 1021 // in the actual script I'd just use get_health(ME), but just to stick with he example started in my OP.


set_flag(x1,y1,health/256) // stores the number of full 256 blocks required to restore the number. I'm assuming BoA rounds down, based on prior experience.

set_flag(x2,y2,health%256) // stores the remainder


Recapitulating the number I could then do as follows:


health = 256 * get-flag(x1,y1) + get_flag(x2,y2)


That said, I haven't tested it yet. Just thinking out loud now... Really though, the best way to go about this is probably to just avoid using large numbers like these...

Edited by Thralni
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...