Moving values between arrays

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
MuNgLo
Posts: 316
Joined: 12 Jul 2012, 03:37

Moving values between arrays

Post by MuNgLo »

I have several Lists. I want to keep PlayerLogins in one list at a time only. So when I add a PlayerLogin to one list(array) I want to remove it from all others.
just using List.remove gives "this value will be discarded" while compiling. Still seems to work but it interupts the compiling and I figure it might make the script really slow if it discards after several attempts to get and remove value.

So what I need some help with is to move a login from a list to a different while making sure that list is the only list that contain the login.

Example:
List A Superman,Hulk,Finn,Gir
List B Flash,Spiderman,Bender
List C Dr_Doom,Alex

Now I want to move Finn from list A to list B
So I add Finn to list B
Remove Finn from list A and C since I don't check if the list contains Finn and just want to make sure not to get duplicate.

Which is the best way to solve it?
User avatar
xGEKKOx
Posts: 271
Joined: 18 Jul 2011, 21:04

Re: Moving values between arrays

Post by xGEKKOx »

if you are using php try:
array_pop
array_shift
TPS Gaseco - iPhone TM Remote Controller - GOS Coder
User avatar
steeffeen
Translator
Translator
Posts: 2472
Joined: 14 Oct 2012, 16:22
Location: Germany

Re: Moving values between arrays

Post by steeffeen »

MuNgLo wrote:just using List.remove gives "this value will be discarded" while compiling. Still seems to work but it interupts the compiling and I figure it might make the script really slow if it discards after several attempts to get and remove value.
try

Code: Select all

declare Temp = Array.remove(Login);
the message only shows that a value is thrown into the matrix without doing something with it ;)
(.remove() returns the removed value)
MuNgLo wrote:So what I need some help with is to move a login from a list to a different while making sure that list is the only list that contain the login.

Example:
List A Superman,Hulk,Finn,Gir
List B Flash,Spiderman,Bender
List C Dr_Doom,Alex

Now I want to move Finn from list A to list B
So I add Finn to list B
Remove Finn from list A and C since I don't check if the list contains Finn and just want to make sure not to get duplicate.

Which is the best way to solve it?
i would suggest a method like this

Code: Select all

Void AddToArray(Text _Login, Text[] _Array) {
	declare Temp = Array1.remove(_Login);
	Temp = Array2.remove(_Login);
	Temp = Array3.remove(_Login);
	_Array.add(_Login);
}
so you only need one line to make sure that the value is only in the intended array and in none other

i'm not sure if there will be an error if you try to remove a value which isn't in an array! so you might want to test the existence of the value (i think there won't be an error, instead there will be Null returned, but you better try it yourself ^^)

Code: Select all

if (Array.exists(Login)) {
	declare Temp = Array.remove(Login);
}
//Edit: 200th post! :yes:
Last edited by steeffeen on 04 Mar 2013, 12:46, edited 1 time in total.
    Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

    ManiaControl, FancyManiaLinks
    User avatar
    Eole
    Nadeo
    Nadeo
    Posts: 1265
    Joined: 26 Apr 2011, 21:08

    Re: Moving values between arrays

    Post by Eole »

    Removing a non-existing value shouldn't cause an error.
    Contribute to the ManiaPlanet documentation on GitHub
    A question about ManiaScript? Ask it here!
    MuNgLo
    Posts: 316
    Joined: 12 Jul 2012, 03:37

    Re: Moving values between arrays

    Post by MuNgLo »

    Thx guys.
    Post Reply

    Return to “ManiaScript”

    Who is online

    Users browsing this forum: No registered users and 2 guests