2

Currently I'm trying to have two instances of an auction run simultaneously, but it seems when I'm creating a new object it's the exact same object as the one before it.

The Code:

Here is the code that creates the new object:

AuctionSell._state = new auctionGen();
AuctionSell._state.init(i);
auctions.push(AuctionSell._state);

To note, auctionGen() is a function that returns an object containing all the variables and logic surrounding the auction itself.

function auctionGen()
{
    return {
        //code
    }
}

^ this is just a blank template to show how the object is actually being created.

Updates the auctions array:

update : function(dt)
{
    if(auctions.length > 0)
    {
        for(var i = 0; i < auctions.length; ++i)
        {
            if(!auctions[i]._expired)
            {
                auctions[i].update(dt);
            }
            else
            {
                auctions.splice(i, 1);
            }
        }
    }
}

As you can see I create the new object then push it into an array that will contain my auctions. This happens in the auction's init function which only gets called when a new auction is to be created. I've checked the data of both auction states and they contain the different vehicles that are up for auction, however it appears as if creating the new one halts the old one.

Example:

If I run one auction alone - depending on the car - the final bid will always be higher than it's actual value. However, if I run these auctions simultaneously it is guaranteed to be under value.

I just ran two auctions simultaneously, the price of the first vehicle was $18000 but the Auction ended with a bid of $6370. The second car was valued at $57,000 and the Auction ended at $46428.

All of the bids are done by AI, and they are programmed to always stop bidding at a certain point above the value of the vehicle. This issue only appears when running two or more auctions at one time.

The Question

Is there another method in which I should be creating these objects to make them truly new objects? As of right now they seem to interfere with each other.

Edit:

After changing the update call to be the following:

update : function(dt)
{
    var i;
    if(auctions.length > 0)
    {
        i = 0;
        while(i < auctions.length)
        {
            if(!auctions[i]._expired)
            {
                auctions[i].update(dt);
                ++i;
            }
            else
            {
                auctions.splice(i, 1);
            }
        }
    }
}

and fixing my ai array to no longer be global, it seems as though the issue now only occurs on the second vehicle auction. The bidding for the $57,000 vehicle stopped at $17,198 where my $12,000 vehicle stopped at $23,947 (intended behaviour)

9
  • have you used something like firebug to see what exactly is inside of these objects? Commented Feb 9, 2015 at 14:40
  • 2
    Your new will almost certainly be unnecessary here, unless you use this inside your auctionGen() constructor, since you're returning from auctionGen(). Commented Feb 9, 2015 at 14:40
  • Does the object returned from auctionGen() happen to reference any variables? Commented Feb 9, 2015 at 14:41
  • function auctionGen(){} should return this after initialization Commented Feb 9, 2015 at 14:45
  • 1
    p.s... I hope these auctions that are ending early aren't real auctions ;). Commented Feb 9, 2015 at 15:01

2 Answers 2

5

You have a bug in your update code: If you remove an entry (your splice call), you don't want to increment i, it's making you skip entries — suppose you have entries 0, 1, and 2; when processing 1, you remove it; now the entry that used to be at 2 is at 1, so if you increment i, you'll skip it.

Instead, only increment i if you're not removing the entry:

update : function(dt)
{
    var i;
    if(auctions.length > 0)             // *** There's no reason to do this
    {
        i = 0;                          // *** Change on this line
        while (i < auctions.length)     // *** Change on this line
        {
            if(!auctions[i]._expired)
            {
                auctions[i].update(dt);
                ++i;                    // *** Note we increment `i`
            }
            else
            {
                auctions.splice(i, 1);  // *** Removing, so don't increment `i`
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for catching this, I've changed it now and that actually solved the issue that the second vehicle would be under bid, however it still happens for the first auctioned vehicle. Changing my ai array as it is global, which a comment on my post made me notice, once I fix that I will check back in.
Yeah, if you have a global array, it's not going to be instance-specific. :-)
Updated OP with results
0

You don't want to carelessly manipulate the container (the array in this case) as you walk over it. In C++ this would be called iterator invalidation, but the idea is the same here: i is getting messed up and pointing to the wrong data. Your splice call is causing your loop to skip some entries.

2 Comments

It's totally fine to remove entries as you move through the array, you just have to handle it properly, as I explained 10 minutes ago.
@T.J.Crowder I added the word "carelessly." I thoroughly enjoyed your explanation but felt it was worth it to draw the analogy to other languages and introduce extant terminology for this issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.