Author Topic: Help me with my simple script (not working)  (Read 1110 times)

0 Members and 1 Guest are viewing this topic.

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Help me with my simple script (not working)
« on: January 19, 2008, 07:45:14 pm »
I have been learning scripting on and off for a while now and understand most of it, i have looked at the specific functions and stuff at enesce.com/help, heck, i've even saved it all on my computer, I can also look at scripts and understand how they work, and can think about how I will write scripts in my head BUT, whenever I try to write them I get lots of errors.

This is a small script i tried to make so that I could make a larger one later:
Code: [Select]
function OnPlayerCommand(ID: Byte; Text: string): boolean;
if text = '/pos' then
WriteConsole(ID,'Your X ordinate is '  +inttostr(GetPlayerStat(ID,'X')),$FFFFFFFF);
WriteConsole(ID,'Your Y ordinate is '  +inttostr(GetPlayerStat(ID,'Y')),$FFFFFFFF);
end;

I have been looking at examples in other scripts plus the documantation and fixed about three errors, but now am stuck on one i can't find, even buy guessing where to put stuff.

This is such a small script, so it should be easy for one of the scripters to find me problem, anywho, the server shuts itself down when i try to run the script.
In the console log, this is the error:
  • Pos -> [Error] (12:3): 'BEGIN' expected


Help is gonna be appreciated.
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: Help me with my simple script (not working)
« Reply #1 on: January 19, 2008, 09:19:42 pm »
function OnPlayerCommand(ID: Byte; Text: string): boolean;
begin
    result:=false;
    if text = '/pos' then begin
        WriteConsole(ID,'Your X ordinate is '  + floattostr(GetPlayerStat(ID,'X')),$FFFFFFFF);
        WriteConsole(ID,'Your Y ordinate is '  + floattostr(GetPlayerStat(ID,'Y')),$FFFFFFFF);
    end;
end;
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Re: Help me with my simple script (not working)
« Reply #2 on: January 19, 2008, 09:39:36 pm »
thanks avarax , but could u tell me when do I put in semicolons? is there a rule?.

Thats funny because in your trenchwar domination I copied the "if text = '/mine' then" bit and there was no begin.

Also, is that result:=False important? because I got that in the log but it said [hint], and what does it do.

Sorry if i'm asking alot of questions.
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Help me with my simple script (not working)
« Reply #3 on: January 19, 2008, 11:25:16 pm »
Semicolons basically go at the end of every logical line.
ie:

Code: [Select]
IF true THEN
  doSomething
ELSE
  doSomethingElse;

While this is split over 4 lines, it's only one logical line.

BEGIN and END aren't needed if you are only using single statements; multiple statements require BEGIN and END as follows:

Code: [Select]
IF true THEN BEGIN
  doSomething;
  doSomethingElse;
END ELSE BEGIN
  doSomeOtherThing;
  doYetAnotherThing;
END;

Note that the statements in BEGIN and END require semicolons; BEGIN and END mark a block of code that is seperate from everything around it.

You should ensure that every possible call to your function produces an explicit return value; values aren't initialized to any default value, so it's possible if you never assign result to ie: false, it's possible it could have a value of true by default.

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Re: Help me with my simple script (not working)
« Reply #4 on: January 19, 2008, 11:53:45 pm »
what does the value of result refer to?
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Help me with my simple script (not working)
« Reply #5 on: January 19, 2008, 11:58:46 pm »
result is the return value of the function.

Offline saintpoida

  • Major(1)
  • Posts: 4
Re: Help me with my simple script (not working)
« Reply #6 on: January 20, 2008, 12:13:50 am »
my understanding of result in the onplayercommand event

is that your telling it whether to pass the command on to soldat

for example

if someone types '/pos' which is what you are looking for then set Result := true;
to tell soldat that its a command that you are going to do stuff for this command so it shouldnt try execute it as well

if however it was  '/kill' then after you have checked your own commands if it is not one of yours return false and soldat will know to try and run this command itself...

this is how it seems to work IMO

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Help me with my simple script (not working)
« Reply #7 on: January 20, 2008, 06:31:34 am »
my understanding of result in the onplayercommand event

is that your telling it whether to pass the command on to soldat

for example

if someone types '/pos' which is what you are looking for then set Result := true;
to tell soldat that its a command that you are going to do stuff for this command so it shouldnt try execute it as well

if however it was '/kill' then after you have checked your own commands if it is not one of yours return false and soldat will know to try and run this command itself...

this is how it seems to work IMO

Read the manual, it tells you how the result of events is used; it varies by event.

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Re: Help me with my simple script (not working)
« Reply #8 on: January 20, 2008, 04:39:57 pm »
f you're talking about enesce.com/help, then it doesn't even mention result ???
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Help me with my simple script (not working)
« Reply #9 on: January 20, 2008, 05:13:49 pm »
f you're talking about enesce.com/help, then it doesn't even mention result ???

Yes it does, in the example for each function that returns a value.

Offline saintpoida

  • Major(1)
  • Posts: 4
Re: Help me with my simple script (not working)
« Reply #10 on: January 20, 2008, 05:56:04 pm »
Sorry i got to agree with chutem here...

The manual has no indication what OnPlayerCommand returns it does however specify what OnCommand returns, but if you had only just started looking you would probably miss that as i only just discovered it and i just used trial and error to work out what the return did.

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Help me with my simple script (not working)
« Reply #11 on: January 20, 2008, 06:59:23 pm »
Guess that OnPlayerCommand had it left out, but it's the same as OnCommand:

Quote
begin
    if Text = '/test' then begin
       WriteLn('Test successful!');
    end;
    Result := false; // Return true if you want to ignore the command typed.
end;

ie: prevent further processing.