ambiera home ambiera home

Ambiera Forum

Discussions, Help and Support.

Create a new account, Search, or Login:
Name:  Password:    
Ambiera Forum > CopperCube > Programming and Scripting
Custom class return value bug
Author Text

xanimalkingx
Registered User
Quote
2018-12-02 01:40:04

I made a small vector2d class. When I print just the vector2d itself, I get a 3 point value despite the class only having a X and Y value.
My class has no Z value or field, so its impossible to return a third value. Its default constructor insures that.
For example:

var newVec = new vector2d(0.5, 0.5);
print(newVec);

Outputs:

0.5, 0.5, 0.0


Where as:

var newVec = new vector2d(0.5, 0.5);
print(newVec.x + ", " + newVec.y);

Outputs:

0.5, 0.5


Here is my class:

// vector2d class by xanimalkingx
// Holds 2 points which can be used for things that require 2D coordinates,
// such as texture coordinates.
// (must be defined at start of file)
vector2d = function(xVal, yVal) {
// Constructor
// Check for null
if (xVal == null) {
this.x = 0;
} else {
this.x = xVal;
}
if (yVal == null) {
this.y = 0;
} else {
this.y = yVal;
}

// Set both points to new values
this.set = function(newX, newY) {
this.x = newX;
this.y = newY;
}

// Set a new value for the x point
this.setX = function(newX) {
this.x = newX;
}

// Set a new value for the y point
this.setY = function(newY) {
this.y = newY;
}

// Clone this vector2d into a new variable
this.clone = function() {
return new vector2d(this.x, this.y);
}
}



just_in_case
Registered User
Quote
2018-12-02 07:05:39

Coppercube will always provide 3 cordinates even if you use vecotor2d... It will still provide you 3 cordinates... You may use ccbget2dposfrom3dpos...
If that helps...


xanimalkingx
Registered User
Quote
2018-12-02 16:24:25

You clearly don't know what I mean. My class is literally a custom made 2d vector class with ONLY 2 points so the default value SHOULD NOT return a 3 point value itself. This is a BUG.


erik
Registered User
Quote
2018-12-02 16:52:03

So, where is the definition of your class?


just_in_case
Registered User
Quote
2018-12-02 20:17:26

We can't see your custom defined class.... So can't say if it is a bug within coppercube or if there is some issue with your custom class?


xanimalkingx
Registered User
Quote
2018-12-03 01:24:24

I updated the post with my class.


niko
Moderator
Quote
2018-12-03 07:16:56

For having the correct output, you need to add something like this into your class:

this.toString = function() {
return this.x + ", "+ this.y;
}


then it should work.


xanimalkingx
Registered User
Quote
2018-12-03 07:23:36

wrote:
For having the correct output, you need to add something like this into your class:

this.toString = function() {
return this.x + ", "+ this.y;
}


then it should work.

That actually works... I didn't actually think to add a standard toString method to my class. Thank you niko!


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internat?onal" (you are not logged in)


Text:

 

  

Forum Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   





Software: Ambiera Light Forum. © 2008-2018 Nikolaus Gebhardt, Ambiera e.U.