script debugging help

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

script debugging help

Post by Willy Elektrix »

The script below causes every hero up to 20 damage (randomized) and then checks to see if any of them are still alive. If none are still alive, the script causes are a game over.

This works perfectly if I have 2 or more heroes in the party. However, if I only have 1 hero, that single hero can reach 0 HP without causing a game over.

Code: Select all

plotscript, crest1, begin
map cure (31, 0)
map cure (31, 1)
map cure (31, 2)
map cure (31, 3)
variable (i, hpleft, someone alive)
someone alive := false
for (i, 0, 3) do (
  hpleft:= get hero stat(i, stat:Hull, current stat)
  if (hpleft>> 0) then (someone alive := true)
)
if (someone alive == false) then (
  show text box (109)
  wait for text box
  show backdrop (0)
  show textbox (235)
  wait for text box
  game over
) 
show text box (110)
end
Here's where things get weird. The scripts listed below run as part of the ship selection screen. If I select the bomber ship (running the blank addbomber script), the bug described above does not seem to manifest. However, if i select the carrier or the fighter (thereby running addcarrier or addfighter), then the bug does appear.

Code: Select all

plotscript, addbomber, begin
end

plotscript, addcarrier, begin
add hero (hero:carrier)
swap by position (0,1)
delete hero (hero:bomber)
end

plotscript, addfighter, begin
add hero (hero:fighter)
swap by position (0,1)
delete hero (hero:carrier)
end
The bug will appear if I am using the bomber, If I rewrite bomberscript as:

add hero (hero:bomber)
swap by position (0,1)
delete hero (hero:bomber)

Therefore, it seems to have something to delete here command. Does anyone have any idea?
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Perhaps instead of "hpleft >> 0" you could use "hpleft >= 0" ? I'm not sure if it would make a difference though.


Though I have no idea why the second script would interfere with it. The first script looks pretty straightforward to me, I'm not sure why it would fail. My guess is that there's a problem with checking the amount of HP, but I could be pretty far off the mark.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Gizmog
Metal King Slime
Posts: 2614
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

Do you have some kind of a placeholder hero until the character has chosen a bomber, carrier or fighter, something that could still be in the party somehow?

EDIT: Oh! You might also try adding a wait after the 4 "map cure" commands. Maybe it isn't giving it a chance to update the stat before it checks it? Doesn't explain why it screws up if you use the add bomber thing though.
Last edited by Gizmog on Sun Mar 24, 2013 5:24 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

It appears that you've discovered a bug, though not the one that you think you have. "map cure" is meant to check if it the whole party is dead and if so run the death script. (much poking around...) Oh dear, this is a really serious bug. For the last 18 months, including Alectormancy, checking whether you've killed your party outside of battle (with harm tiles, items, spells, or "map cure") has not worked. Fixed now, though I notice another problem in the game over code...

To answer your question about why your script is misbehaving, it's because of an obscure feature of getherostat which exists for backwards compatibility: nonexistent heroes have stats too. After you delete a hero, using getherostat on the slot that it was in continues to return the removed hero's stats. So your for loop sees a phantom hero with non-zero HP.

It's always a good idea to add checks such as whether there is actually a hero in a party slot rather than assuming it's ok to pass invalid arguments to a command. Your loop should look like:

Code: Select all

for (i, 0, 3) do ( 
  if (hero by slot(i) > -1) then (
    hpleft:= get hero stat(i, stat:Hull, current stat) 
    if (hpleft > 0) then (someone alive := true) 
  )
) 
Anyway, to answer the actual question, assuming that those textboxes don't show the same thing as your default gameover script, download the next nightly (not right now) and do:

Code: Select all

plotscript, crest1, begin 
  set death script (@crest death script)
  map cure (31, 0) 
  map cure (31, 1) 
  map cure (31, 2)
  map cure (31, 3)
  wait(1)  # Necessary to allow the death script to be triggered
  # continues if not dead...
  set death script (@whatever the default death script is)
  show text box (110) 
end
  
script, crest death script, begin
  show text box (109) 
  wait for text box 
  show backdrop (0) 
  show textbox (235) 
  wait for text box 
  game over 
end
There's a number of other options, like putting that in the existing gameover script instead.
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

Post by Willy Elektrix »

TMC wrote:

Code: Select all

for (i, 0, 3) do ( 
  if (hero by slot(i) > -1) then (
    hpleft:= get hero stat(i, stat:Hull, current stat) 
    if (hpleft > 0) then (someone alive := true) 
  )
) 
Implementing this script eliminated the bug, for my purposes at least.

I'm going to release v1.1 of Dreg Sector this week. I can't thank you enough for the help you have provided squashing the game's bugs. This new version should be vastly improved and it's mostly thanks to your work.
Last edited by Willy Elektrix on Sun Mar 24, 2013 11:38 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

No, don't do that, it'll break when you switch to Beelzebufo.
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

Post by Willy Elektrix »

TMC wrote:No, don't do that, it'll break when you switch to Beelzebufo.
Cool. Instead, I'll update to the newest nightly when it's posted. Although, I doubt I would bring this project into Beelzebufo anyway.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Nightlies are built automatically, twice a day, I think. The fix for that bug is available already.

You are using Beelzebufo already, it just isn't official! Unless you mean you'll stick with Zenzizenzic and adapt your script. Writing scripts in a way that's known to break in later engine versions is definitely a bad idea.

You could actually have your script work on all versions by changing the ondeath script to a blank script at the start and restoring it at the end.
Last edited by TMC on Mon Mar 25, 2013 2:51 am, edited 1 time in total.
Post Reply