How to center sprite to middle of screen? (SOLVED)

I am making a top down 2D game, and I need to know how to take the width and length of the current screen and center the sprite with it. Thanks.

If you are scrolling the screen then you need to know the window size and the offset from the origin. You can get the window size from the render script, put it into a Lua module, and then get the offset based on how you are scrolling the screen. That’s what’s happening in this game’s demo Pixcade Cosmos

Here are some files from it for you to look at hopefully they help you.

camera.script (1.1 KB)

camera_offset_helper.lua (110 Bytes)

split_screen.render_script.script (3.9 KB)

camera_helper.script (5.5 KB)

camera_position_offset.script (745 Bytes)

3 Likes

Where can I go to learn more about Lua modules and sharing information between scripts in the game?

You create a new Lua module from the right click menu.

This is a “filename.lua” then in that file you add

local M = {}
return M

This is a blank Lua module.

You’ll need to set scripts to be shared state in your game.project file.

0 - 2D FOV Field of View

Now you can require this file in a “filename.script” file like

local lua_module = require("modules.filename") -- imagine the filename.lua is a folder called modules

You can require this file in multiple scripts and get/set values.

lua_module.name = "adrylain"
print(lua_module.name)

render_script files have access to different functions than normal script files so that’s why you have to pass information about the screen from the render_script to a Lua module then to a script. You can also send information with message passing but modules are better.

3 Likes

Thanks, that was REALLY helpful.

All gucci now. :ok_hand:

1 Like

There’s a whole section on Lua in the manual.