Author Topic: script bug?  (Read 1305 times)

0 Members and 1 Guest are viewing this topic.

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
script bug?
« on: May 23, 2007, 05:50:07 pm »
This happened while running a server with 6 Poncho's for testing purposes:

Code: [Select]
(00:19:56)  [*] Run-time error (OnPlayerDamage): Invalid Opcode
(00:19:56)      Scripting is now disabled due to an error in your script.
(00:19:56)      A copy of this error can be found in /logs/scriptlog.txt
(00:21:49) Server: Scripting has been disabled on your server due to an error in your script.

Code: [Select]
function OnPlayerDamage(Victim, Shooter: byte; Damage: integer): integer;
var
poop: integer;
hp: integer;
begin
if ( Victim <> Shooter ) then
begin
poop := 149 - GetPlayerStat( Shooter, 'health' );
if ( ( GetPlayerStat( Shooter, 'Primary' ) = 7 ) or ( GetPlayerStat( Shooter, 'Primary' ) = 8 ) or ( GetPlayerStat( Shooter, 'Primary' ) = 13 ) or ( GetPlayerStat( Shooter, 'Primary' ) = 15 ) or ( GetPlayerStat( Shooter, 'Primary' ) = 17 ) ) then
begin
hp := 0;
end else
begin
hp := Round( 0.8 * Damage );
end;
if ( hp < poop ) then
begin
DoDamage( Shooter, - hp  );
end else begin
DoDamage( Shooter, - poop );
end;
end;

  result := Damage;
end;

Weirdness. Is this a bug? Is there a fluke in my script or is doing negative damage not allowed? What does the error invalid opcode refer to? Note that I have a thread running using threadfunc when this happens.

Thanks in advance,
~ Sai`ke
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: script bug?
« Reply #1 on: May 24, 2007, 09:42:19 am »
calling DoDamage in OnPlayerDamage is a bad idea in general, since it can cause infinite loops.
enesce also told me, that the current serverversion's ThreadFunc is still rather buggy, especially if you use sleep() in it.
« Last Edit: May 24, 2007, 09:55:29 am by Avarax »
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: script bug?
« Reply #2 on: May 24, 2007, 01:50:27 pm »
First of all, thanks for the reply. I still have a few questions though.

Why does dodamage in onplayerdamage cause infinite loops? Does dodamage call OnPlayerDamage again then? And if so, what shooter parameter is passed to it when it does? If this is known I could manually make sure it does not go into recursion by a simple if statement.

So threadfunc is buggy? Will this be fixed? What is the proper way to run timers then? And what can I do to avoid buggy behaviour as much as possible (since the idea I have just requires timing)?
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline Quantifier

  • Major
  • *
  • Posts: 70
Re: script bug?
« Reply #3 on: May 24, 2007, 02:29:12 pm »
To answer first part of your post:
Yes, dodamage() calls onplayerdamage() again, with shooter parameter equal to victim.

Your procedure already has:
Code: [Select]
if ( Victim <> Shooter ) then...at the beginning, it's enough to prevent endless recursion (and stack overflow).

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: script bug?
« Reply #4 on: May 24, 2007, 05:54:07 pm »
If you need to use timers it's better that you take a look at the AppOnIdle event, rather than using threads. It acts like a timer.
urraka

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: script bug?
« Reply #5 on: May 24, 2007, 06:28:46 pm »
Thanks, it's good to know that DoDamage calls onplayerdamage again. Also yes, AppOnIdle is exactly what I need. Just need to do a bit of rewriting, no biggie.

Thanks :D

Last question, it says AppOnIdle is run every second, can I rely on this or should I still check the ticks to see if we haven't skipped more than a second? It seems to do steps of 60 every time, but I haven't checked it with a server full of people.

Date Posted: May 24, 2007, 07:04:04 pm
Also, are there no functions with a higher temporal resolution? I had done a text effect, but it's not the same when everything is spaced by full seconds.
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: script bug?
« Reply #6 on: May 25, 2007, 12:09:16 am »
the only alternative is sleep() afaik
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: script bug?
« Reply #7 on: May 25, 2007, 08:07:05 am »
Yes, but that puts the rest of the script execution to sleep too unless I use threadfunc, which is supposed to be buggy. But I guess I just have to work a bit around the limitations... Make sometihng as close to what I want with what I can have ;D

Well, anyways, thanks for all the help people. I'll let you know if I run into more trouble.
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com