[solved] gadget:Initialize() - spring vs. spring-mt

[solved] gadget:Initialize() - spring vs. spring-mt

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

Moderator: Moderators

Post Reply
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

[solved] gadget:Initialize() - spring vs. spring-mt

Post by qray »

Hi,

I stumbled about a (at least to me) strange problem when playing around with a KotH gadget I tested in my local BA install. The gadget behaves differently depending on whether I use the spring or the spring-multithreaded binary:

In the function gadget:Initialize() in the synced part everything runs as expected when using the spring-multithreaded binary.
When I use the spring binary it seems that some parts are not executed:
For example a Spring.Echo command I put in the first line for "debugging" doesn't give a message output (and a second one later in the function neither). Also a SendToUnsynced(..) that is there isn't working.
Some other lines that set variables for the synced part and call a function there work normal with both binaries.

Has anybody a hint where this could come from?
... And maybe how to work around it? :-)

Thanks a lot in advance for any tip!

CU qray.
Last edited by qray on 15 Aug 2013, 18:22, edited 1 time in total.
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: gadget:Initialize() - spring vs. spring-multithreaded

Post by qray »

Hi again,

after getting the tip to look into the communcation between synced/unsynced I digged in the docu.
Although I do not fully understand the problem (still seems to me that my code should work in spring and if not then rather not in the MT version), I found a way around it.

So I mark this a solved..

Cheers, qray
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: [solved] gadget:Initialize() - spring vs. spring-mt

Post by smoth »

when you find a solution to your problem. Post the solution so others in the future know what you did.
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: [solved] gadget:Initialize() - spring vs. spring-mt

Post by qray »

Hi there.

In principle I agree. I didn't do it since the solution is a work around that to me seems quite ugly. Besides, the gadget is based on a script by Alchemist and I didn't want to post it here without checking with him first if he's OK with it (so far, I only used it in private games for testing, learning and playing around).

Nevertheless, here in a more or less short way:

In the synced part in gadget:Initialize() there is (besides some other stuff that works without problems):

Code: Select all

	function gadget:Initialize()
		goalTime = (Spring.GetModOptions().hilltime) * 30 * 60
		SendToUnsynced("initialTimekoth", goalTime / 30 )        
                ...
	end
This calls a function (which is hence called once at the beginning) in the unsynced part which should set the hilltime counters list to the initial hill time (they are counting down in the game):

Code: Select all

gadgetHandler:AddSyncAction("initialTimekoth", setTeamTimerskoth)
 ...
function setTeamTimerskoth(cmd, iTime, ...)
    for _, t in ipairs(Spring.GetAllyTeamList()) do
        teamTimers[t] = iTime
    end
end
The teamTimers list is then updated in the game (via another SendToUnsynced which works without problems) and used for writing the current times in a loop over the teams to the screen via gl.Text().

This works perfectly with spring MT but not with the normal spring. There, teamTimers is just not filled (a temporary Spring.Echo() in the setTeamTimerskoth function for debugging showed, that it is not called at all).

So, for now as a workaround I added before the output loop a check and repeat the function code:

Code: Select all

    if ( #teamTimers == 0 ) then
        local starttime = (Spring.GetModOptions().hilltime) * 60
        for _, t in ipairs(Spring.GetAllyTeamList()) do
            teamTimers[t] = starttime
        end			
    end 
As I said at the beginning: this is more a work-around than a solution. But I couldn't think of anything better for now(1), since I thought if there was a problem with the SendToUnsynced call in Initialize, it should have happened in the MT version and not in the normal one; although I wouldn't expect a problem in MT since AFAIK BA runs in ASIM mode 4 and there it should work... which it does.
So IMHO, there is probably not much to be learned from this work around... except for me if sbd now points out serious errors in my thinking :wink:

CU, qray

-----------------
(1) this is my first try to write/modify lua stuff for spring
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: [solved] gadget:Initialize() - spring vs. spring-mt

Post by zwzsg »

I would venture that Initialize is a bit too early, that synced is already doing its Initialize while unsynced hasn't yet run AddSyncAction.

So I would suggest moving your SendToUnsynced to GameStart instead.
User avatar
qray
Posts: 377
Joined: 02 Feb 2009, 18:49

Re: [solved] gadget:Initialize() - spring vs. spring-mt

Post by qray »

Hi zwzsg,

thanks for this idea.

You're right, if I put it in then it in gadget:GameStart() it works with both versions.
And this probaly explains why it works with the MT version, since I guess that this runs several things in parallel and by chance has already added the AddSyncAction.

I played around with it a bit more, since I liked it that the display is already there before the game starts. Therefore, I put the SendToUnsynced in gadget:GamePreload(). There it works with the normal spring, but not with springMT.
The only way to get what I planned in both versions is if I double the SendToUnsynced call and have it in Initialize and GamePreload. Still not 100% perfect, but much better than the hack I used before :-)

Thanks for you help!

CU qray.
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: [solved] gadget:Initialize() - spring vs. spring-mt

Post by zerver »

A very interesting find, thanks for reporting
Post Reply

Return to “Lua Scripts”