|
|
#1 |
|
Huge Clucker
![]() ![]() ![]() Join Date: Jan 2011
Location: Bucharest, Romania
Posts: 245
Reputation: 77
|
Things to do with OnPlayerText First of all, as you may know, OnPlayerText gets called every time a player types something in the chat. There are many uses for it which could make your server more interesting, so I am going to show you some of them. NOTE: This tutorial assumes that you have atleast basic knowledge of PAWN and of how to use default SA-MP functions. 1. IntroductionOnPlayerText itself is easy to understand. It has two parameters, 'playerid' and 'text[]'.
pawn Code:
2. Uses
First of all we change 'return 1;' to 'return 0;'. If we do so, the original message will not be sent. pawn Code:
Then, we define a new string of size 128 . Call it whatever you want, but in this tutorial I will call it 'msg'. pawn Code:
Now, we'll have to format the string. Call the format function, and turn the string into '[%d] %s' as so. %d means integer, and we will put the playerid in its place. %s means string, and we will put the text in its place. pawn Code:
It may look like we're done, but we aren't. The message is formatted, but it isn't sent! Therefore, if you try to chat right now, no message will come out - and I don't think that helps at all, so there's one last thing to do. Call SendPlayerMessageToAll, with senderid as playerid and with the text as 'msg', as so: pawn Code:
You could potentially adjust this so you can see anything you want - if you have a function in your gamemode that returns something like the team name or the rank of the player as a string (i.e as a text, not as a number), we could use it and show it in the message. pawn Code:
Say, you want to discuss things with your admins in private on the server, but without PMing each of them - that's why you need an admin chat! You could also make it with a command, but why not simplify it: If the admin makes a message that starts with #, it will automatically be shown to other admins! We will once again start from the 'default' OnPlayerText, but you can continue on the 'old' one as well. pawn Code:
First, add a check to see if the player is an admin. We will use IsPlayerAdmin, which is used to check if a player is logged in RCON, but you can change it with any other function/variable which shows the player's admin level. pawn Code:
To the if statement we have just created, we will add "&& text[0] == '#'" to check if the first character in the text is #. You can change it to whatever other character you'd choose for admin chat. pawn Code:
Then, we will create a new string, with the size of 128 (like in the previous parts, name it whatever you'd like), and format it as so: pawn Code:
But wait! We don't have 'pName'. Create it as a string with the size of MAX_PLAYER_NAME, and use GetPlayerName to get the player's name. In this tutorial we assume that you know how to use the basic functions, so we won't explain the usage of GetPlayerName in detail - however, there's not much to explain at all. pawn Code:
Perfect! Now we need to show it to all admins. Add this to the end of your gamemode: pawn Code:
Now, add it as so into OnPlayerText: pawn Code:
You could also add the ID as in the previous example, like so: pawn Code:
This can be used for various things, and I will give an example. Once again, we start with the 'base' OnPlayerText. pawn Code:
We will first add an if statement which checks for two 'words' in the text: 'how' and 'get car'. pawn Code:
Then, we return 0, so the message doesn't get sent, and we send the player this message: "To get a car, please use the /v command.". pawn Code:
You can use this with various other strings in order to make your fun/stunt server more awesome, for example if the player says 'help me up', you can set his position 10 Z higher. pawn Code:
Once again, we start with the base OnPlayerText function. pawn Code:
First of all, we will have to create some variables, but before that, please do the following if you haven't already: At the top of your gamemode, put this: pawn Code:
Now, we can create the variables! Make IsAnswering and pAge, with the size of MAX_PLAYERS, and define QUESTION_AGE as 1 and QUESTION_NONE as 0, like so: pawn Code:
Now, where shall we ask the question? Let's do it in OnPlayerSpawn. pawn Code:
Now, let's add an if statement which checks whether the player is answering QUESTION_AGE. pawn Code:
Now, let's think. What should we do? We should return 0 so the players don't see it, but how do we get the value as an integer, given that the text is a string? Here's where we get saved by a very magic function - strval! We will declare a new variable and store the value of the string in it. pawn Code:
Of course, we might want to keep players from lying about their age or to keep 'kids' off our servers... pawn Code:
... This tutorial will be updated soon with more examples! 3. Challenges for beginners
All teh tutorial are belong to me. If you stealz, teh Ceiling Cat is watching you. Remember. P.S: If you actually read the whole thing and you have found mistakes in the tutorial, please tell me. Thanks!
Last edited by TheBetaFox; 17/04/2012 at 04:25 PM. |
|
|
|
|
|
#2 |
|
Big Clucker
![]() ![]() Join Date: Sep 2010
Posts: 199
Reputation: 85
|
Good tutorial
|
|
|
|
|
|
#3 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2011
Location: Sublime Text 2
Posts: 2,008
Reputation: 326
|
This is one of the very few tutorials that I liked. I like how you challenge the reader at the end. You should make more.
|
|
|
|
|
|
#4 |
|
Huge Clucker
![]() ![]() ![]() Join Date: Jan 2011
Location: Bucharest, Romania
Posts: 245
Reputation: 77
|
Updated! I've added a section on 'Question answering', plus one new Easy challenge! Enjoy!
Thanks a lot, guys! I appreciate that you like the tutorial. I'm still in the Easter vacation, so I have some time to make more tutorials. |
|
|
|
|
|
#5 | ||
|
Beta Tester
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: 629
Posts: 13,325
Reputation: 1910
|
Quote:
Quote:
Last edited by Y_Less; 17/04/2012 at 04:02 PM. |
||
|
|
|
|
|
#6 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Mar 2009
Location: Cloud 9
Posts: 2,295
Reputation: 223
|
Nice tutorial. You can also format the text argument so you don't have to create an additional array. Not a big improvement but worth mentioning. This is assuming that it doesn't matter if "text[]" is altered.
Removed code that can cause possible bugs. Last edited by iggy1; 17/04/2012 at 03:07 PM. |
|
|
|
|
|
#7 | |
|
Beta Tester
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: 629
Posts: 13,325
Reputation: 1910
|
Quote:
|
|
|
|
|
|
|
#8 |
|
Gangsta
![]() ![]() ![]() ![]() Join Date: Nov 2008
Posts: 693
Reputation: 5
|
pawn Code:
|
|
|
|
|
|
#9 |
|
Big Clucker
![]() ![]() Join Date: Jan 2012
Posts: 60
Reputation: 1
|
Good Tutorial
|
|
|
|
|
|
#10 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2010
Location: INDIA !!! :)) Punjab
Posts: 1,078
Reputation: 107
|
Nice tutorial
useful for all
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OnPlayerText hmm | Twinki1993 | Scripting Help | 10 | 09/01/2012 01:17 AM |
| [Help] OnPlayerText | JFT | Scripting Help | 1 | 19/09/2011 03:12 AM |
| [NGG] [NGRP] Well, So I'll just make a thread to highlight... certain things... to more, set things straight. | Sew_Sumi | Everything and Nothing | 21 | 03/07/2011 05:29 PM |
| Weird things on OnPlayerText | leong124 | Help Archive | 3 | 03/04/2011 03:50 AM |
| OnPlayerText | Hash [NL-RP] | Help Archive | 5 | 27/07/2010 05:16 PM |