Thoughts, ideas and cool bits that fall from the minds of the Skyline Developers as we are working. Not a blog but fun facts or small news as we try new things.

Bitwise in Lua

Bitwise in Lua

Postby SolarPortal » 19 Mar 2019, 18:11

With the upgrade of lua from 5.1 to 5.3.5, we can finally use bitwise operators in lua which makes things like storing bitflags and bitmasks a breeze:

So like in c++, set your flags up like so.
Code: Select all
BIT_Walk = 1 << 0;
BIT_Swim = 1 << 1;
BIT_Door = 1 << 2;

then set the flags like so:
Code: Select all
polyflag=BIT_Walk | BIT_Door
polyflag=1<<0 | 1<<2

then you can use functions like this example from the navmesh editor being developed to add or remove the bitflag from the variable:

Code: Select all
function setAreaPolyFlag(_area, _flag, _state)
    if(_state == true)then
        props_area.AreaTypes[_area].polyflag = props_area.AreaTypes[_area].polyflag | _flag;
    elseif(_state == false)then
       props_area.AreaTypes[_area].polyflag = props_area.AreaTypes[_area].polyflag & ~_flag; 
    navmesh.areaTool.setAreaBuildPolyFlags(_area-1, props_area.AreaTypes[_area].polyflag)-- -1 to start from zero for c++

You can even do bit tests in lua:
Code: Select all
if(   (currentAreaProps.polyflag & BIT_Walk) == BIT_Walk)then

Just wanted to share the cool new way of coding in lua :P
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.

Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
Skyline Founder
Skyline Founder
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Return to SkyDev Corner

Who is online

Users browsing this forum: No registered users and 1 guest