Difference between revisions of "AGIWiki/Memory and Script"

Jump to navigation Jump to search
m
Text replacement - "</source>" to "</syntaxhighlight>"
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(5 intermediate revisions by 2 users not shown)
Line 139: Line 139:
== Script Buffer ==
== Script Buffer ==


The script buffer stores an ordered list of commands such as loading/discarding resources, drawing/overlaying pictures and adding views to the background ([[AGIWiki/Add.to.pic|add.to.pic()]]) which are later saved in a save game. Because the memory layout is so dependent on order (as explained above), the script is necessary to ensure this order is conserved between save games. These commands are later executed from a restored game to ensure the interpreter is in the exact same state as it was when the game was saved. The script buffer is cleared when [[AGIWiki/New.room|new.room()]] is called so the script is room-centric.
The script buffer stores an ordered list of commands such as loading/discarding resources, drawing/overlaying pictures and adding views to the background ([[AGIWiki/add.to.pic|add.to.pic()]]) which are later saved in a save game. Because the memory layout is so dependent on order (as explained above), the script is necessary to ensure this order is conserved between save games. These commands are later executed from a restored game to ensure the interpreter is in the exact same state as it was when the game was saved. The script buffer is cleared when [[AGIWiki/new.room|new.room()]] is called so the script is room-centric.


The default size of the script buffer is 50 entries. Each entry is 2 bytes long so the default buffer size is 100 bytes. You can increase the size of the buffer by using the command [[AGIWiki/script.size|script.size()]]. Certain commands can increase script usage unless script writing has been disabled by setting flag 7 (script_blocked in template [[AGIWiki/defines.txt|<nowiki>#defines.txt</nowiki>]]). Care must be taken so that the game does not overflow the script buffer as this has been a cause of many problems. The interpreter keeps a record of the largest script size used in the game, which can be accessed from the command show.mem().
The default size of the script buffer is 50 entries. Each entry is 2 bytes long so the default buffer size is 100 bytes. You can increase the size of the buffer by using the command [[AGIWiki/script.size|script.size()]]. Certain commands can increase script usage unless script writing has been disabled by setting flag 7 (script_blocked in template [[AGIWiki/defines.txt|<nowiki>#defines.txt</nowiki>]]). Care must be taken so that the game does not overflow the script buffer as this has been a cause of many problems. The interpreter keeps a record of the largest script size used in the game, which can be accessed from the command show.mem().
Line 164: Line 164:
|}
|}


==== Script Item Types ====
There are nine resource activities that are tracked in the AGI script:
{| border="1" cellpadding="5" cellspacing="5" style="border-collapse: collapse; border-style: hidden;"
|
<center>'''Script Activity #'''</center>
|
<center>'''AGI Command'''</center>
|-
|
<center>0</center>
| height="5" |
<center>load.logic</center>
|-
|
<center>1</center>
|
<center>load.view</center>
|-
|
<center>2</center>
|
<center>load.pic</center>
|-
|
<center>3</center>
|
<center>load.sound</center>
|-
|
<center>4</center>
|
<center>draw.pic</center>
|-
|
<center>5</center>
|
<center>add.to.pic*</center>
|-
|
<center>6</center>
|
<center>discard.pic</center>
|-
|
<center>7</center>
|
<center>discard.view</center>
|-
|
<center>8</center>
|
<center>overlay.pic</center>
|}
<nowiki>*</nowiki>''add.to.pic'' actually uses 4 script table entries (total of eight bytes) as follows:
: byte 0: script activity number
: byte 1: not used
: byte 2: view number
: byte 3: loop number
: byte 4: cel number
: byte 5: x position
: byte 6: y position
: byte 7: priority
=== Loading/Discarding Any Number of Times ===
=== Loading/Discarding Any Number of Times ===


Line 191: Line 256:
However, script blocking is useful for small bits of code that don't allow the player to save or restore in between:
However, script blocking is useful for small bits of code that don't allow the player to save or restore in between:


<syntax type="C++">
<syntaxhighlight lang="cpp">
#define script_blocked f7
#define script_blocked f7
   
   
Line 204: Line 269:
  reset(script_blocked);
  reset(script_blocked);
  }
  }
</syntax>
</syntaxhighlight>


==== Save Script Position ====
==== Save Script Position ====
Line 211: Line 276:


The script works like this (you wouldn't use this in a game):
The script works like this (you wouldn't use this in a game):
<syntax type="C++">
<syntaxhighlight lang="cpp">
  load.view(10); // script contains view 10
  load.view(10); // script contains view 10
  push.script();
  push.script();
  load.view(20); // script contains view 10, 20
  load.view(20); // script contains view 10, 20
  pop.script(); // script contains view 10
  pop.script(); // script contains view 10
</syntax>
</syntaxhighlight>
To use in a real game, you need to initialise push.script first. This is similar to the first example in blocking script writing (but overkill):
To use in a real game, you need to initialise push.script first. This is similar to the first example in blocking script writing (but overkill):
<syntax type="C++">
<syntaxhighlight lang="cpp">
  if (new_room)
  if (new_room)
  {
  {
Line 234: Line 299:
  pop.script(); // script doesn't contain either
  pop.script(); // script doesn't contain either
  }
  }
</syntax>
</syntaxhighlight>
Here is my version of code to swap the ego's view and still letting the player save the game:
Here is my version of code to swap the ego's view and still letting the player save the game:


In the room logic (will be needed in all rooms):
In the room logic (will be needed in all rooms):
<syntax type="C++">
<syntaxhighlight lang="cpp">
  if (new_room)
  if (new_room)
  {
  {
Line 251: Line 316:
   
   
  ...
  ...
</syntax>
</syntaxhighlight>
Logic 70 - View switching logic. It can be given any number, just make sure it's the same in the calling logic.
Logic 70 - View switching logic. It can be given any number, just make sure it's the same in the calling logic.


<syntax type="C++">
<syntaxhighlight lang="cpp">
#include "defines.txt"
#include "defines.txt"
   
   
Line 302: Line 367:
   
   
  return();
  return();
</syntax>
</syntaxhighlight>


This is a better solution, because the script will have the correct views loaded when a game is restored and the script size will no increase each time the game is saved.
This is a better solution, because the script will have the correct views loaded when a game is restored and the script size will no increase each time the game is saved.
Line 327: Line 392:


Previously known as unknown.172().
Previously known as unknown.172().
These two commands are only available in AGI versions 2.915 and above.
Despite their names, the [[AGIWiki/push.script|push.script]] and [[AGIWiki/pop.script|pop.script]] functions are not stack functions. Pushing the script will save the current script table position. If push.script is called multiple times, it will overwrite the stored script position each time it is called.
Popping the script will restore the script table position. After calling pop.script , the next script entry will be made at the restored position. It is important that push.script be called BEFORE pop.script. Popping the script position without first pushing it could result in unpredictable results since the memory location where the script position is stored may contain an unknown value.
Because AGI relies on the script entries to correctly [[AGIWiki/Saving games|save and restore games]], it is important to exercise caution when manipulating scripts.


=== Script Flags ===
=== Script Flags ===
TrustedUser
2,147

edits

Navigation menu