byebye widget go go gadget

byebye widget go go gadget

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

Moderator: Moderators

Post Reply
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

byebye widget go go gadget

Post by knorke »

I want to make a widget that marks puts all "miner units" in a table indexed by unitID.
Whenever a miner dies or is created, the table is updated.
Then I draw some stuff around the unit, similiar to teamplatter.
And later the miners should mine by killing solars but that is only the next step.

For testing I started with a widget, "arm_flash" is the miner unit and the results looks like this:
Image
Flashes are marked with this diamond/red triangle thing, other units not. Cool, it works.

How do I transform it into a gadget?
If i simpy replace all widget: with gadget: that does not work complety.
New build flashes are ignored also it seems that UnitFinished() is called. Maybe it is a problem with unitIDs?
When doing /luarules reload the make_miner_table() works and gets all flashs. Even if I put make_miner_table() in UnitFinished() it does not work. wtf?

I made a test gagdet that just counts units and that seems to work: http://pastebin.ca/1954798

I suppose I must use some

Code: Select all

if (gadgetHandler:IsSyncedCode()) then
here go all the callins?
else
here goes the Drawing stuff?
end
construct but I could not get it working.

Here is the widget:
http://pastebin.ca/1954785
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: byebye widget go go gadget

Post by zwzsg »

Yeah, indeed the UnitFinished, UnitCreated, etc.. callins exist only in the synced of the gadget, while the DrawWorld, etc... callins are only called in the unsynced of the gadget.

So yeah, you need to use some synced->unsynced communication stuff.

And yeah, it is a bit painful to have to go through that just to work around the lack of UnitX callins in unsynced gadgets.

The easy way is to use _G.MyData and SYNCED.MyData, see http://pastebin.ca/1954857 for exemple.

The slightly less easy way is to use SendToUnsynced(name,...) and gadgetHandler:AddSyncAction(name,func)
See Kernel_Panic_4.1.sd7 \LuaRules\Gadgets\set_alpha_threshold.lua for exemple.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: byebye widget go go gadget

Post by knorke »

The easy way is to use _G.MyData and SYNCED.MyData, see http://pastebin.ca/1954857 for exemple.
this seems to work and looks simple enough that I will probally do it like this.
The _G thing is strange though, didnt see that in wiki and how would one guess its done this way?
The slightly less easy way is to use SendToUnsynced(name,...) and gadgetHandler:AddSyncAction(name,func)
See Kernel_Panic_4.1.sd7 \LuaRules\Gadgets\set_alpha_threshold.lua for exemple.
this would be similiar to this i assume: http://pastebin.com/hMHv3Lrf (by quantum)
just in case someone finds this and wants to look at this way.
Andrej
Posts: 176
Joined: 13 Aug 2006, 18:55

Re: byebye widget go go gadget

Post by Andrej »

Here is my version:
http://pastebin.ca/1954880
tested with BA

Some stuff that tripped me over and i hope will save you THEPAIN:

The "#" table length operator only works when you have an array like:
x[1] = "aaaa";
x[2] = "bbbb";
x[2] = "cccc";
If you index by strings or nonsequential numbers (your miners table is indexed by unitIDs) or have nil values inbetween it will fail.

EDITED PART:
In unsynced to iterate through a table from synced like "SYNCED.miners" in a for loop you need to use "spairs" insted of "pairs" this took me like 30 min to realize i hate spring sometime

PS IT HELPS US TO READ YOUR CODE IF YOU INDENT IT REALLY
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: byebye widget go go gadget

Post by knorke »

Image

and YES i too hate the ipairs, pairs #bla, table.getn stuff.
PS IT HELPS US TO READ YOUR CODE IF YOU INDENT IT REALLY
yes i know, sorry. formating got kinda lost when i tried to get it working also i removed all kind of stuff that was not needed for the basic problem. even though some stuff remained i guess.

http://spring.pastebin.com/vmZkyRH9
in this version, miners fill their "cargo bay" when they destroy ressources. (kill solars to grow the yellow triangle next to the miners)

edit:
put all the choatic stuff in the wiki, may satan sort it out:
http://springrts.com/wiki/Lua_sync_to_unsync
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: byebye widget go go gadget

Post by zwzsg »

The "#" table length operator only works when you have an array like:
x[1] = "aaaa";
x[2] = "bbbb";
x[2] = "cccc";
If you index by strings or nonsequential numbers (your miners table is indexed by unitIDs) or have nil values inbetween it will fail.
Yes, but this is not what I meant.

When you do such an array (indexed by integer from 1 to N without hole), it works well, as long as you keep that array within synced, or within unsynced.

However, when that array is sent through _G. and received at SYNCED., somehow, for unclear reason, the # and ipairs magically stop working. This is what I wanted to stress.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: byebye widget go go gadget

Post by jK »

Because SYNCED just contains metatables (for syncing reasons), the standard table operators don't work on those. That's why there are spairs() & sipairs() (Spring API, not Lua official functions!).
Post Reply

Return to “Lua Scripts”