Jump to content

User defined functions for dummies?


Guest
 Share

You are about to reply to a thread that has been inactive for 3524 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Before starting with my question I wanted to thank you all!. It was the generous help in this forum that helped me getting my first project started (and then reading on in the wikis): An adventure game system.

This was a quite ambitious project for someone that had never written one single line of code in any computer language before. Now I have finished a proof of concept for my system and it works quite neat (I think). If you want to see what your initial has helped me to achieve, you can try the mini-game for free here: http://maps.secondlife.com/secondlife/Escapades/149/183/24

in the meantime I have started to develop a "real" game based on the system and I realised that with more content, the script lenght and efficiency is really getting an issue. Amongst other things I would like to create some user defined functions for repeating processes. The lslwiki as well as the official sl-wiki give a short overview and 2-3 random examples. When reading the text I have the feeling that it's made for programmers who already have some experience with user created functions in other languages and who just need to get their head arround the lsl specifics. The explanations seem not to be sufficient for someone who has never done anything like it, like me.

Do you guys know any tutorials that explain the nature of user created functions in a more or less systematic way, starting with simple functions? Maybe in another scripting language than lsl? I would so much appreciate finding a source that helps me getting my head arround this topic.

Link to comment
Share on other sites

It's not a particularly arcane topic.  A user defined function is simply a bit of code that you have made global so you can access it from any point in your script.  This means that you can save on script memory by writing the code only once and then calling it repeatedly, instead of copy/pasting the code in several parts of your script. For example, you can write a function called Debug that looks like this:

Debug(string message){    if(gVERBOSE)    {        llOwnerSay(message);    }}

 

And then call it from any event or state in a script:

integer gVERBOSE = TRUE;  // Change to FALSE to suppress error messagesDebug(string message){    if(gVERBOSE)    {        llOwnerSay(message);    }}default{    state_entry()    {        Debug("The script is in state default.");    }    touch_start(integer num)    {        llSetRegionPos(llGetPos() + <1.0.,0.0,0.0>*llGetRot());        Debug("The object should have moved forward 1 meter");        state next;    }}state next{    state_entry()    {         Debug("Control has passed to state next.");    }    touch_start (integer num)    {        float time = llGetAndResetTime();        llSay(0, "Ths script was reset " + (string) time + " seconds ago.");        Debug("Resetting time.");        state default;    }}

 This is a trivially simple function, but it saves you writing the same if test and  llOwnerSay command over and over again, and makes it possible to switch from debugging mode to normal mode by changing the variable gVERBOSE at the top of the script.  It's not usually worth doing this sort of thing unless you are going to use the function in more than three or four places, so I use user-defined functions sparingly.  However, if you have utility functions that generate new menus or check a user's current status at several steps in a process, for example, they can make your script more efficient and much easier to read.

Visit the College of Scripting, Science, and Music in world.  They have a nice set of examples to walk you through the finer points of writing user-defined functions.

 

 

Link to comment
Share on other sites

  • 2 weeks later...

While the College of Scripting is great for starters and the wiki (User-defined functions) goes into a little more detail, neither explain it very clearly.

Functions can accept data passed into them and return data out of them, depending on how they're declared in the script.

Example 1

Do something.  No data passed in or out.  Note that global variables can still be changed and local variables can be used.  Called from anywhere in your script using do_something();  Even though no data is passed into the function, the parentheses still have to be there.

 

do_something(){    // Do stuff}

 

Example 2

Pass data into a function for use.  Variables can be any data type and names of your choosing.  In this example we pass text into a variable named 'text', which we then use in the function to place the data where we want it.  Example: say_something("The menu has expired.");

 

say_something(string text)
{
    llSay(PUBLIC_CHANNEL,text);
}

 

We can have more than one variable passed to our function.  Example: say_turns_left(llKey2Name(current_player_key),turns);

Sample output:  "Yingzi Xue has 5 turns left."

 

say_turns_left(string name, integer number){    llSay(PUBLIC_CHANNEL,name+" has "+(string)number+" turns left.");}

 

Example 3

We can also return values back from the function using 'return' followed by a data type that matches our declared data type before the function name.  So... in our script:  calculate_word_score("delicious",100,50); would return an integer of 950.  We could calculate our word score and store it in our current_score all in one statement:

current_score += calculate_word_score(submitted_word,current_score_modifier,current_bonus);

 

integer calculate_word_score(string word, integer modifier, integer bonus){    return (llStringLength(word)*modifier)+bonus;}

 

 

The format is as follows:

 

return_data_type function_name(passed_data_type variable_name, passed_data_type variable_name...){}

 

As shown in the first example above (do_something), you don't have to set a return data type if you're not returning data.  You don't have to pass data into the function.  What your function does is directly dependent on how you declare it.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3524 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...