[SOLVED] Collectible Object Spawns On Player Which Increases Score More Than Once

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
AdonisMF
Prole
Posts: 3
Joined: Tue Jun 07, 2022 10:59 pm

Re: Collectible Object Spawns On Player Which Increases Score More Than Once

Post by AdonisMF »

ReFreezed wrote: Wed Jun 08, 2022 8:44 am When you choose a new position for the apple, do it in a loop that repeats until the new position isn't close to the player (i.e. brute force it), something like this:

Code: Select all

local function isCloseToPlayer(extraSpace)
    return (P.y + P.h > T.y - extraSpace)
       and (P.y < T.y + T.h + extraSpace)
       and (P.x + P.w > T.x - extraSpace)
       and (P.x < T.x + T.w + extraSpace)
end

function T:CheckOverlap()
    if isCloseToPlayer(0) then
        -- math.randomseed(os.time()) -- Do this only once, in love.load instead!
        Score = Score + 1
        repeat
            T.x = math.random(love.graphics:getWidth())
            T.y = math.random(love.graphics:getHeight()) 
        until not isCloseToPlayer(100) -- 100 is how close the apple and the player can be.
    end
end
Also move math.randomseed, as the others have commented on. The reason the score goes up by a lot is because math.randomseed makes math.random return the same values for up to a second, because os.time returns whole seconds (i.e. it returns the same value for a second).
The loop seemed to have fixed it! I also did change the math.random() to love.math.random() as the others suggested. Thank you all for the help :)
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Collectible Object Spawns On Player Which Increases Score More Than Once

Post by pgimeno »

The real problem was using the same seed over and over during one second, as togFox first noted. The loop just makes it never spawn at the player; it's a design decision whether that's desirable or not - it seems to me that your problem was with the abnormal score increase and not with the fact that it was spawned at the player, but I may be wrong. Using love.math.setRandomSeed(os.time()) would have led to the very same outcome when using love.math.random() instead of math.random().

On the other hand, LuaJIT's default random number generator is better than Löve's built-in one for simple use cases like this one, where you don't have to care about e.g. producing independently reproducible sequences. The main advantage is that it accepts seeding with an arbitrary float, taking into account all the decimals, while love.math.setRandomSeed() only accepts 32-bit integers. You can use Löve's love.timer.getTime() for seeding it with microsecond precision; in this case you would not have gotten the same number multiple times in consecutive frames if you had used love.timer.getTime() instead of os.time(), because from one frame to the next you could be sure that the seed had changed.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: [SOLVED] Collectible Object Spawns On Player Which Increases Score More Than Once

Post by darkfrei »

Why not my solution? It gives any random position except near the player without the loop.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests