User Tag List

Results 1 to 9 of 9

Thread: ZScript Technical FAQ (READ BEFORE POSTING BUGS)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator DarkDragon's Avatar
    Join Date
    Oct 2001
    Posts
    6,228
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    11,031
    Level
    31
    vBActivity - Bars
    Lv. Percent
    8.81%

    Waitframe problems

    Q: Ack! I'm trying to do something to all items each frame, but ZC acts weird or crashes!

    A: Great care must be taken whenever you invoke Waitframe() around pointers. As mentioned here, pointers become stale after each frame, and moreoever there's occasionally even greater subtlety. Consider for instance the following snippet:
    Code:
    int numitems = Screen->NumItems();
    while(true)
    {
       for(int i=0; i<numitems; i++)
       {
           item it = Screen->LoadItem(i);
           it->X++;
       }
       Waitframe();
    }
    Can you spot the problem?
    Even though I'm refreshing the item pointers each frame, I'm NOT refreshing the number of items on the screen, so if the number of items decreases (say Link picks one up), one of the LoadItem() calls will return a dangling pointer. The fix is simple; just be sure to recompute both the pointers and the number of items each frame:
    Code:
    while(true)
    {
       for(int i=0; i<Screen->NumItems(); i++)
       {
           item it = Screen->LoadItem(i);
           it->X++;
       }
       Waitframe();
    }

  2. #2
    Administrator DarkDragon's Avatar
    Join Date
    Oct 2001
    Posts
    6,228
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    11,031
    Level
    31
    vBActivity - Bars
    Lv. Percent
    8.81%

    Interfacing ZScript and ASM

    Q: Can I mix ASM and ZScript scripts?

    A: Yes, if you're very, very careful.

    In general adding ASM scripts in slots unused by ZScript is safe. Just realize that if you write to global or screen registers, you may be writing over values used by some of the ZScript scripts.

    After every compilation the full ASM output of the compiler is written to allegro.log, so by inspecting this output you can determine what global variables are being used by ZScript, so that you can avoid (or not avoid!) using them in your ASM scripts.

    On the other hand, directly modifying the ASM output of a ZScript compilation requires very, very great care, for several reasons:

    1. Almost all of the D registers (currently D0-D6) are reserved as important scratch spaces for things like computing the stack frame offset of local variables, and should not be overwritten by foreign code.

    2. ZScript-compiled code is heavily reliant on the stack for storing everything from local variables to parameters of function calls to this pointers. The state of the stack must thus also be preserved by foreign code.

    3. Changing the line numbers of ZScript code requires a lot of work verifying that references to those line numbers (in things like GOTO) are modified accordingly. Even more tricky is that line numbers are sometimes indirectly stored in registers, pushed onto the stack, then popped off much later and used as return values via GOTOR.

    4. If you ever decide to modify the ZScript code and recompile, you'll of course need to reapply your "patch."

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Armageddon Games is a game development group founded in 1997. We are extremely passionate about our work and our inspirations are mostly drawn from games of the 8-bit and 16-bit era.
Social