General gadget question (keeping things synced)

General gadget question (keeping things synced)

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Master-Athmos
Posts: 916
Joined: 27 Jun 2009, 01:32

General gadget question (keeping things synced)

Post by Master-Athmos »

Hi!

I'm interested in writing some gadgets and would like to ask for some general info. What I'd like to know is what to keep in mind in order not to let things getting desynced...

Like let's say when you want to do Warcraft like per unit mana pools or an unique resource system like in Gundam RTS - what mustn't one do or use in order to keep those datas synced and the same for all players involved?

The Wiki and Spliff's guide gave me a rough idea but are there some no-goes or noob traps one should take care of?
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: General gadget question (keeping things synced)

Post by SpliFF »

It's simple. Don't do anything on one machine that affects world objects directly unless you're sure all other machines will do the same. The only way that can really happen is if you're passing data from widgets to gadgets synced to unsynced. If you're only using synced code you don't have anything (except possibly engine bugs) to worry about.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: General gadget question (keeping things synced)

Post by Kloot »

Yes, you do. See the last paragraph of http://springrts.com/wiki/Debugging_sync_errors.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: General gadget question (keeping things synced)

Post by Argh »

With table operations which entail deleting an element, there are operations you should write and delay until the next GameFrame: I use a simple routine (written by lurker, iirc) to do so:

Code: Select all

local delayedCalls = {}
function DelayedCall(fun)
	delayedCalls[#delayedCalls+1] = fun 
end


function gadget:GameFrame(f)
	if delayedCalls then
		for i=1,#delayedCalls do
			local fun = delayedCalls[i]
			fun()
		end
		delayedCalls = {}
	end
end
Where you simply put operations with in that function loop:

Code: Select all

DelayedCall(function()
--Do Stuff
end)
There are probably other examples of methods that are necessary to ensure synced operations, but that's one of the biggies, for things like building / destroying a table's elements to do some big operation, etc.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: General gadget question (keeping things synced)

Post by zwzsg »

Nowadays it's very hard to accidentally create sync error with Lua code.

Just go ahead and code without worrying about sync errors.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: General gadget question (keeping things synced)

Post by Argh »

+1 to that, in general.

You only start to see weirdness creep in when you're doing graphics stuff or really weird table stuff or communications between the synced / unsynced world. If you're just getting going here, don't spend a lot of time stressing about that; get your code working first.
Master-Athmos
Posts: 916
Joined: 27 Jun 2009, 01:32

Re: General gadget question (keeping things synced)

Post by Master-Athmos »

Allright - thanks for your answers! :-)
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: General gadget question (keeping things synced)

Post by imbaczek »

there's one thing in lua you need to be aware of - it's table iteration; if you iterate over pairs(), it can result in different order of iteration on different machines. trepan has a patch for that, which imho spring should include, but for now, don't do that.
Post Reply

Return to “Lua Scripts”