Favorite way to store next loop state?

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.
Post Reply
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Favorite way to store next loop state?

Post by parallax7d »

Lets say I have many components sharing state (a block of memory or a big table or whatever, it doesn't matter) and doing work to setup the values for the next loop. I don't know where to put these calculated values though.

Lets say all the data is stored in a big table called 'current'. I can't let my components do their work and write back to 'current' because other components (they all act independently) potentially need access to that original data for their own work.

Should I create a whole new table and let each component write their results to it? A table called 'next'? And each loop rename 'next' to 'current'?

Or should the 'current' data and 'next' data be written in the same table like [[xpos_1, xpos_2], [ypos_1, ypos_2]] and just have all functions know if they should be using field 1 or field 2 during this loop?

Just interested in the pros and cons of different methods you folks commonly use.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Favorit way to store current loop state and next loop st

Post by undef »

I would use 2 tables and one reference that is changing:

Code: Select all

local table
local table1 = { a, b, c }
local table2 = { d, e, f }

local function doStuffWithTable( t ) -- call with table
<...>
end

local function updateState( tableNum )
    if tableNum == 1 then
        table = table1
    else
        table = table2
    end
end
That way you only switch out the table you are performing computations on.
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: Favorite way to store next loop state?

Post by parallax7d »

I was thinking about that, but if the condition results change literally every frame, it ends up being option a roughly 50% of the time, and option b 50% of the time. Won't that royally screw up any sort of branch prediction the cpu is trying to do? Is there a way to toggle the tables without using a condition? How about changing the names of the tables right before the next frame, that way the components never know about the change, and no if is needed?
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Favorite way to store next loop state?

Post by undef »

Oh, I didn't understand that you wanted to change the state every frame...
In that case a counter would probably work.
Just increment a counter and use modulo to keep it in a certain range.
If you have only two states you would do something like if counter%2==0 then else end.

Switching out tables and functions is useful if you go from the main menu into the game, or from the game to the pause menu or something.
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Favorite way to store next loop state?

Post by Robin »

parallax7d wrote:Won't that royally screw up any sort of branch prediction the cpu is trying to do?
That line of thinking seems to me like a massive case of premature optimization.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 70 guests