Return? How to do it?

Greetings,

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!

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?

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

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 JavaScript Tutorial but I am not sure how good it is, especially in the context of unity.

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.

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