LumpRead: Difference between revisions

Add example
(Update formatting and specifications on failure)
(Add example)
Tag: Source edit
Line 6: Line 6:
By default, the ''type'' returned is an unsigned 8-bit integer.  
By default, the ''type'' returned is an unsigned 8-bit integer.  
===Parameters===
===Parameters===
*''lump'': The lump index as returned from <tt>[[LumpOpen]]</tt>.
*''lump'': The lump index as returned from [[LumpOpen]].
*''pos'': The byte position in the lump to read from.
*''pos'': The byte position in the lump to read from.
*''type'': The type of integer that should be returned.
*''type'': The type of integer that should be returned.
Line 13: Line 13:
**<code>LUMP_READ_SHORT</code>: Signed 16-bit integer.
**<code>LUMP_READ_SHORT</code>: Signed 16-bit integer.
**<code>LUMP_READ_USHORT</code>: Unsigned 16-bit integer.
**<code>LUMP_READ_USHORT</code>: Unsigned 16-bit integer.
** <code>LUMP_READ_INT</code>: Signed 32-bit integer.
**<code>LUMP_READ_INT</code>: Signed 32-bit integer.
**<code>LUMP_READ_FLOAT</code>: Signed 32-bit float converted to a fixed-point.
**<code>LUMP_READ_FLOAT</code>: Signed 32-bit float converted to a fixed-point.
===Return value ===
===Return value===
Returns the integer that was read.
Returns the integer that was read.


Returns 0 if the ''lump'' passed is invalid, or when the ''type'' passed is invalid, alongside a console message.
Returns 0 if the ''lump'' passed is invalid, or when the ''type'' passed is invalid, alongside a console message.


Also returns 0 when called on a lump that was not opened with <tt>[[LumpOpen]]</tt>, alongside a console message.
Also returns 0 when called on a lump that was not opened with [[LumpOpen]], alongside a console message.
 
==Examples==
==Examples==
{{noexamples}}
<syntaxhighlight lang="js" line="1">
// This script opens all lumps with the name "MYLUMP" and reads the integer that exists on position zero.
// "MYLUMP" is not a real lump, and you should replace it with a lump that does exist.
// `CLIENTSIDE` is optional.
Script 1 OPEN CLIENTSIDE
{
int startIndex = -1;
while(true)
{
startIndex = LumpOpen("MYLUMP", startIndex + 1);
 
// The next lump was not found.
if (startIndex == -1) {
break;
}
// Read the integer that exists in the lump at position zero.
int value = LumpRead(startIndex, LUMP_READ_INT, 0);
Log(s:"The integer that was read is ", d:value, s:".");
 
// Make sure to close the lump again to free the handle.
LumpClose(startIndex);
}
}
</syntaxhighlight>
 
==See also==
*[[LumpRead]]
*[[LumpReadArray]]
*[[LumpReadString]]
*[[LumpGetInfo]]
*[[LumpRead]]
*[[LumpClose]]