mp atomicity advice?
-
- Filter Inserter
- Posts: 549
- Joined: Fri Jan 29, 2016 2:48 am
- Contact:
mp atomicity advice?
Is there any official (or conventional) "best practice" I can follow if I want a mutex or lock of some kind, to muck around with the "global" global, or something hanging off of it, atomically?
For example (not my actual use-case) if I am making some kind of custom per-player money system, is there some safe procedure to debit player "a" and credit player "b" with minimal possibility of double-spending and/or vaporizing cash, assuming my virtual money is just an integer stored in global.player_wallet[player_index]?
For example (not my actual use-case) if I am making some kind of custom per-player money system, is there some safe procedure to debit player "a" and credit player "b" with minimal possibility of double-spending and/or vaporizing cash, assuming my virtual money is just an integer stored in global.player_wallet[player_index]?
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: mp atomicity advice?
I.e. no other mod can change your mods money integers. The "global" table is only global in the lua sense that it is not a local variable.Data Lifecycle on global access wrote:Each mod has its own Lua instance and own global table to store data.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
-
- Filter Inserter
- Posts: 549
- Joined: Fri Jan 29, 2016 2:48 am
- Contact:
Re: mp atomicity advice?
Yes, I know. I'm shooting for, "won't cause a desync in multiplayer," not "can't trample another mod's data".
Also, to be clear, I know all about the dining philosophers and all that stuff.... it's an API concurrency question, not a "how do I do concurrent programming correctly" question.
Also, to be clear, I know all about the dining philosophers and all that stuff.... it's an API concurrency question, not a "how do I do concurrent programming correctly" question.
Re: mp atomicity advice?
As a mod developer you don't need to concern yourself with concurrency, because control.lua is not a concurrent environment. All clients will execute all lua events in the same order, and lua events are not executed in parallel.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: mp atomicity advice?
Well. I have no clue how complex your "not your actual use-case" money-spending system is supposed to be. But causing desyncs isn't easy. As long as you don't do something stupid like storing persistent data in a local variable. Something simple as
certainly isn't going to cause any desyncs.
And your question is too vague to give sensible advice regarding what you're actually trying to do
Code: Select all
local function money_transfer(wallet1, wallet2, transfer)
wallet1 = wallet1+transfer
wallet2 = wallet2-transfer
end
And your question is too vague to give sensible advice regarding what you're actually trying to do

Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
-
- Filter Inserter
- Posts: 549
- Joined: Fri Jan 29, 2016 2:48 am
- Contact:
Re: mp atomicity advice?
OK, so, by whatever magical method, my mod's event-driven code effectively has a global lock on all shared game state including the global global?
For example, if player 1 and player 2 both press a GUI button to transfer all of their money to the other player at tick t, at tick t+1 one player will always end up with all the money? Can I assume anything about which player's code runs first?
For example, if player 1 and player 2 both press a GUI button to transfer all of their money to the other player at tick t, at tick t+1 one player will always end up with all the money? Can I assume anything about which player's code runs first?
Re: mp atomicity advice?
Both events will be called sequentially at tick t. When your mod is finished handling them, the player whose event was processed first will have all the money at tick t.golfmiketango wrote:For example, if player 1 and player 2 both press a GUI button to transfer all of their money to the other player at tick t
I don't know why you care which player's code runs first, someone has to win a tie but it doesn't matter who.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: mp atomicity advice?
I think the method would be called "event queue" or "single threaded execution". Everything happens in sequence. (Also as stated before the global "global" is not a shared table.)golfmiketango wrote:OK, so, by whatever magical method, my mod's event-driven code effectively has a global lock on all shared game state including the global global?
In the unlikely event they actually managed to press in the same tick. Yesgolfmiketango wrote:For example, if player 1 and player 2 both press a GUI button to transfer all of their money to the other player at tick t, at tick t+1 one player will always end up with all the money?
No. You do not even know if anyone else pressed any keys at all. As you get all events in order and each event only tells you about one player pressing a key. Of course you could store all the event data from tick t somewhere and then in tick t+1 look at the stored data to see if more than one player pressed a key. But i have no idea why you would want to do that.golfmiketango wrote:Can I assume anything about which player's code runs first?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: mp atomicity advice?
GUI click will create input action that is sent to server, server will put input actions it receives into sequence and sends then to clients, when client receives sequence of input action from server it exectutes them in series. In case of clicking on mod GUI executing input action means dispatching an event to Lua. I am not 100% sure, but I think server sorts input actions by player index.golfmiketango wrote:OK, so, by whatever magical method, my mod's event-driven code effectively has a global lock on all shared game state including the global global?
For example, if player 1 and player 2 both press a GUI button to transfer all of their money to the other player at tick t, at tick t+1 one player will always end up with all the money? Can I assume anything about which player's code runs first?
-
- Filter Inserter
- Posts: 549
- Joined: Fri Jan 29, 2016 2:48 am
- Contact:
Re: mp atomicity advice?
Groovy, thanks for the free science packs guys!
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: mp atomicity advice?
Oh. It actually resorts them instead just keeping the (random) order they were recieved in? Interesting.posila wrote:I am not 100% sure, but I think server sorts input actions by player index.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: mp atomicity advice?
Yes, all things must be deterministic. It makes automated tests that things are correct *much* easier if running the same inputs produces the same outputs every time. That's something we can verify on every platform we support.eradicator wrote:Oh. It actually resorts them instead just keeping the (random) order they were recieved in? Interesting.posila wrote:I am not 100% sure, but I think server sorts input actions by player index.
If you want to get ahold of me I'm almost always on Discord.