Author Topic: Writing a function  (Read 991 times)

Okay, so I was just working on a random RPG script solely for the purpose of learning and to add to my RPG. I wanted to write my own addExperience function so I could call it in other functions.

Here is how I wrote it:
Code: [Select]
function addExperience(%client, %tempexp, %class)
{
$claslovep = %client.claslovep;
$claslovep + %tempexp;
$classlevel = %client.classlevel;
$classreqexp = mPow($classlevel,2);
if($claslovep == $classreqexp)
{
$claslovep = 0;
$classlevel + 1;
%client.centerPrint("\c6Level Up! Your \c3"@ %class @"\c6 level is now \c3"@ $classlevel @"\c6!","3");
}
}

So if their exp reaches, they level up. Similar to the way my events worked.

And this is how I tried to call it in another function:

Code: [Select]
function servercmdup(%client)
{
if($class == 1)
{
%target = fireRaycastFromEye(%client,20,$Typemasks::PlayerObjectType);
if(isObject(%target))
{
%target.addVelocity("0 0 20");
$claslovep.addExperience(%client, 1, Wizard);
%client.centerPrint("\c6+1 Exp","1");
}
}
else
{
%client.centerPrint("\c3You must be a Wizard to cast a spell!","3");
}
}


I got the syntax error in the function at: $claslovep + %tempexp;

Any help please?

Try $claslovep+=%tempexp;
+= means add the second one to the first one.
As for calling it in another function, do it this way:
addExperience(%client,1,Wizard);

Aww I thought this was literally the question, "How do I make a function?" would've been quite fun to answer. :(

I need a life.

Ok, so it executes without syntax now, but it doesn't seem to be leveling up the client.

Here is how I wrote the function:
Code: [Select]
function addExperience(%client,%tempexp,%class)
{
$claslovep+=%tempexp;
$classreqexp = mPow($classlevel,2);
if($claslovep == $classreqexp)
{
$claslovep = 0;
$classlevel++;
%client.centerPrint("\c6Level Up! Your \c3"@ %class @"\c6 level is now \c3"@ $classlevel @"\c6!","3");
}
}

This is what happens when they set their class:
Code: [Select]
function servercmdsetclass(%client,%class)  //This is for setting class
{
if(%class !$= "Wizard")
{
%client.centerPrint("\c6You may only choose \c3Wizard \c6right now","3");
}
else
{
%client.class = "Wizard";   //Sets class
$class = %client.class;
$classlevel = 1;
$claslovep = 0;
%client.centerPrint("\c6You are now a \c3Wizard\c6.","3");
}
}

Here is one of the spells you can do:
Code: [Select]
function servercmdfly(%client)
{
if($class $= Wizard)
{
%client.player.addVelocity("0 0 20");
$claslovep.addExperience(%client,1,Wizard);
%client.centerPrint("\c6+1 Exp","1");
}
else
{
%client.centerPrint("\c3You must be a wizard to cast a spell!","3");
}
}

And this is what to use when checking your level:
Code: [Select]
function servercmdlevel(%client)
{
if($class !$= "")
{
%client.centerPrint("\c3Your "@ $class @" level is "@ $classlevel @"!","3");
}
else
{
%client.centerPrint("\c3You don't have a class!","3");
}
}

Now when I do a spell multiple times, it doesn't seem to really be adding to my experience. When I type /level, it always displays it as 1. I think I have my variables screwed up, but idk. Any help?



Would I just put that within the function? Or %client.addExperience(%client,1,Wizard);, or $claslovep.addExperience(%client,1,Wizard); ?

Replace $claslovep.addExperience(%client,1,Wizard);
It's obviously wrong.

Here is one of the spells you can do:
Code: [Select]
function servercmdfly(%client)
{
if($class $= Wizard)
{
%client.player.addVelocity("0 0 20");
addExperience(%client,1,Wizard);
%client.centerPrint("\c6+1 Exp","1");
}
else
{
%client.centerPrint("\c3You must be a wizard to cast a spell!","3");
}
}
Would this work? I just don't understand really what it's adding to. Is it adding to $claslovep? Or %client since $claslovep is part of the client?

if($class $= "Wizard")
Do it like that, $= is a string comparison, otherwise it's not really a string.
Same with %client,1,"Wizard".

Would this work? I just don't understand really what it's adding to. Is it adding to $claslovep? Or %client since $claslovep is part of the client?
$claslovep is NOT a part of %client. $claslovep is a GLOBAL variable. Usable in all functions and throughout the game.
Example:
Code: [Select]
function setGlobal(%val)
{
$global = %val;
}
function echoGlobal()
{
echo($global);
Doing setGlobal(23); then echoGlobal(); would put 23 in the console.

You would need to do %client.claslovep rather than $claslovep

Ok thanks for clearing me up. I was all jumbled up, well that should solve it then.
Final question:
Would I put %client.addExperience(%client,1,"Wizard");
Or is %client already designated in the first arg?

It would be just addExperience.
Unless you change the function to GameConnection::addExperience,
then you would use %client.addExperience(1,"Wizard");

Either way works.

Awesome, thanks. That helped a ton.