Skip to content

Not deleting a tween is resulting in memory leaks and confusion between ids #29

@brunu97

Description

@brunu97

There are a lot of problems with Tweens in AGK, especially when using hundreds of them across levels or maps

However, I was unable to detect all of their origins, but I was able to detect some

This problem results in text letters changing position, despite being a sprite tween, it is controlling a tween as if it were text

Notice that the text "Continue" will inherit the tween properties of the sprite!
the solution is to delete tweens when switching screens, but this shouldn't even be happening
the text is not using any tween, it should not be possible to inherit tweens
and a probable memory leak will happen here

d33dfdf

Code Example

SetErrorMode(2)
SetWindowTitle("BugTest")
SetWindowSize(1280, 720, 0)
SetWindowAllowResize(1)
SetVirtualResolution(320, 180) 
SetOrientationAllowed(1, 1, 1, 1) 
SetSyncRate(0, 0) 
SetScissor(0,0,0,0) 
UseNewDefaultFonts(1) 

#CONSTANT    KEY_1            49
#CONSTANT    KEY_2            50

global screen as integer = 1

global spr as integer
global txt as integer

global _tweenChain as integer
global _tweens as integer[]

New_Screen1()

do
	if screen = 1
		Update_Screen1()
	endif
	
	if screen = 2
		Update_Screen2()
	endif
	
	if GetRawKeyPressed(KEY_1) and screen = 2
		Delete_Screen2()
		New_Screen1()
		screen = 1
	endif
	
	if GetRawKeyPressed(KEY_2) and screen = 1
		Delete_Screen1()
		New_Screen2()
		screen = 2
	endif
	

	UpdateAllTweens(GetFrameTime())
    Print(ScreenFPS())
    Sync()
loop


function newTween()
	_tweenChain = CreateTweenChain()

	tween1 = CreateTweenSprite(0.8)
	SetTweenSpriteY(tween1, GetSpriteY(spr), GetSpriteY(spr) + 10, TweenSmooth2())
	_tweens.insert(tween1)
	AddTweenChainSprite(_tweenChain, tween1, spr, 0)

	tween2 = CreateTweenSprite(0.8)
	SetTweenSpriteY(tween2, GetSpriteY(spr) + 10, GetSpriteY(spr), TweenSmooth2())
	_tweens.insert(tween2)
	AddTweenChainSprite(_tweenChain, tween2, spr, 0)
	PlayTweenChain(_tweenChain)
endfunction


function newSprite()
	spr = CreateSprite(0)
	SetSpriteSize(spr, 20, 20)
	SetSpriteOffset(spr, GetSpriteWidth(spr)/2, GetSpriteHeight(spr))
	SetSpritePositionByOffset(spr, 40, 40)
endfunction

function newText()
	txt = CreateText("Continue")
	SetTextSize(txt, 24)
	SetTextPosition(txt, 100, 90)
endfunction


function New_Screen1()
	newSprite()
	newTween()
	newText()
	SetClearColor(151, 170, 204)
endfunction

function New_Screen2()
	SetClearColor(0, 170, 204)
endfunction


function Delete_Screen1()
	DeleteSprite(spr)
	DeleteText(txt)
	// I'm not deleting the tweets on purpose!
endfunction

function Delete_Screen2()
	
endfunction


function Update_Screen1()
	Print("Screen 1")
	
	if GetTweenExists(_tweens[1])
		if not GetTweenChainPlaying(_tweenChain)
			PlayTweenChain(_tweenChain)
		endif
	endif
endfunction

function Update_Screen2()
	Print("Screen 2")
endfunction

And there is another problem that I have never been able to understand, when deleting a tween, and then creating another, it is possible that it will never be possible to create a tween again and the create tweens command will never return a tween again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions