Lua sync to unsync: Difference between revisions

From Spring
Jump to navigationJump to search
Flozi (talk | contribs)
→synced and unsynced lua: 97.0 ! changes to SYNCED
Maddox (talk | contribs)
m Minor fixes; Added proper SyntaxHighlight according to https://www.mediawiki.org/wiki/Extension:SyntaxHighlight
Line 8: Line 8:


If you want to draw on the screen (for example to mark units) then the drawing will have to happen in unsynced code.
If you want to draw on the screen (for example to mark units) then the drawing will have to happen in unsynced code.


The following example demonstrates how to change the value inside the synced block of code and read the value in the unsynced block of code:
The following example demonstrates how to change the value inside the synced block of code and read the value in the unsynced block of code:


 
<syntaxhighlight lang="lua">
 
<code><pre>
if (gadgetHandler:IsSyncedCode()) then
if (gadgetHandler:IsSyncedCode()) then
   --SYNCED CODE
   --SYNCED CODE
Line 28: Line 25:
   end
   end
end
end
</pre></code>
</syntaxhighlight>


== 97.0+ ==
== 97.0+ ==
As of 97.0 unsynced gets most of the [[Lua:Callins|callins]] available to synced (excepting those which control synced actions e.g. AllowUnitCreation etc) which means that forwarding data from synced to unsynced is now not always required. Additionally in 97.0 there were changes to the behaviour of the <code>SYNCED</code> table described above.
As of 97.0 unsynced gets most of the [[Lua:Callins|callins]] available to synced (excepting those which control synced actions e.g. AllowUnitCreation etc) which means that forwarding data from synced to unsynced is now not always required. Additionally in 97.0 there were changes to the behaviour of the <code>SYNCED</code> table described above.


Change 1: <code>SYNCED.</code> does a copy on access now and so is slow:
Change 1: <code>SYNCED</code> does a copy on access now and so is slow:<br /><br />
  - Bad:     
'''Bad:     
<code><pre>for i=1,100 do ... SYNCED.foo[i] ... end</pre></code>
<syntaxhighlight lang="lua">for i=1,100 do ... SYNCED.foo[i] ... end</syntaxhighlight>
  - Good:   
'''Good:   
<code><pre>local foo = SYNCED.foo; for i=1,100 do ... foo[i] ... end</pre></code>
<syntaxhighlight lang="lua">local foo = SYNCED.foo; for i=1,100 do ... foo[i] ... end</syntaxhighlight>
Change 2: <code>SYNCED.</code> can't be localized in global scope anymore:
Change 2: <code>SYNCED.</code> can't be localized in global scope anymore:<br /><br />
  - Bad:     
'''Bad:     
<code><pre>local foo = SYNCED.foo;    function gadget:DrawWorld() ... foo ... end</pre></code>
<syntaxhighlight lang="lua">local foo = SYNCED.foo;    function gadget:DrawWorld() ... foo ... end</syntaxhighlight>
  - Good:   
'''Good:   
<code><pre>function gadget:DrawWorld() local foo = SYNCED.foo; ... foo ... end</pre></code>
<syntaxhighlight lang="lua">function gadget:DrawWorld() local foo = SYNCED.foo; ... foo ... end</syntaxhighlight>


==Using special functions==
==Using special functions==

Revision as of 09:23, 9 May 2018

Synced -> Unsynced Communication

Using globals (96.0 and previous)

When making a gadget you will sometimes want to combine synced and unsynced code.

For example if you do some things with units, that code will need to be synced.

The UnitDestroyed, UnitFinished etc. callins can only be called in synced code. Using them inside unsynced code is not going to work.

If you want to draw on the screen (for example to mark units) then the drawing will have to happen in unsynced code.

The following example demonstrates how to change the value inside the synced block of code and read the value in the unsynced block of code:

<syntaxhighlight lang="lua"> if (gadgetHandler:IsSyncedCode()) then

  --SYNCED CODE
  local number = 10               -- declaring local variable 'number'
  function gadget:SomeSyncedCallin(...)
     number=number+1              -- increment variable's value by one
     _G.number=number             -- save the new value into a global variable
  end

else

  --UNSYNCED CODE
  function gadget:SomeUnsyncedCallin(...)
     Spring.Echo(SYNCED.number)   -- print the global variable's value on screen
  end

end </syntaxhighlight>

97.0+

As of 97.0 unsynced gets most of the callins available to synced (excepting those which control synced actions e.g. AllowUnitCreation etc) which means that forwarding data from synced to unsynced is now not always required. Additionally in 97.0 there were changes to the behaviour of the SYNCED table described above.

Change 1: SYNCED does a copy on access now and so is slow:

Bad: <syntaxhighlight lang="lua">for i=1,100 do ... SYNCED.foo[i] ... end</syntaxhighlight> Good: <syntaxhighlight lang="lua">local foo = SYNCED.foo; for i=1,100 do ... foo[i] ... end</syntaxhighlight> Change 2: SYNCED. can't be localized in global scope anymore:

Bad: <syntaxhighlight lang="lua">local foo = SYNCED.foo; function gadget:DrawWorld() ... foo ... end</syntaxhighlight> Good: <syntaxhighlight lang="lua">function gadget:DrawWorld() local foo = SYNCED.foo; ... foo ... end</syntaxhighlight>

Using special functions

Another way is to use the SendToUnsynced callin. Here is an example by quantum:

http://pastebin.com/hMHv3Lrf


[LCC]quantum[0K] every time there  is a new target, i send "addTarget" and its number to unsynced
[LCC]quantum[0K] when a target is removed from the list, i send "removeTarget" and its number
[LCC]quantum[0K] this wierd stuff:
[LCC]quantum[0K]   _G.laserEventArgs = eventArgs
[LCC]quantum[0K]   SendToUnsynced("LaserEvent")
[LCC]quantum[0K]   _G.laserEventArgs = nil
[LCC]quantum[0K] puts the eventArgs table somewhere were unsynced can find it
[LCC]quantum[0K] sends a signal to unsynced called "LaserEvent"
[LCC]quantum[0K] and removes the table
[LCC]quantum[0K] on the unsynced side, _G.laserEventArgs becomes SYNCED.laserEventArgs
[LCC]quantum[0K] this copies it to another table, ready for reuse:   for k, v in spairs(SYNCED.laserEventArgs) do
[LCC]quantum[0K]     eventArgs[k] = v
[LCC]quantum[0K]   end
[LCC]quantum[0K] also, this: gadgetHandler:AddSyncAction("LaserEvent", HandleLaserEvent)
[LCC]quantum[0K] means that i want the HandleLaserEvent function to be called every time i do SendToUnsynced("LaserEvent")