Page 1 of 1
General gadget question (keeping things synced)
Posted: 17 Jan 2010, 14:48
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?
Re: General gadget question (keeping things synced)
Posted: 17 Jan 2010, 14:51
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.
Re: General gadget question (keeping things synced)
Posted: 17 Jan 2010, 14:55
by Kloot
Re: General gadget question (keeping things synced)
Posted: 17 Jan 2010, 15:57
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.
Re: General gadget question (keeping things synced)
Posted: 17 Jan 2010, 15:59
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.
Re: General gadget question (keeping things synced)
Posted: 17 Jan 2010, 16:11
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.
Re: General gadget question (keeping things synced)
Posted: 17 Jan 2010, 17:32
by Master-Athmos
Allright - thanks for your answers!

Re: General gadget question (keeping things synced)
Posted: 18 Jan 2010, 09:45
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.