Raised This Month: $12 Target: $400
 3% 

2-Part Beginner Scripting Tutorial [REVISED]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 08-09-2008 , 12:23   2-Part Beginner Scripting Tutorial [REVISED]
Reply With Quote #1

I was really bored last week..So for your enjoyment I made a 2-part tutorial covering the VERY basics of amxx scripting for those new scripters who don't understand how anything works.

I really hope you enjoy them and learn from them. +Karma is appreciated if you like my work.

Part I - The Beginning...

Code:
/* This is my part one of my first tutorial, meant for people that just cannot learn scripting.
What we're going to do with this tutorial is make it so when a player types /tutorial, they get
special abilities and stuff.  In part II, you will learn more basics and stuff we can add on
to make this plugin better.
 
You can PM me on AlliedModders if you have questions or need extra help.  I hope
you all like it.  By the way, if I put // in front of a line, that means it's a comment.
If you want a comment to be multiple lines long, do what I did like in this comment.
You can delete all the lines with // in front once your finished, if you want.
*/
 
// These are the modules you need to make the plugin work.  Leave them alone
// unless you know what you're doing.
#include <amxmodx>
#include <cstrike>
#include <fun>
 
// These define the name, version, and author of the plugin.
#define PLUGIN "Beginner Tutorial Part I"
#define VERSION "1.0"
#define AUTHOR "PvtSmithFSSF"
 
// Think of this as your control center, where many things happen.
public plugin_init() 
// These {'s and }'s are needed whenever you have code.  They need to surround all the code.
{    
    // This defines what we wrote above.
    register_plugin(PLUGIN, VERSION, AUTHOR)
 
    // Register_clcmd is saying 'When a user types 'something' , then 'something' happens.
    register_clcmd("say /tutorial", "steroids")
    // The first parameter ("say /tutorial"), is where you define what the user must type
    // in chat, in order for the second parameter ("steriods") to happen.  So this is saying,
    // "When a user types /tutorial , "steriods" happens.  But AMXX doesn't know what 'steroids'
    // is yet.  So we tell it.
}
 
// This tells AMXX what 'steriods' is, which is what you made above.
public steroids(id)
{
    // Now lets make something happen.  Everything here will happen when a user
    // types /tutorial .  This is because of the register_clcmd that we just put in.
 
    // So let's set the players health to 150. This is easy, and only takes one line.
    set_user_health(id, 150)
    // set_user_health is the command, don't mess with "id", and 150 is the amount
    // to set the players health to.
 
    // Now let's give the player 400 gravity (800 is normal).
    set_user_gravity(id,0.50)
    // This is a bit more tricky, but still simple. 
    // set_user_gravity is the command. Once again, don't mess with "id".
    // 0.50 is the amount of gravity to set them with.  It's at 0.50 because if we set
    // it to "1", that would be 800 gravity (normal gravity).  0.50 is half of 1 so it's
    // half of 800 gravity (400 gravity).
 
    // Now.. let's send them a message in chat.
    client_print(id, print_chat,"You just purchased some steroids, dude!")
    // client_print is the command, 'id'.. just leave it, 'print_chat'.. just leave it,
    // and what's in quotes, that's the message to display.
}
 
// Now, when a user types /tutorial, his health goes to 150, his gravity is set to 400, and
// a message is sent to him saying "You just purchased some steroids, dude!"
// This part I of II tutorial was meant to show you the basics.  In part II,
// I will show you some cooler things you can add and some more advanced things you 
// can do to spice up your new plugin.
 
// I hope you enjoyed reading this. If it helped you, it never hurts to leave me
// a good ole +karma on the forums.  It's more appreciated than you think!
// - Pvt. Smith [FSSF]
Part II - A little more advanced...But still simple.

Code:
/* Welcome! This is part II of II in "Beginner Tutorial" in my scripting tutorials series.
If you haven't read the last one, please read it.  I won't explain things that I already
explained in Part I.  I will only explain the new things that I introduce. */
 
// Here we go!
 
#include <amxmodx>
#include <cstrike>
#include <fun>
 
#define PLUGIN "Beginner Tutorial Part II"
#define VERSION "1.0"
#define AUTHOR "PvtSmithFSSF"
 
// These register new phrases called "plugin_on" and "price" for the plugin to use later.
new plugin_on
new price
 
public plugin_init() 
 
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_clcmd("say /tutorial", "steroids")
 
 
// This tells us what "plugin_on" and "price are. We registered the phrases so
// now we say what they are. We're going to make an ON/OFF cvar out of plugin_on,
// and we're going to make a cvar for the price of steroids (price).
plugin_on = register_cvar("amx_tutorial_plugin", "1")
price = register_cvar("amx_tutorial_price", "4000")
// register_cvar is the command to use. "amx_tutorial_plugin" or
// "amx_tutorial_price" is the cvar name.
// For example the cvar that is typed in game will be whatever is here. 
// "1" is what the default is set to for the cvar amx_tutorial_plugin. 
// We set it to 1 because we want the plugin to be on when the server starts, 
// don't we? The 4,000 on "amx_tutorial_price is the default cost of steroids.
}
 
public steroids(id)
{
// This is making a new phrase called 'money' that we can use later.
// What we're saying here is "the phrase 'money' means "Get a player's money".
// So whenever we have 'money' written in this section of the plugin, 
// it will get a player's money.
new money = cs_get_user_money(id)
// The reason we put it here and not with the others at the beginning of the plugin
// is because this line contains (id) in it, and that must be there. 
// You cannot put (id) where the others are, unfortunately.  So we put it here
// where we need it.
 
// Before we do anything, we want to make sure that plugin_on = 1.  
// In other words, we want to make sure the plugin is still on, by checking
// if the cvar is set to 1.
if (get_pcvar_num(plugin_on) == 1)
 
{
 // this is saying if a player's money is more than/equal to the price, we give them everything.
 if (money >= get_pcvar_num(price))
 {
 
  set_user_health(id, 150)
 
  set_user_gravity(id,0.50)
 
  client_print(id, print_chat,"You just purchased some steroids, dude!")
 }
 
 // and this is saying 'if money is was NOT equal to or greater than the price, 
 // we display them a message shown here.
 else
 {
  client_print(id, print_chat,"Not enough money. Go work out, loser!")
 }
 
 
 // This makes it cost money.  What we're doing here is saying
 // "set the user's money to:  money - price".  Now remember, since we registered
 // the new phrase 'money', it now gets a players current money amount. Also, we
 // set "price" to what this will cost.
 // So what this will do is actually get the player's money and subtract whatever
 // the admin has the cvar set to, making it cost whatever the cvar is set to.
 cs_set_user_money(id, money - get_pcvar_num(price))
}
// Yes, the "if (plugin_on == 1)" function needs its own {'s and }'s because it's saying
// "If plugin_on = 1, than do 'this'"
 
 
// I hope you learned a lot.  PM me on AlliedMods for questions or extra help.
// If it helped you, it never hurts to leave me a good ole +karma on the forums.  
// It's more appreciated than you think!
// - Pvt. Smith [FSSF]

Last edited by PvtSmithFSSF; 11-10-2008 at 08:37.
PvtSmithFSSF is offline
fxfighter
Veteran Member
Join Date: Feb 2007
Location: Trollhättan
Old 08-09-2008 , 17:19   Re: New to scripting? This will get you started.
Reply With Quote #2

Some minor corrections
Quote:
Originally Posted by PvtSmithFSSF View Post
Code:
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>
cstrike and amxmisc isnt used in your script.
wouldnt hurt telling them that they give acces to other powerfull commands/nativs
Quote:
Originally Posted by PvtSmithFSSF View Post
// These {'s and }'s are needed whenever you have code. They need to surround all the code.
You should explain that they are only needed when your doing more then one thing.
Quote:
Originally Posted by PvtSmithFSSF View Post
// Register_clcmd is saying 'When a user types 'something' , then 'something' happens.
register_clcmd is when a user ho's runing cs types something in his console
register_concmd is when someone types something in server or clients console
register_srvcmd is when something is typed in the servers console
Quote:
Originally Posted by PvtSmithFSSF View Post
// So whenever we have 'money' written in this section of the plugin,
// it will get a player's money.
new money = cs_get_user_money(id)
it stores the value in the integer not get it every time.
Quote:
Originally Posted by PvtSmithFSSF View Post
// The reason we put it here and not with the others at the beginning of the plugin
// is because this line contains (id) in it, and that must be there.
// You cannot put (id) where the others are, unfortunately. So we put it here
// where we need it.
just tell them that they can only use the integer id in the function where it was created.

other stuff*

you should tell them that the thing you calld phrases are integers and explain what they do and what they store in this plugin.
you should explain that id is storing the first parameter in the function and it contains the players number ho triggerd it in your cases.

Exept those stuff it was nice keep it up and sry about my spelling.
__________________
If one of my plugins become broken, contact me by mail. [email protected]

Last edited by fxfighter; 08-09-2008 at 17:30.
fxfighter is offline
Send a message via MSN to fxfighter
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 08-09-2008 , 18:31   Re: New to scripting? This will get you started.
Reply With Quote #3

Trying to make it 100% newb-friendly so calling them 'integers' may confuse )
But cs_get_user_money uses cstrike include, no?
PvtSmithFSSF is offline
fxfighter
Veteran Member
Join Date: Feb 2007
Location: Trollhättan
Old 08-10-2008 , 02:25   Re: New to scripting? This will get you started.
Reply With Quote #4

wierd was able to compile it whitout it OO
But it dosent hurt to tell them what everything do and how it all work.
Leaving some minor details out might confuse them later on.

Explaining the integers and what they do in your cases are quite important so they know what they do exactly and how they work.
So they dont have to ask if they used it right later on.

Think its good calling them integers because when they search for help later on they will easly find it if they used the correct words.
But its a nice guide^^ so keep it up as i said
__________________
If one of my plugins become broken, contact me by mail. [email protected]

Last edited by fxfighter; 08-10-2008 at 02:35.
fxfighter is offline
Send a message via MSN to fxfighter
whyme?
New Member
Join Date: Aug 2008
Old 08-10-2008 , 02:34   Re: New to scripting? This will get you started.
Reply With Quote #5

Hi, im new to scripting.
very nice tutorial.
if possible to make:
part.III - building menus.
part.IV - how to add commands to menu.

thanks
whyme? is offline
G-Dog
Senior Member
Join Date: Dec 2005
Location: Thunderstorm Central
Old 08-10-2008 , 06:03   Re: New to scripting? This will get you started.
Reply With Quote #6

an error I noticed with part II code is your usage of price, and plugin_on. The way you defined them they are pcvars thus instead of
Code:
if (plugin_on == 1)
you should have
Code:
if (get_pcvar_num(plugin_on) == 1)
same for price. To use them instead the way that you did you would have to do this in plugin_init
Code:
    register_cvar("amx_tutorial_plugin", "1")
    register_cvar("amx_tutorial_price", "4000")
    plugin_on = get_cvar_num("amx_tutorial_plugin")
    price = get_cvar_num("amx_tutorial_price")

edit:
yes do
Code:
cs_set_user_money(id, money - get_pcvar_num(price))
and upon better inspection of the code since the mass comments overwhelmed everything, I would suggest that you first check if the user has enough money to afford the steroids before setting the health and gravity. Also rather than just arbitrarily setting heath to 150 I would instead add 50 health since someone could use the command just before dieing to reset health to max each time.
Code:
public steroids(id)
{
    if ( !get_pcvar_num(plugin_on) ) {	
    /*
    ! operator means false which equates to 0 in numerical sense so this equates
    to if ( get_pcvar_num(plugin_on) == 0 )
    */	
        client_print(id, print_chat, "Steroids currently disabled");
        return;		//ends the function on the spot
    }
    new money = cs_get_user_money(id);
    new cost = get_pcvar_num(price);	//rather than calling get_pcvar_num 3 times, store it to a variable
    if ( money >= cost )	//run a check on if user has enough cach
    {  
        cs_set_user_money(id, money - cost);  
        set_user_health(id, ( get_user_health(id)+50 ) );	//sets user health 50 points greater tehn current health incase they have more or less than starting 100
        set_user_gravity(id, 0.50 );
        client_print(id, print_chat, "You just purchased some steroids, dude!");
    }
    else 	//if they didn't have enough cash display a message stating how much more they need
    	client_print(id, print_chat, "Sorry you need %d more cash to afford steroids", cost-money);
}
__________________
If at first you don't succeed, then skydiving isn't for you.

Last edited by G-Dog; 08-10-2008 at 13:47.
G-Dog is offline
Send a message via AIM to G-Dog
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 08-10-2008 , 09:43   Re: New to scripting? This will get you started.
Reply With Quote #7

@ fxfghter thanks for tips, oh and btw i just noticed you said clcmd was when they typed in console.
I thought it was when they typed in chat? What is, if this isn't?
@ whyme, read Emp's tutorial about menus. If you can't understand it post here again and I'll try to make a more noob-friendly one.
@ g-dog thanks a lot but what do I need to change for price?
Change that line to:
cs_set_user_money(id, money - (get_pcvar_num(price))

??

Last edited by PvtSmithFSSF; 08-10-2008 at 09:52.
PvtSmithFSSF is offline
fxfighter
Veteran Member
Join Date: Feb 2007
Location: Trollhättan
Old 08-10-2008 , 12:39   Re: New to scripting? This will get you started.
Reply With Quote #8

register_clcmd only a client can use it
register_concmd server and client
register_srvcmd server only

so if you use concmd whit a say cmd the server can trigger it to.
So use clcmd for say msgs.

When they say something in chat they use say or say_team cmd in the console.
__________________
If one of my plugins become broken, contact me by mail. [email protected]
fxfighter is offline
Send a message via MSN to fxfighter
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 08-10-2008 , 16:44   Re: New to scripting? This will get you started.
Reply With Quote #9

Yah that's how I have it
PvtSmithFSSF is offline
fxfighter
Veteran Member
Join Date: Feb 2007
Location: Trollhättan
Old 08-11-2008 , 04:33   Re: New to scripting? This will get you started.
Reply With Quote #10

just your explanation of it was wrong.
you should update the first post betwin whit the changes.
__________________
If one of my plugins become broken, contact me by mail. [email protected]
fxfighter is offline
Send a message via MSN to fxfighter
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:55.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode