Before updating to Windows 10 you may need to return your Pro license. More information here

Return? How to do it?


I just have to ask about something I have always wanted to learn how to do myself, when it comes to scripting.

I am not very experianced on writing scripts myself, but I have been working for along time whit plugin development, such as "Bukkit, for Minecraft"! Which brings me to my question..

When I scripted to make plugins, whit the use of the "Bukkits API", there was some methods I cant seem to understand how to make, nor can I seem to find help to learn how to make these, since I have no clue what they are called.. Let me give you an example code I would be able to use;

something.getLocation(); // this would return an Vector3 something.getLocation().getWorld(); // this would return the string for the world name!

What is this called, and how can I make them in my game? I dont even know how to make something return anything else then true or false! (bool)!

It just dont make sense to be, I see why the first example can return Vector3 (even though I dont know how), but the second line? How can it suddenly return this string?

I hope someone can help me, so that I in my game can make something like this;

players.getExperiance().getCurrentExperiance(); // this then returns the current exp of the players!

Thanks, Winsjansen

EDIT; also, in the API, I could do this;

if(something.getLocation().getWorld == "somewhere"){. // returns true if the world have that name!

more ▼

asked Sep 19, 2012 at 01:31 AM

avatar image

148 16 25 30

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

The key to this is public methods. A method that is public is accessible outside the class you write it in, and a many development environments will be able to populate the auto-complete suggestion field with these methods. Avoid making things public when you don't need to, however! It might not be a good idea to let your modders tamper with all your functions - expose only what you need to.

Planning ahead makes most projects easier, but in the case of an open source system or even just a moddable API, you'll really really want to plan things out. If you don't, you'll end up with a bit of a mess, which will make exposing it to modders both difficult for you and confusing for them.

If you've structured it such that something.getLocation() returns a class you've written, and that class has a getWorld() call in it, the intellisense handles the rest.

Here's some sample files that you can use to see the suggestion box in action:

In a file called SampleLocation:

 using UnityEngine;
 using System.Collections;
 public class SampleLocation : MonoBehaviour {
     SampleWorld myWorld;
     //other stuff related to a location - maybe some textures, or enemy spawning code; who knows!
     public SampleWorld getWorld()
         return myWorld;

in a SampleWorld file:

 using UnityEngine;
 using System.Collections;
 public class SampleWorld : MonoBehaviour {
     //some stuff that worlds do
     string worldType;
     public string getType()
         return worldType;


A SampleWorld object can now have the .getWorld() method called; the result of that call (a sampleWorld) will be able to have the .getType() method called on it. This means we can now write something like, mySampleLocation.getWorld().getType(), and it'll function.

Note that the sample code above doesn't handle assigning the variables it's using, so you'll get some null references if you try it. You can fix this by making the variables public and assigning them through the inspector, or by providing additional logic (probably in a startup method?) to fill these variables out prior to these calls. You could also ditch the variables entirely, and have the return values be calculated when you call the method - you have a lot of versatility there, which is why I didn't write that portion out - it'll most likely change from place to place in your code.

Does that help?

more ▼

answered Sep 19, 2012 at 10:50 PM

avatar image

Michael Covert
296 1 2 3

Thanks for the reply, and YES, it looks like you understand what I am looking for to do!

I find it abit hard to understand though, why it works! I see that I know can do "something.getLocation().getWorld().getType(); but can any of these alone return something?

I should be able to use "something.getLocation();" and that would alone return Vector3, but if I simple added "getWorld();" after it, no Vector3 will be returned, just the string for the world name! and, if I again add "getType();" it would not return the string for the world name, only the string for the type!

I know that I could just make one method for each, and just document the API for the developers to call different methods for everything, but I see this as so much better way to do it, so I can just document everything that has whit location to do, under getLocation();!

I will try look closer to your reply and see what I can get out of it! Thanks, we are atleast on the same page!

Sep 20, 2012 at 04:09 PM winsjansen

@winsjansen: Yes and no. Your getLocation function does return something. It always returns the same thing, a reference to a SampleWorld-class (at least in michael's example). It's not possible (in almost all compiled language, Java included) to return different types with the same function. Even in a dynamic languages the function can't determine what you would expect as return value.

However, it is possible in C# to create an implicit casting operator which can "convert" your custom class into another type.

For example when you implement an operator like this in your SampleWorld class you can use the class "like a string":

     public static implicit operator string(SampleWorld w)
         return w.getType();

Such operators might cause confusion because you can't see what happens behind the scenes. It's always better to use destict functions or properties.

Btw, every class in C# / .NET has a built-in function called "ToString" (same in Java, there it's called "toString") which will return "the string representation" of this type. For number types it's usually just the number formatted as string. Classes usually just return their classname by default. In custom classes you can override this function to return something else. For example the Vector3 struct in Unity will return a string that looks like "(x,y,z)".

The compiler will automatically call ToString when you concat your class with a string.

 int i = 5;
 string s1 = "i equals" + i;
 string s2 = "i equals" + i.ToString();

The s1 and s2 example is exactly the same. In the first case the compiler detects automatically that you want to concat an integer with a string and it calls ToString implicitly.

Another way are extension methods which do not exist in Java(Note: I just mention Java because you said you developed a Bukkit plugin. Java has nothing to do with Javascript or UnityScript). Extension methods allows you to kind of "inject" your own method into another existing class. When you try to call a function on a class that doesn't exist in the class itself or anywhere up the inheritance chain, the compiler will look for an extension method.

Extension method can be declared in any static class and have to take the extending class as first parameter with the additional "this" keyword.

@michael-covert: You have to be careful, GetType() is already a built-in function (note the capital "G").

Sep 20, 2012 at 04:58 PM Bunny83

Each of those methods does return something, yeah. You can call any of them individually, and do whatever you want with the result. In fact, when you call the chain like that, your code isn't actually running them all at once - the compiled version of your code will really be calling them individually, storing the results, then calling the next function on the result of the previous call. You're also right - it's a lot better to stick everything related to locations in the location class, everything related to worlds in the world class, etc. It'll make things a lot easier for you later on, and I wasn't trying to suggest otherwise; rather, encourage it!

Sep 20, 2012 at 05:01 PM Michael Covert

Btw, i never developed a bukkit plugin, but getLocation() doesn't return a Vector3, it returns a Location class. This can probably be implicitly casted into a Vector3, but it isn't a Vector3. "getWorld" is probably a function of the Location class and i'm pretty sure it will return a "World" class. The World class has probably it's toString function overridden so it returns the name of the world.

I just did a quick search and found the header of the World interface ;) The world has lots of methods and one is called "getName()". This is probably what is used.

This is btw the Location class

Sep 20, 2012 at 05:15 PM Bunny83

I'm aware GetType() is a function; I'd normally name it differently, but I was simply following his naming - and since getType() doesn't bother it, I figured it wasn't too important to the discussion at hand.

Sep 20, 2012 at 05:24 PM Michael Covert
(comments are locked)
10|3000 characters needed characters left

This depends on the language.

UnityScript is easier. You just return whatever type you want:

 function getVector()
   return new Vector3(1,2,3); // for example. returns a Vector3

Vector3 is a 'class' which itself has many methods (functions) that come for free. One of them is ToString() which will return a string, like so:

 function ToString()
   return new String (v.x+","+v.y+","+v.z); // for example

== is also a function which returns a boolean. I forget exact syntax for defining those, but you can find that at MSDN.com

more ▼

answered Sep 19, 2012 at 01:35 AM

avatar image

34.7k 420 360 561

Thanks, for the reply!

This helps me alot, I didnt know it was that easy to return something else then just true or false through bool methods!:)

But, do you know how I can do something like what I said above?


I understand now how to make two different methods, whit the location in Vector3, and the world name in string! but, how can I call them out like that?

Sep 19, 2012 at 12:18 PM winsjansen

getLocation() must return an object, and one its methods must be ToString() which is called implicitly when printing to the console. Another method on that object must be getWorld() which returns the name as a string (it's basically like ToString())

Sep 19, 2012 at 09:29 PM DaveA

By the way, you can set this up in the debugger, set a break point, then examine the variables to see what methods and properties they have, it's a good way to get familiar with things.

Sep 19, 2012 at 09:29 PM DaveA

It looks like you're after a class array (locations[thisLocation].locationName).

 class Locations {
     var locationName : String;
     var somethingElse : int;

 var locations : Locations[] = new Locations[amount];

Otherwise a separated function for looking up a name for a location would do the trick which returns the string for that contained area.

Sep 19, 2012 at 09:46 PM save
(comments are locked)
10|3000 characters needed characters left

The first call getLocation() returns an object. This object is used to call it's getWorld() method.

I think it is really good that you are teaching yourself scripting.

To learn more you could try googling for a javascript tutorial. Heres one http://www.w3schools.com/js/default.asp but I am not sure how good it is, especially in the context of unity.

more ▼

answered Sep 19, 2012 at 02:09 PM

avatar image

16 1 3 5

No, getLocation() do not return an object! Let me show you two different outputs in the console from the two different methods;

something.getLocation(); console says: 10.0 10.0 10.0! <-- Vector3!

something.getLocation().getWorld().getName(); console says: "awsome world"! <-- String, whit world name!

Sep 19, 2012 at 02:45 PM winsjansen
(comments are locked)
10|3000 characters needed characters left

Not sure about what you are seeing - I believe you - just not sure about Unity implementation.

In general, the technique is called method chaining. You often see it in other languages like Ruby. http://stackoverflow.com/questions/603499/javascript-object-method-chaining-useful

Perhaps getLocation() is returning an object, and the console is calling it's description method to show you the text.

more ▼

answered Sep 19, 2012 at 03:51 PM

avatar image

16 1 3 5

It was for java, so since I use C# I understand its abit different, but it should be possible?

Since I dont know how I am atm just making one method for everything, which ofc works perfectly, but it would be so much better to be able to just do it like I explained..

But, anyways thanks for trying to help! If you, or anyone else figure out more about it, please let me know more! :)

The methods btw, can be more then just 2, like;


Sep 19, 2012 at 04:33 PM winsjansen

Basically, OptimisticMonkey's been telling you what's going on. The getLocation returns a vector3 - this vector3 sticks around for a little bit in temporary memory. Then, we use that vector3 to call getWorld(), and it uses the vector3's data to determine what world it's in. Once that call finishes, we store that result in another temporary variable, then call getType() using that temporary variable.

The key to these chains is to return something useful to call methods on; if your function returns, say, a bool, there might not be a useful function to call. If instead it returns some custom object, say, a class holding data about your player, or the world, any methods in that custom class can be called on the result.

Your chain is nearly the same thing as doing: Vector3 tempVector = something.getLocation(); String tempName = tempVector.getWorld(); Type type = tempName.getType();

You're just saving yourself the work of these immediate steps.

Sep 19, 2012 at 05:09 PM Michael Covert

Thanks for the reply, I think I can understand alittle bit of what you are trying to teach me, but do you mind giving me an example for one method that could return different things like that?:)

Would have been nice, thanks!

Sep 19, 2012 at 07:41 PM winsjansen

Dave A.'s answer above provides an example of 2 methods that return various things in UnityScript. I'm unfamiliar with it, however from what I've heard/read, simply returning an object of that type defines the return type.

In C#, you declare this in your function declaration.

When you write something like,

public void MyFunction() { //function stuff in here } that 'void' word is where you determine what the function returns. If, for example, you changed it to this: public Vector3 MyFunction() { Vector3 someVector3; //stuff in here return someVector3; } we'll return a vector3. Void indicates no return type. You can return literally any class or struct you've written, and a few you haven't written (these are provided to you by the language and the Unity framework).

If you called this function, then the result would be a Vector3. This means that you can either store that vector3 yourself, or you can chain with any method that you could normally call on a Vector3. This makes a call like MyFunction().SomeFunction() legal syntax, as long as the Vector3 object supports a SomeFunction() call.

Does that help? If you want more detail, it'd be good to know what language you're trying to use.

Sep 19, 2012 at 08:57 PM Michael Covert

Thanks for the reply! :)

I use C#, and I am currently using methods like you explained whit the return of Vector3, or int/string and what not! It works fine, and for my own development of the game, I dont really need to make it more complicated. But, I want my game to be open source so people can create plugins and such to the game!

Thats why I want to be able to do this, just like "Bukkit for Minecraft"!

So, when you type "something.getLocation()" it will show up in the suggestion field stuff like "getWorld()" or, "getType"!

Sep 19, 2012 at 09:03 PM winsjansen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Sep 19, 2012 at 01:31 AM

Seen: 1198 times

Last Updated: Sep 20, 2012 at 05:24 PM