[Close me] Dynamic Array Functions not working

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
Post Reply
User avatar
Major Cooke
Posts: 8176
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

[Close me] Dynamic Array Functions not working

Post by Major Cooke »

I've been trying to work with the Move and Copy functions but neither of them seem like they're actually working for some reason.

Attached is a sample file.

Here's the code behind the add function used from within D4BlackList.

Code: Select all

//--------------------------------------------------------------------------
// Adds a monster to the black list
void Add(Name mon, bool parent = false)
{
    // Make sure it's an actual thing that can be added, as in an actual class
    // that's an actor.
    Class<Actor> check = mon;
    if (!check)    
    {
        String thing = mon;
        Console.Printf("Bad actor %s", thing);
        return;
    }
    
    Array<String> Test;
    if (parent)        Test.Copy(PList);
    else            Test.Copy(List);
    
    String thing = mon;
    
    if (Test.Size() > 0)
    {
        //If it's already in, do nothing.
        for (int i = 0; i < Test.Size(); i++)
        {
            if (mon == Test[i])
            {
                Console.Printf("%s already in", thing);
                return;
            }
        }
    }
    
    Test.Push(mon);
    Console.Printf("Added %s", thing);
    
    if (parent)        Test.Move(PList);
    else            Test.Move(List);
} 
The expected result of performing "netevent BL_Add_LostSoul" twice should be

Code: Select all

Added LostSoul
LostSoul already in
But the arrays are completely empty -- both PList and List. Thus is just says "Added LostSoul" every time I try.

If I change it to cycle through (P)List directly, however, it'll work:

Code: Select all

//--------------------------------------------------------------------------
// Adds a monster to the black list
void Add(Name mon, bool parent = false)
{
	// Make sure it's an actual thing that can be added, as in an actual class
	// that's an actor.
	Class<Actor> check = mon;
	if (!check)	
	{
		String thing = mon;
		Console.Printf("Bad actor %s", thing);
		return;
	}
	
	String thing = mon;
	if (parent)
	{
		if (PList.Size() > 0)
		{
			//If it's already in, do nothing.
			for (int i = 0; i < PList.Size(); i++)
			{
				if (mon == PList[i])
				{
					Console.Printf("%s already in", thing);
					return;
				}
			}
		}
		PList.Push(mon);
	}
	else
	{
		if (List.Size() > 0)
		{
			//If it's already in, do nothing.
			for (int i = 0; i < List.Size(); i++)
			{
				if (mon == List[i])
				{
					Console.Printf("%s already in", thing);
					return;
				}
			}
		}
		List.Push(mon);
	}
}
Attachments
test.pk3
Start a new game and type in "netevent BL_Add_LostSoul" twice.
(1.66 KiB) Downloaded 20 times
Last edited by Major Cooke on Fri Jun 09, 2017 6:46 am, edited 1 time in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49073
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Dynamic Array Functions not working

Post by Graf Zahl »

You got the order wrong.

It's 'dest.Copy(src)', not 'src.Copy(dest)'.
User avatar
Major Cooke
Posts: 8176
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Dynamic Array Functions not working

Post by Major Cooke »

What about move?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49073
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Dynamic Array Functions not working

Post by Graf Zahl »

Same
User avatar
Major Cooke
Posts: 8176
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Dynamic Array Functions not working

Post by Major Cooke »

Tried changing them around, tested them all one at a time. Doesn't work still.

Code: Select all

void Add(Name mon, bool parent = false)
{
	// Make sure it's an actual thing that can be added, as in an actual class
	// that's an actor.
	Class<Actor> check = mon;
	if (!check)	
	{
		String thing = mon;
		Console.Printf("Bad actor %s", thing);
		return;
	}
	
	Array<String> Test;
	if (parent)		PList.Copy(Test);
	else			List.Copy(Test);
	
	String thing = mon;
	
	if (Test.Size() > 0)
	{
		//If it's already in, do nothing.
		for (int i = 0; i < Test.Size(); i++)
		{
			if (mon == Test[i])
			{
				Console.Printf("%s already in", thing);
				return;
			}
		}
	}
	
	Test.Push(mon);
	Console.Printf("Added %s", thing);
	
	if (parent)		PList.Move(Test);
	else			List.Move(Test);
	
}
User avatar
AFADoomer
Posts: 1326
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: Dynamic Array Functions not working

Post by AFADoomer »

I thought you had the Copy calls right already - You *want* to copy to Test from either PList or List, right, not the other way around?

Then at the end you want to move Test to PList or List? So your second example's Move calls are right, I think.


Out of curiosity, why aren't you using Test.Find(mon) instead if iterating over the array? If 'mon' is not found, Test.Find(mon) will return Test.Size()...
User avatar
Major Cooke
Posts: 8176
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Dynamic Array Functions not working

Post by Major Cooke »

AFADoomer wrote:I thought you had the Copy calls right already - You *want* to copy to Test from either PList or List, right, not the other way around?
Correct. But see what Graf has to say.
AFADoomer wrote:Then at the end you want to move Test to PList or List? So your second example's Move calls are right, I think.
Also correct.

I swear, they should be renamed to CopyFrom/To. This is too confusing.
AFADoomer wrote:Out of curiosity, why aren't you using Test.Find(mon) instead if iterating over the array? If 'mon' is not found, Test.Find(mon) will return Test.Size()...
I couldn't ever get Find() to work no matter what I tried, so I gave up on it and I do it manually from now on. Mainly because, since it returns the size, it means you can't use it for array indexes or the VM will abort.
User avatar
AFADoomer
Posts: 1326
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: Dynamic Array Functions not working

Post by AFADoomer »

It returns the index if it's found - you just have to compare it to Size, and if the return value is equal to Size, the thing wasn't found.

I've used this with no problem as part of a player class that populates an array of things within a certain range and FOV with certain flags:

Code: Select all

			if (ScannableThings.Find(mo) == ScannableThings.Size()) // If it's not there
			{
				ScannableThings.Push(mo); // Push it to the array
			}
EDIT:

Graf said it should be dest.Copy(src), which is what you had for the original part right after you declared the Test Array.
User avatar
Major Cooke
Posts: 8176
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Dynamic Array Functions not working

Post by Major Cooke »

So first part should be

Code: Select all

if (parent)		Test.Copy(PList);
else			Test.Copy(List);
second should be

Code: Select all

if (parent)		PList.Move(Test);
else			List.Move(Test);
Right?
User avatar
AFADoomer
Posts: 1326
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: Dynamic Array Functions not working

Post by AFADoomer »

That looks right, yes.

Granted, now I'm second-guessing myself...
User avatar
Major Cooke
Posts: 8176
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Dynamic Array Functions not working

Post by Major Cooke »

Huh. That actually did the trick.

Big thanks, man!
Post Reply

Return to “Closed Bugs [GZDoom]”