Contents
Chapter 4: Lets
make a donation box
In this chapter we will be making our next product. This will be a
donation box that accepts donations and shows the highest donator. To
do this we need to do test to see if the donators amount is higher
than the current highest and replace it if it is. To show the highest
donated we will use floating text. Lets also make it so the donation
box will accept a user and pay that user. That way we are not only
learning how to accept payments but also how to send that payment to
the person using the donation box.
Lets
Get Started
Ok so the first step is to create a script within an object. You
should know how to do this by now. I am going to see how much you
have learned and let you do the set up scripting. In this script we
want to have two states. We learned how to do states in the last
chapter so I will leave that up to you to figure out. Lets call the
states, default and inUse. Both of these names are without spaces.
In the default stage setup a touch_start event. In that touch
event make the script say “Welcome to the donation box Raiden
Faxel. You are now logged into me.” . Replace Raiden Faxel with the
name of the avatar touching the script. Then make the script swicth
to the inUse state.
In the inUse state make a touch_start event and make that event
say “Thank you for using me Raiden Faxel. You earned $L0”. Again
replace my name with the name of the avatar touching the box. Then
switch back to the default stage.
Do you have your script written? Ok... lets see how well you did.
Your script should look something like this.
How did you do? Yours may be slightly different. Touch the object
and test it. If you are see the two messages, a different one each
time you touch it then your script is working well and you have
learned for the past chapters, well done. If not, take the time out
to look at my script and compare it to yours. What did you do
different?
Sometimes when you write scripts you will try to save it and you
will get this message.
This reads as so. On line 24, character 4 you have an error. You
will also notice that the line 24 is in bold. This is showing you
that this is the line that has the error. In this example and this
can be the most common error, I forgot to place a ; at the end of
line 23. The ; tell the script that it is the end of a line. Every
line needs one at the end.
llSetText
If you are looking to script in Second Life then you have
definitely been around long enough to see objects and people with
text above them. It may be something from a floating text above
avatars heads or a donation box with some text over it. The llSetText
is how this is achieved.
So lets go ahead and add the floating text.
Step 1: Add a state_entry event to each of your default
state.
Step 2: Add the following
line of code inside the state_entry event.
Step 3: Save your script.
You will notice that the floating text
your entered will appear above the prim your script is in.
Before we continue I want to explain
what vectors are.
Vectors
Vectors are a group of 3 floats used as a single variable. They
are seen as <x,y,z>. They can be used to set locations ,
velocity, and colours. In locations you have an x, y, z location.
With velocity we have speed on x axis, speed on y axis and speed on z
axis. In our script we have used the vector for a colour. This is set
as an RGB colour code (Red, Blue, Green). You can get all the RGB
colours you want from this a quick search on google. Just type RGB
colour codes. With the vector x = Red, y = Blue and z = Green.
Alpha
So now we have a little understanding on vectors, the other thing
I want to explain is alpha. In the SetText command we have an alpha
parameter. The alpha parameter set how invisible the set is. 0.5 will
make the text 50% see through. 0 will make is not show at all. 1
will make the text 100% visible play around with your text for a bit
changing the colour and alpha.
Clearing
Floating Text
If you don't want your text to appear anymore all you have to do
is set the text to nothing.
The important thing to remember is this. If you add floating text
to your prim, then decide you don't want that anymore, you can't
simply remove the line of code that sets the text. This will keep the
text there. The prim itself has a text variable and you are just
setting it. So if you set the text and then remove this script, the
text will remain. Before you remove the script you must first clear
the floating text and run the script.
Continue
Our Scripting
Ok now let continue with our script. Add a state_entry event to
the inUse state. Now add some floating text to the inUse state_entry.
Make this one say “Raiden Faxels Tip Jar\nAll Tips Greatly
Appreciated\nTop Tipper Sigh Parx $L0”. Of course change the Raiden
Faxel to the users name. Later we will change Sigh Parx to the
actually highest tipper and the amount they tipped. The \n in the
text makes a newline in the floating text. In order to change the
name to the user we need to store that name when they log in. We will
also store their key for later use. Add this at the top of your
script.
Then in the touch_start event of the default state, set these
variables with the following code.
Now we can use this variable in the state_entry of inUse to change
the floating text to include the users name.
Your code should look like the following.
Accepting Payments
There's only one command you must add to accept payments into an
object but there is an event you must add also to run code when
someone makes a payment. Without either of these you can not accept
payments. So lets look into that. To accept payments into your script
you have to set the pay price of the prim. This is done with the
following code.
The price integer is the default amount you want in the custom pay
box. The list of quick_pay_buttons are the buttons people can press
to pay that amount. Before we start this let me explain lists.
List
A list is a group is variables in one variable. You can have a
list of all different types of variables except other lists. I like
to see a list as an organised box of things you want to use later.
For example I can have a list of avatar names. I would write this as
follows.
Each name in my list has an index number. The first name in the
list has 0. Then each index goes up by one. So Raiden Faxel has 0,
Sigh Parx would be 1, Roxy Beliveau has 2 and Warfina Tisch has 3. We
can use these numbers to get the information later by using the
llList2String command.
So to get “Sigh Parx” for the list all we type in is this.
Let's Add The Pay Option
Ok so we are ready to add the pay option.
Step 1: In the
state_entry event of the inUse state create a list with four payment
prices. I like 50, 100, 200, and 500 as a default.
Step 2: Now add in the
SetPayPrice command. We'll put the price as 100 and use the
quick_pay_button list we just created.
Step 3: Add the money
event to the inUse state.
The money event simply gets the
avatars key and the amount they donated. We'll look into this very
soon.
Step 4: Save your script
and test it. Log into your donation box, and then right click the
object to select pay. You should see this dialog come up.
As you can see our quick pay buttons
are on the side and our default amount is in the other amount
textbox.
This script is the basics of a
donation box. But we are going to go a little further and put the
highest donator in the floating text, and we also need to make this
pay the person logged in. At the moment it will pay the owner.
How it works
As the script starts the script starts
in the default state and waits for someone to touch it. When they log
in it moves to the inUse state when the llSetPayPrice command runs
and sets the buttons for the donator. Having the money event in as
well allows the object to then show the pay option. When an avatar
pays an amount they are given a dialog to choose an amount. When they
click pay. The amount is taken from them and given to the owner of
the object.
GiveMoney
This script is what takes money from
you and hands it to another avatar. But there are a few things you
need to understand about this command before using it. First of all,
there is no way to know if the money was actually sent or not. Lets
say the script says to send $500L but the avatar only has $100L.
There is no way to test to see if the full amount was paid. There is
another command that gives all this information but for this script
we are only sending the funds as someone makes a donation, so the
money will always be there. Second, a give money command must be
accompanied by a permission allowing for the the transfer of funds.
The give money command looks like
this.
The destination script is the avatars
key where the money will be sent. The amount is the amount of lindens
that will be transferred.
Let's add this into our script.
Step 1: Add the
permission line of code in the state_entry of the default state. This
will ask the owner for the permission to send the donated money to
the logged in user.
There are many other permissions to
give but for now this is the only one we need. As you can see this
gets the owners key and requests the PERMISSION_DEBIT permission. The
avatar will be sent this permission dialog.
Step 2: Add the give
money command to the money event we added in the inUse state. We can
use the userKey for the destination. We can also use the amount
variable in the money event.
Step 3: Save your script.
You will be sent the permissions if you have your script in an
object in the sim. Accept the permissions,. If you have a second
avatar, try logging them on and log them into the donation box. The
use you avatar to make a donation. This should pay your first avatar,
then your first avatar will pay the avatar using the donation box.
If you use 1 avatar you will see, you
have paid [username] [amount] twice. This is because you made the
donation which goes to the object owner. Then the object sends that
donation to the avatar logged into the donation box, which is also
the owner in this case.
How It Works
When the script first starts the owner
is asked to accept permissions to take lindens from them. This is
required to send the money to the avatar using the donation box. When
someone makes a donation the money is sent to the owner and then the
script sends that amount from the owner to the person using the
donation box.
Notes
There is a lot of worry out
there about vampire bites that take your money or other scripts that
do the same. This is not possible. For someone to take money from you
you need to first accept the permission for the money to be taken
from you. If you get a permission and you dont know why, deny it
straight away. It is possible to place a give money command inside a
product you buy and then drain you money. So if there is no reason
for a scrip to to take your money, deny to permission. However if its
something that must use money then you kinda need to take that risk.
If you see your money draining just take the object out of the world
and then money will stop being taken.
The second thing that must happen to
take your money, is the object has to be owned by you. No one can
make a script to take money from you in an object they own and
without permissions. So relax a little with all these scares.
Final Additions
The last thing we need to do is show
who made the highest donation to the avatar and a total amount of
donations made. Lets just jump in and do it.
Step 1: Add three more
global variables. Global variables are ones outside all of the script
so we can use them through the whole script. These variables will
be...
highestName will be used for the
avatars name who donated the highest amount. HighestAmount will be
used for the highest amount that avatar donated and totalDonated will
be the total amount of lindens the logged in person have raised.
Step 2: In the money
event we need to add the amount donated to the totalDonated.
What this says is, “Set the
totalDonated to what totalDonated already equals plus the amount the
avatar donated.
Step 3: Change the
message the user gets when they log out to show the total donated.
Use write (string) before totalDonated
because totalDonated is an integer not a string and we are trying to
add it to a string. This tells the script to treat it as a string.
Step 4: Reset the total
donated when they log out. Do this by setting totalDonated to 0 when
the script enters the default state_entry event.
If statements
So now lets look at if statements.
These run tests on informations to run different code. And if
statement looks like this.
The condition is something that must
equal true for the code inside the brackets to run. Lets take our
code example. We need to see if the donated amount is higher than the
current highest donated amount.
However we can not write it like this.
The script will not understand.
If we place the if statement in the
money event we can use the amount they donated against the
highestAmount variable we made. That would look like this.
The code inside the curly brackets
will only run if the if statement is true. We have a few things we
can use to do tests.
<
|
Less Than
|
>
|
Greater Than
|
==
|
Is Equal To
|
!=
|
Is Not Equal to
|
You can also use a string in a if
statement. For example if avatarName = “Raiden Faxel”
So lets add it all in.
Step 1: Add the if
statement above in the money event.
Step 2: Inside the if
statement set the highestAmount to equal the amount.
Step 3: Set the
highestName. We do this by running a new command call llKey2Name.
This command looks like this.
This command takes and avatars key and
gives back the avatars name. We can use the id in the money event to
get the donators key. Heres the code to set the highestName to the
avatar who donated.
Step 4: Set the floating
text to show the highest donator. This must be done in the if
statement as we only want it to change if the amount is higher.
The final code inside your money event
should be.
How It Works
When a user donates, the money event
is run the amount donated is added to the total amount and then the
money is sent to the avatar using the donation box. The amount is
then tested to see if it is bigger than the current highest amount
and if it is the amounts are changed and the floated text updated to
show the highest donator.
One More Thing For
You
At the moment if someone is logged in
anyone can log them out just by touching the box. We need to stop
this from happening by seeing if the avatar touching the box is the
same avatar that has logged in. If not, tell the toucher the donation
box is currently in use. The other thing to do is when the user logs
out we need to reset the amounts in highest donator. I'll leave this
to you to figure out. And show you the code next week.