AlfonsAberg

This is probably an easy question, how do I assign to a Vector2
This was my first bad guess:

Game1.PlayersIdea.Position = (0f,0f);

but it didn't work, I know I can assign to the .X and .Y of the Vector2 but I want to do it in one simple step. Also can you do it without creating a new Vector 2

Cheers
Alfons


Re: XNA Game Studio Express Assign to Vector2?

pitil

Do you try : position = new vector2(X,Y);





Re: XNA Game Studio Express Assign to Vector2?

AlfonsAberg

Yes, I know that possibility but can it be done in one line without creating a new Vector




Re: XNA Game Studio Express Assign to Vector2?

Jim Perry

No, just do what's been suggested. Is there a particular reason why you don't want to do it like this




Re: XNA Game Studio Express Assign to Vector2?

AlfonsAberg

Because what I heard it's bad to create unnecessary new objects when running the solution on the Xbox360. And I want to do it in one line instead of:

Game1.PlayersIdea.position.X = 0f;
Game1.PlayersIdea.position.Y = 0f;

But I guess I can always create a method in my Player object.





Re: XNA Game Studio Express Assign to Vector2?

Jim Perry

Initially you have to create it, otherwise you'll get an error. If you're talking about updating it after that you could use a method to the Player object that takes two floats. If the position is something that's going to be updated constantly you could keep a Vector2 member of the Player that is used to update the position and just change that:

Vector2 _position;
Vector2 _move; //say this has X=5/Y=2

public void UpdatePosition()
{
    _position += _move;
}

public void ModifyMovement(float x, float y)
{
    _move.X = x;
    _move.Y = y;
}

 






Re: XNA Game Studio Express Assign to Vector2?

AlfonsAberg

Yes, the player object is already created, and position is a Vector2 field in that object.

Why can't you just assign a Vector2 with 2 floats

and this is what my method would look like, seems like an unnecessary step:

private Vector2 _position;

public void ModifyPosition(float x, float y)
{
_position.X = x;
_position.Y = y;
}




Re: XNA Game Studio Express Assign to Vector2?

Jim Perry

AlfonsAberg wrote:
Why can't you just assign a Vector2 with 2 floats

Because the structure doesn't allow it It would be overkill for every object to allow assignment by every permutation possible. You're talking about 1 extra line of code.






Re: XNA Game Studio Express Assign to Vector2?

Richard Kain

AlfonsAberg wrote:
Yes, the player object is already created, and position is a Vector2 field in that object.

Why can't you just assign a Vector2 with 2 floats

and this is what my method would look like, seems like an unnecessary step:

private Vector2 _position;

public void ModifyPosition(float x, float y)
{
_position.X = x;
_position.Y = y;
}

A "Vector2" is an object, yes Since you have to use "new" to create one, I just assume that it qualifies as an object. If Vector2 is an object, it really isn't possible to assign an object to another object. After all, objects are instances of classes. They don't actually exist until you create them. So the only real way to change the values of your Vector2 in the fashion you desire is with another created instance of Vector2. If you substantiated ANOTHER Vector2 earlier, you could probably use that Vector2 to change the value of your Vector2 exactly as you described. Of course, even then you would have to change the values of your additional Vector2 manually.

Defined types are a fundamental of programming. And you usually cannot assign a type of one kind to a type of a different kind. At least, that's what I've gathered from my limited experience.






Re: XNA Game Studio Express Assign to Vector2?

Shawn Hargreaves - MSFT

You can't assign two floats to a vector, because what does that even really mean .NET doesn't have any type that represents "two floats", so how would you write such a thing

You could have an array of two floats, or a structure containing two floats (which is exactly what Vector2 is) but you can't just have two floats floating freely around without some kind of type to gather them together. That's just not how it works.





Re: XNA Game Studio Express Assign to Vector2?

AlfonsAberg

Shawn, I think that my language skills might be to my disadvantage(and my lack of experience in programming). What I meant with "2 floats" was two values of the type float, or in other words, is there a way/method to change both the X and Y component in one line

/Alfons from Sweden







Re: XNA Game Studio Express Assign to Vector2?

Shawn Hargreaves - MSFT

No.

Assignment can only ever assign one thing to one other thing at a time. You write:

a = b;

If you want to assign multiple things, you have to either use two statements:

a1 = b;
a2 = c;

Or you could assign a single wrapper object that contains both b and c:

a = new WrapperObject(b, c);

There isn't really any other option...





Re: XNA Game Studio Express Assign to Vector2?

AlfonsAberg

Thanks for clearing that out for me.






Re: XNA Game Studio Express Assign to Vector2?

pkazaria

Is there a performance difference between using...

position.X = X;
position.Y = Y;

...and...

position = new Vector2(X, Y);

...

There shouldn't be any difference in allocation on the heap because Vector2 is a struct and structs are allocated on the stack (so no GC problem with the Xbox360). But is the compiler able to allocate directly to 'position' rather than a temporary stack variable





Re: XNA Game Studio Express Assign to Vector2?

waheyluggage

You could
position.X = x; position.Y = y;
Technically it's two lines but it would be on one visual line.