x


Return statement in class function (Javascript)

I tried to translate a script from C# to UnityScript (javascript?). The C# code works perfect... but my "translation" throws this error:

"Cannot Convert 'PlayerDataClass' to 'void'"

Surely I made something wrong in the "translation" and I don't know what it is...

The original code in C#:

public class PlayerDataClass {
   public int networkPlayer;
   public string playerName;
   public int playerScore;
   public string playerTeam;

   public PlayerDataClass Constructor() {
      PlayerDataClass capture = new PlayerDataClass();
      capture.networkPlayer = networkPlayer;
      capture.playerName = playerName;
      capture.playerScore = playerSocre;
      capture.playerTeam = playerTeam;
      return capture;
   }
}

My UnityScript translation:

 class PlayerDataClass {
 var networkPlayer : int;
 var playerName : String;
 var playerScore : int;
 var playerTeam : String;

    function PlayerDataClass() {
       var capture : PlayerDataClass = new PlayerDataClass();
       capture.networkPlayer = networkPlayer;
       capture.playerName = playerName;
       capture.playerScore = playerScore;
       capture.playerTeam = playerTeam;
       return capture;
    }
 }

"return capture" is where the console tells me it's wrong.

Thanks in advance to anyone willing to help me.

more ▼

asked Aug 07 '12 at 10:36 PM

BLF-Games gravatar image

BLF-Games
123 1 3 12

Good you have that return at the end, otherwise it would probably compile and your applicatrion would crash since you call the constructor from within the constructor --> endless recursive function call --> stack overflow.

Aug 07 '12 at 11:22 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The constructor in a class can't return anything. The correct translation of

public PlayerDataClass Constructor() {

is

function Constructor() : PlayerDataClass {

In this case you have a function called "Constructor", but it's not actually a constructor. I'd recommend creating a proper constructor instead.

more ▼

answered Aug 07 '12 at 11:13 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

Yep, actually the function should be called something like CreateClone() since that's what it does.

An alternative would be to implement a copy-constructor which is a true constructor that takes a reference to an existing instance.

Or if you don't actually need it as class you could turn it into a struct, but i guess that's not what you want.

Aug 07 '12 at 11:19 PM Bunny83

So perfectly correct and precise as always Eric5h5! Thanks a lot! :D

Aug 07 '12 at 11:36 PM BLF-Games
(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

By RSS:

Answers

Answers and Comments

Topics:

x1
x1

asked: Aug 07 '12 at 10:36 PM

Seen: 217 times

Last Updated: Aug 07 '12 at 11:36 PM