Loaded A PNG Image in Lua Script File - How To Display PNG Image In Same Lua Script File?

Hi,

I loaded a PNG image in a Lua script file.
How do I display that PNG image in the same Lua script file?
Let me know, thanks!

SS

function init(self)
	-- Add initialization code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
	local file = io.open("main/assets/images/BG_Brick_Wall.png", "rb")
	local bytes = file:read("*a")
	local png = image.load(bytes)
end

You can create an atlas at runtime and use it on a Sprite component:

Also this:

1 Like

Note that this is reading the image file from your project source folder and won’t work when you bundle your game.

You can add /main/assets/images/BG_Brick_Wall.png to the Custom Resources field in the project settings to tell Defold to include it in the final build, then use sys.load_resource("/main/assets/images/BG_Brick_Wall.png") instead of those IO functions. See the manual page on working with files for more details:

4 Likes