Author Topic: Procedures  (Read 2259 times)

0 Members and 1 Guest are viewing this topic.

Offline RunMan

  • Major(1)
  • Posts: 27
Procedures
« on: April 04, 2008, 10:18:48 am »
What is wrong with this one?
<6:1> Identifier needed
Code: [Select]
procedure OnPlayerCommand(ID: byte; Text: string);

begin

procedure WriteLol(Name: string);
begin
WriteConsole(ID,'You are LOL '+Name,$00AABBCC);
end;

if Text = 'lol' then
begin
WriteLol(RunMan);
end;

end;

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: Procedures
« Reply #1 on: April 04, 2008, 10:20:58 am »
You can't have a procedure declared within another procedure. To do it properly, you have to place the whole WriteLol above OnPlayerCommand, like this:

Also make sure that RunMan is a declared string variable and that a value is assigned to it. Otherwise, write 'RunMan' instead of just RunMan in the WriteLol call.

Code: [Select]
procedure WriteLol(Name: string);
begin
  WriteConsole(ID,'You are LOL '+Name,$00AABBCC);
end;

procedure OnPlayerCommand(ID: byte; Text: string);
begin
  if Text = 'lol' then begin
    WriteLol(RunMan);
  end;
end;
« Last Edit: April 04, 2008, 10:23:51 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 RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #2 on: April 04, 2008, 11:05:12 am »
OK, but now when I write 'lol' nothing happens (I put RunMan into ')

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Procedures
« Reply #3 on: April 04, 2008, 11:56:56 am »
That code shouldn't even compile i believe. The variable ID in WriteLol isn't assigned. Change it to 0 or include ID as input for your procedure.
Come join: EliteCTF
Listen to: My Music

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #4 on: April 04, 2008, 12:22:11 pm »
Ohh, I did it. Sorry I forgot to put changed code
Code: [Select]
procedure WriteLol(Name: string);
begin
  WriteConsole(0,'You are LOL '+Name,$00AABBCC);
end;

procedure OnPlayerCommand(ID: byte; Text: string);
begin
  if Text = 'lol' then
  begin
    WriteLol('RunMan');
  end;
end;
« Last Edit: April 04, 2008, 12:25:47 pm by RunMan »

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Procedures
« Reply #5 on: April 04, 2008, 12:25:07 pm »
ahh. OnPlayerCommand only works when you do '/' instead of 't' ingame. So change 'lol' to '/lol' and type /lol ingame or change OnPlayerCommand to OnPlayerSpeak.
Come join: EliteCTF
Listen to: My Music

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #6 on: April 04, 2008, 01:24:04 pm »
Yeah, finally it works! Thank You very, very much :)

Oh, and be prepared for my another questions :P

@edit:

I made a script, everything is good, but now standard commands like /kill, /adminlog, /info don't work. I tried to put at the end of case:
Code: [Select]
else
end;
to end the script if player writes a command, which isn't in my list, but it didn't work.
Know how to make it work again? :)

Offline Boblekonvolutt

  • Soldier
  • **
  • Posts: 222
  • "YOU are a CAR."
Re: Procedures
« Reply #7 on: April 04, 2008, 02:17:37 pm »
You have to put begin before end...

Offline ghg

  • Camper
  • ***
  • Posts: 411
  • Village Idiot
Re: Procedures
« Reply #8 on: April 04, 2008, 04:59:23 pm »
"end else begin"
To be precise.
-=Gradius wuz you=-

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Procedures
« Reply #9 on: April 05, 2008, 07:13:12 am »
but now standard commands like /kill, /adminlog, /info don't work.
put
Code: [Select]
result := false;somewhere in there

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #10 on: April 06, 2008, 12:54:29 pm »
I don't understand. Can you explain it easier? :(
Code: [Select]
begin
case Text of
--commands--
end;
else
begin
result := false
end;
end;
This one doesn't work

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Procedures
« Reply #11 on: April 06, 2008, 02:23:18 pm »
'end; else begin' is a faulty combination, you should use 'end else begin' without ;
But just put result := false before the last end; and let it always happen so:
Code: [Select]
begin
  case Text of
    --commands--
  end;
  result := false;
end;
Come join: EliteCTF
Listen to: My Music

Offline Boblekonvolutt

  • Soldier
  • **
  • Posts: 222
  • "YOU are a CAR."
Re: Procedures
« Reply #12 on: April 06, 2008, 02:59:48 pm »
Why even bother putting result := false?

Offline amb2010

  • Camper
  • ***
  • Posts: 264
  • Fear the dot ...
Re: Procedures
« Reply #13 on: April 06, 2008, 06:43:22 pm »
The result := false is what tells it to do the enable the command. With out it the defualt commands(/kill /mercy etc..) wouldnt work.
And as the lyrics go in the United State's national anthem: "America, f**k YEAH!".

Offline Boblekonvolutt

  • Soldier
  • **
  • Posts: 222
  • "YOU are a CAR."
Re: Procedures
« Reply #14 on: April 06, 2008, 06:52:28 pm »
I know that, but it works fine without it...

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Procedures
« Reply #15 on: April 07, 2008, 05:18:34 am »
I know that, but it works fine without it...
Thats because variables are default false, 0, or whatever.

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Procedures
« Reply #16 on: April 07, 2008, 05:45:01 am »
Thats because variables are default false, 0, or whatever.
not always =F I think its highly recommended to reset your vars because of the nature of the language... Dont know the details, but it does act funny sometimes

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #17 on: April 07, 2008, 09:19:14 am »
now it tells me unknown identifier 'result' ;)

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Procedures
« Reply #18 on: April 07, 2008, 09:36:28 am »
now it tells me unknown identifier 'result' ;)
cause youre using OnPlayerSpeak, it doesnt return anything

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #19 on: April 07, 2008, 10:05:48 am »
So how can I solve my problem?
Quote
I made a script, everything is good, but now standard commands like /kill, /adminlog, /info don't work.

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Procedures
« Reply #20 on: April 07, 2008, 10:27:29 am »
Ok, let me try to explain. We work here with two different things: Procedures and Functions. The only difference between this two is that a Function will return a variable (a boolean, string, integer etc.). What the function returns is called the 'result'.
OnPlayerCommand is a function. This function will return a boolean (true or false). If it returns True then soldat will disable all commands accept the ones you discribe in OnPlayerCommand. Therefor you usually want to have result := false somewhere in the OnPlayerCommand function.
OnPlayerSpeak is a Procedure, it doesn't return anything. So if you put 'result' in OnPlayerSpeak the compiler will tell you that the identifier was not found.
Come join: EliteCTF
Listen to: My Music

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #21 on: April 07, 2008, 10:47:28 am »
But I use OnPlayerCommands not OnPlayerSpeak

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Procedures
« Reply #22 on: April 07, 2008, 12:07:23 pm »
then really recheck your code. It should be a Function like this:

Code: [Select]
function OnPlayerCommand(ID: Byte; Text: string): boolean;
begin
  --blabla, whatever here--
  Result := false;
end;

Result cannot be an unknown identifier then...
Come join: EliteCTF
Listen to: My Music

Offline RunMan

  • Major(1)
  • Posts: 27
Re: Procedures
« Reply #23 on: April 07, 2008, 01:02:16 pm »
Thanks, you are so cool :D

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Procedures
« Reply #24 on: April 07, 2008, 02:30:46 pm »
Thats because variables are default false, 0, or whatever.
not always =F I think its highly recommended to reset your vars because of the nature of the language... Dont know the details, but it does act funny sometimes
I found that 100% of the time in pascal that statement true, but not in other languages though.. Although it is good practice to set default values to variables.