Jump to content

Learning How to Script


HouseMD

Recommended Posts

Second Question : Why are %a and $4 different! because %a displayes the same thing as $4, doesn't it?

%a is a variable, $4 is a parameter.

you have to assign value(s) to a variable in most cases, and most common variables are only present within specific functions/aliases, eg:

if you have a script to count from 'x' to 0, you could use a variable (name it whatever you want), and do it like this:

on *:SIGNAL:mta.command:{

if ($3 == !count) {

if ($4) && ($4 isnum) && ($4 > 1) { <-- $4 is the 2nd word a person would say in a sentence. If i typed '!count 200' then $4 would be '200'

var %x = $4 <-- Assign variable x - it has been made equal to $4 as this is the value we are counting down from.

while %x > 0 {

mta.say $1 Countdown: %x

!dec %x

}

}

else mta.msg $1 $2 Error - Syntax: !count

}

}

This should show you that variables and parameters are different :P

%a could return any value you set it to return, $4 is usually an assigned constant. That's how i look at it anyway, i didn't learn from any tuts etc so i make up my own ways of understanding (lol)

Link to comment

I wrote this a year or so ago back in '0.5', ive added a few notes to the bottom for the the updates in 'mtasa:race'

I know noone asked for it, but if anyones interested in learning some basic scripting, this should help.

scripting help by [uVA]Scooby.

Written for mtavc 0.5 (see notes at bottom)

over the last few yrs people have been asking for help with scripting,

since there is no proper tutorial for mta scripting i know in the beginning it can be kinda hard.

some have picked up the basics and need help with harder stuff, some dont know a thing.

heres a little something i thought id write for anyone wanting to understand it more.

its very basic stuff, but should help you on your way to making you first script.

scripts are written into a text file in .mrc format - some people use mirc script editor,

some use notepad and some use other programs - to be honest, it doesnt really matter

as long as u can type and save as .mrc

ok i will show u an extremely basic script, then explain a few things about it.

on *:SIGNAL:mta.command:{

 if ($3 == !moo) mta.say $1 $mta.name($1,$2) says moooooooo!
 if ($3 == !hp) mta.msg $1 $2 Your Health is at $mta.health($1,$2) $+ %

}

so there we have it... thrilling eh.

ok the first line 'on *:SIGNAL:mta.command:{' this is where all the stuff u type with a ! is looked at.

ok so, by typing !moo it displays in the chat '[uVA]Scooby says moooooooo!'

$mta.name($1,$2) is what we use to return the name of the person who typed the info.

and by typing !hp we get a pm telling us our health. mta.msg is used for this.

$mta.health($1,$2) is what we use to return the health of the person typing the info

after comes $+ this is used to remove the space between text if it wasnt used it would show as 100 %

rather that 100%. finally, its finished with a }

you will find that these are very important. when a { is used it must always be finished with a }

also note, when sending a pm its a little different to mta.say

the $1 $2 and $3 may look a little confusing to you. i'll explain what they do as easily as poss.

$1 = Server

$2 = PlayerID

$3 = the first word u type

$4 = the second word u type

$5 = the 3rd word u type... and so on

u can also use a minus sign after, this is the same as saying 'the rest of the info'

$3- = the first word and all the words that come after the first word.

so if u wanted to find out if the word 'moo' was bein typed, you would use: if (moo isin $3-)

ok that should have been easy enough for you to follow - if not - give up.

there is a list of all the identifiers in the scripting help files that came with mtama.

please remember when writing a script, there is always more than one way to do it,

 if ($3 == !moo) mta.say $1 $mta.name($1,$2) says moooooooo!

 if $3 == !moo { mta.say $1 $mta.name($1,$2) says moooooooo! }

 if ($3 == !moo) { mta.say $1 $mta.name($1,$2) says moooooooo! }

 if ($3 == !moo) { 
   mta.say $1 $mta.name($1,$2) says moooooooo! 
 }

all these lines will work..

however, the top one is the most common.

ok so we're still doing really really basic stuff - lets make it more interesting by adding a variable

 var %a = $iif($4 == $null,$2,$mta.getid($1,$4))

looks complicated huh... ok i'll explain the line.

$iif is commonly used however at this stage i'll just briefly explain what it does.

$iif(1 == 2,yes,no) would return no

or

$iif(1 == 1,yes,no) would return yes

so lets take our line: $iif($4 == $null,$2,$mta.getid($1,$4))

it says if $4 == $null, $4 is our second word, $null means 'empty',

$2 is the ID who typed it, $mta.getid is an alias that finds a name from a number or part name

so basically it says: if the second word is empty, use your ID, else get the ID from the name given.

On *:SIGNAL:mta.command: is an alias - it contains whatever u choose. These are commands that begin with !

very soon you will be making your own alises, however,

mtama has several aliases that come with it, which we will be using for now.

so if we use the 'var %a =' line within our previous commands we can make it do a lot more.. here it is again.

on *:SIGNAL:mta.command:{

 var %a = $iif($4 == $null,$2,$mta.getid($1,$4))

 if ($3 == !moo) {
   if ($4 == $null) mta.say $1 $mta.name($1,$2) says moooooooo!
   else mta.say $1 $mta.name($1,$2) says mooooooo to $mta.name($1,%a)
 }

 if ($3 == !hp) {
   if ($4 == $null) mta.msg $1 $2 Your Health is at $mta.health($1,$2) $+ %
   else mta.msg $1 $2 $mta.name($1,%a) $+ s health is at $mta.health($1,%a) $+ %
 }

}

ok now u can see we've introduced an 'else'

so we ask... is $4 empty? - is there a second word?

if there is no second word... do this, else do this

you will find that if u add this script, it will work, but it still has one fault.

your probably confused enough so i will just explain.

the alias mta.getid does just that, it gets the id number, from the name u typed.

so if u type !hp bob, it looks thru all 26 players for the word bob

if bob is not there it returns -1

if bob is there it returns the id number

if u specify the id number, eg !hp 4 - it looks to see if id 4 is used, and returns -1 if the slot is empty

so we need to add another few lines to our script just in case someone types !hp bob, and bob isnt there.

here it is.

on *:SIGNAL:mta.command:{

 var %a = $iif($4 == $null,$2,$mta.getid($1,$4))

 if ($3 == !moo) {
   if (%a == -1) mta.msg $1 $2 Error: Invalid Name/ID.
   elseif ($4 == $null) mta.say $1 $mta.name($1,$2) says moooooooo!
   else mta.say $1 $mta.name($1,$2) says mooooooo to $mta.name($1,%a)
 }

 if ($3 == !hp) {
   if (%a == -1) mta.msg $1 $2 Error: Invalid Name/ID.
   elseif ($4 == $null) mta.msg $1 $2 Your Health is at $mta.health($1,$2) $+ %
   else mta.msg $1 $2 $mta.name($1,%a) $+ s health is at $mta.health($1,%a) $+ %
 }

}

ok almost complete... however there is still more we can add.

at the moment the script contains 2 commands, the first looking for the word !moo, and the second !hp,

but if we type !moo then we're obviously not typing !hp. so by adding 'else' to the second command,

will just about do us. its not majorly important and will work fine without it, but if you have 200 commands,

it has to check all 200 every time a ! is used in the chat.

if you have a big script this can slow things down.

so the final script should look like this:

on *:SIGNAL:mta.command:{

 var %a = $iif($4 == $null,$2,$mta.getid($1,$4))
 if ($3 == !moo) {
   if (%a == -1) mta.msg $1 $2 Error: Invalid Name/ID.
   elseif ($4 == $null) mta.say $1 $mta.name($1,$2) says moooooooo!
   else mta.say $1 $mta.name($1,$2) says mooooooo to $mta.name($1,%a)
 }
 elseif ($3 == !hp) {
   if (%a == -1) mta.msg $1 $2 Error: Invalid Name/ID.
   elseif ($4 == $null) mta.msg $1 $2 Your health is at $mta.health($1,$2) $+ %
   else mta.msg $1 $2 $mta.name($1,%a) $+ s health is at $mta.health($1,%a) $+ %
 }

}

there you have it. you made your script. this is still very basic,

but by now you should be starting to understand it. if u dont fully understand what u just did

start again. dont copy and paste what i wrote, type it out so u get used to it.

now by looking at mtama scripting help you can find a list of other identifiers u can use.

so far we have used just 3 - $mta.name, $mta.health and $mta.getid

the $ at the beginning of these points to an alias within a script - the ones we have used

are writtin into mta.mrc, they return information sent from the second part of it, we used ($1,$2) and ($1,%a)

we sent the alias $2 and %a and it returned the info on each.

-----------------------------------------------------------------------------------------------------

writing to .ini files.

ok so u need info saving to a file. lets start with something basic.

on *:SIGNAL:mta.command:{

 if ($3 == !seen) {
   if ($4 == $null) mta.msg $1 $2 Error: please specify name.
   elseif ($readini(seen.ini,seen,$4) == $null) mta.say $1 $4 has never been seen in this server.
   else mta.say $1 $4 was last seen on $readini(seen.ini,seen,$4)
 }
}

on *:SIGNAL:mta.part:{
 !writeini -n seen.ini seen $mta.name($1,$2) $time $date
}

there we have it. as people leave your server - their name is written to a ini file with the time and date.

and by typing !seen it looks through the ini file for the name u specified

and retuns the info stored, if none is found says they have never been seen here.

inis are a simple way of storing information in text format - it can be easily opened and viewed by you

if the ini is going to be small - just a few lines, the -n is not needed, but i would reccomend it.

!writeini -n

so inside the ini you have sections you can have as many as you like within 1 ini.

however, try to keep them as small as possible.

so our ini is called seen.ini, inside it we have

[seen]

bla=time and date last seen

moo=time and date last seen

bob=time and date last seen

and to read this info $readini( , , ) this will return the info stored

it will return $null if nothing is stored for the name u gave.

so if u typed !seen bob

it looked in the ini called seen.ini

it looked for the section called seen

it then looked down the list the the name 'bob' and returned the info stored.

ok so we can add this to your script. it should look like this now.

on *:SIGNAL:mta.command:{

 var %a = $iif($4 == $null,$2,$mta.getid($1,$4))
 if ($3 == !moo) {
   if (%a == -1) mta.msg $1 $2 Error: Invalid Name/ID.
   elseif ($4 == $null) mta.say $1 $mta.name($1,$2) says moooooooo!
   else mta.say $1 $mta.name($1,$2) says mooooooo to $mta.name($1,%a)
 }
 elseif ($3 == !hp) {
   if (%a == -1) mta.msg $1 $2 Error: Invalid Name/ID.
   elseif ($4 == $null) mta.msg $1 $2 Your health is at $mta.health($1,$2) $+ %
   else mta.msg $1 $2 $mta.name($1,%a) $+ s health is at $mta.health($1,%a) $+ %
 }
 elseif ($3 == !seen) {
   if ($4 == $null) mta.msg $1 $2 Error: please specify name.
   elseif ($readini(seen.ini,seen,$4) == $null) mta.say $1 $4 has never been seen in this server.
   else mta.say $1 $4 was last seen on $readini(seen.ini,seen,$4)
 }
}

on *:SIGNAL:mta.part:{
 !writeini -n seen.ini seen $mta.name($1,$2) $time $date
}

ini's can be very handy to use when beginning with scripts,

people have big ideas and this is an easy way to store info.

this can be adapted now to save any info u want to save.

good luck and keep at it:D

NOTES:

These are the changes in the different versions.

MTA0.5 -> MTASA:Race

mta.name -> mta.nick

mta.say -> mta.text

mta.msg -> mta.pm

mta.health -> Not Added

Link to comment
Sc00b, I just right now tested the !writeini, and it doesn;t work, I finally got it to work somehow, but the $readini doesn't work either :o

on *:SIGNAL:mta.command:{ 
 if ($3 == !seen) { 
   if ($4 == $null) mta.pm $1 $2 Error: please specify name. 
   elseif ($readini(seen.ini,seen,$4) == $null) mta.text $1 $4 has never been seen in this server. 
   else mta.text $1 $4 $+ : last seen $readini(seen.ini,seen,$4) 
 } 
} 

on *:SIGNAL:mta.part:{ 
 !writeini -n seen.ini seen $mta.nick($1,$2) $time $date 
}

if ur trying to do it in SA, it should look like this^

it might help if u paste me what u have, or the error ur getting.

Link to comment
Can you make it say, when you are kicked, a reason for being kicked...eg Kicked from the game by console Reason: Bla bla bla?

VC or SA?

Theres a few ways u can do it, either b4 the line that kicks the player,

u can add a reason for kicking them.

or like here in mta.part, from SA

on *:SIGNAL:mta.part:command

This event is triggerd when a player leaves.

Parameters: $1 = Server, $2 = PlayerID, $3 = 0/1/2/3 (parted/kicked/banned/timed-out)

i havent tested this but, from what it says, u could add:

if ($3 != 0) mta.text $1 $mta.nick($1,$2) Parted: $replacex($3,1,Kicked,2,Banned,3,Timed-Out)

Although this would only show after the player leaves, So most people just add a reason, b4 the line that kicks them.

u could just go thru ur script and add the reasons b4 all the lines that say mta.kick

Link to comment

I tried and failed, mostly because i didn;t understand this:

   var %t 
 sockread %t 
 if (country-rirdata isin %t) { 
   tokenize 32 %my.trace 
   mta.text $1 $mta.nick($1,$2) ( $+ $mta.ip($1,$2) $+ ) - Location: $gettok(%t,2-,58) 
   sockclose $sockname 

What does the 2-,58 mean in gettok and "Country-rirdata" is something wroten on the website...I tried some other websites and they didn't work.

Link to comment
I tried and failed, mostly because i didn;t understand this:

   var %t 
 sockread %t 
 if (country-rirdata isin %t) { 
   tokenize 32 %my.trace 
   mta.text $1 $mta.nick($1,$2) ( $+ $mta.ip($1,$2) $+ ) - Location: $gettok(%t,2-,58) 
   sockclose $sockname 

What does the 2-,58 mean in gettok and "Country-rirdata" is something wroten on the website...I tried some other websites and they didn't work.

2-,58 use mirc, help, search $gettok, it explains it in full.

the trace script is fine, i tested it, i dont see why u need another :roll:

Link to comment
  • 2 months later...

How to use "While"

Var %a = 0 (Variable named %a is 0)

while (%a <= mta.server($1).cmax) { (while %a is lower than $mta.server($1).cmax, The max amount of ppl in server, then do)

mta.text $1 %a (say text %a (Say the thing that is %a))

!inc %a (Add 1 to %a, you can use !dec to lower 1)

}

With this, it will say 1 2 3... until the no. of max players!

This way you can make it write for every player in the server and so on...

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...