Mod Question Repository... Questions come in, answers go out - Page 57

Mod Question Repository... Questions come in, answers go out

Resources to get you going on your new project, or to help you over some emergent problems during your development cycle.

Moderator: Moderators

Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Mod Question Repository... Questions come in, answers go out

Post by Tobi »

zwzsg wrote:The name of the signal doesn't matter at all. However it's very important that each signal is a power of 2 distinct from other signals (Do you speak binary? Ever worked with bitfields?)

Code: Select all

local function my_script()
    SetSignalMask( a_custom_signal_to_kill_the_myscript_tread  )
    Signal( a_custom_signal_to_kill_the_myscript_tread  )
    Spring.Echo('Executing my script')
end
You inverted Signal and SetSignalMask. You want to kill previous instance of my_script, not yourself! And don't forget to assign some 2^n number to a_custom_signal_to_kill_the_myscript_tread.

Then what I'm saying is from Cob, I assume it's the same for Lua unit scripts but I never tried.
That works, but in recent versions of Spring you can also use any Lua object for your signals. That way you can simply set each signal to a unique empty table object, allowing for an unlimited amount of signals (instead of only 24).

Essentially if you use this you can see SetSignalMask as setting the name of the thread and Signal stopping all the threads with the given name.
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

I've been using CarRepairs Bos to Lua coverter, to see if i could make some of my own models work on a Lua script.
My aim was to make a nanotower so i converted such a script and to see if it would result in anything useful (for a noob like me).
Although i don't understand most of it, I made some changes and hope it will work.

Now my problem:
Bos...

Code: Select all

while( Static_Var_2 != currentstate )
...converts into Lua

Code: Select all

while  Static_Var_2 not = currentstate  do
But this line doesn't seem to be valid.
I now think that if i fix this line, the script might just work. But I can't figure what it should be. What should this line look line in Lua?
(A reference to what it actually might do, and how that should be noted down, will also be appreciated.)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Mod Question Repository... Questions come in, answers go out

Post by zwzsg »

Replace

Code: Select all

not =
by

Code: Select all

~=
Also tell CarRepairer to update his Bos2Lua
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

Oh, i tried that. like this:

Code: Select all

while  (Static_Var_3 ~= currentstate) do
Guess the problem lies somewhere else.
Tanks anyway.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Mod Question Repository... Questions come in, answers go out

Post by zwzsg »

FireStorm wrote:A reference to what it actually might do, and how that should be noted down, will also be appreciated.
In C and Bos:

Code: Select all

while(a!=b)
{
	blah;
}

In Lua:

Code: Select all

while a~=b do
	blah
end
Where blah can be a big bunch of code several lines long.

If a and b are equal, this does nothing. If a and b are different, this execute blah, over and over and over, until a and b become equal. You'd better make sure there's something in blah that change a and b, and eventually make them equal, or else you'll be stuck in an infinite loop and your Spring application will freeze. Or another way is to exit the loop with a break or a return.



FireStorm wrote:Guess the problem lies somewhere else.
Maybe you have an error message? Read it.
For Bos/Cob Scriptor will tell you at least the first line that had an error.
For Lua, check your infolog.txt
Well, unless it's an infinite loop problem. Then you won't have a nice error message, just Spring freezing. And recently was added the hang detection thing, which will fill your infolog with unreadable dump in that case.

Either way, describe the symptom, post the whole script, post infolog.
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

the elaborate feedback is greatly appreciated, although in my script I am not (yet) bothered by complicated logic, because i haven't really touched that after conversion, afaik.
I think my current problems are still more in the area of understanding the syntax and Spring-specific things.

Spring now does accept my script; it shows some of the animations in it.
But the model still doesn't act as a nano (and infolog does not reference to any errors). Perhaps something in my animation prevents the copied and converted part to work.

But a more obvious thing i can't get right is the Reclaim-button showing up in the UI. I think i added all the relevant tags to the unitDef-file, so i was wondering:
Does the script has to work properly to make the button show up in game?
Or perhaps my script doesn't work because of the (or a different) reason it doesn't show?
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Mod Question Repository... Questions come in, answers go out

Post by CarRepairer »

FireStorm_ wrote:I've been using CarRepairs Bos to Lua coverter, to see if i could make some of my own models work on a Lua script.
My aim was to make a nanotower so i converted such a script and to see if it would result in anything useful (for a noob like me).
Although i don't understand most of it, I made some changes and hope it will work.

Now my problem:
Bos...

Code: Select all

while( Static_Var_2 != currentstate )
...converts into Lua

Code: Select all

while  Static_Var_2 not = currentstate  do
But this line doesn't seem to be valid.
Fixed. It should now convert != to ~=. In the future please PM me if you know a certain line of bos is not converting properly (I see you aren't quite familiar with lua so I understand that in this case you wouldn't have known).
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

In the future please PM me if you know a certain line of bos is not converting properly
If I encounter and recognise, i will.
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

I feel stuck again so i feel inclined to ask some potentially embarrassing and nooby questions:

How do i make my unit unpack into to its operating position after it is finished being build?

Code: Select all

local function open_up()
     -- here are some callouts that make for an unpacking animation
     -- seems to works fine btw
end

function script.Create(unitID)
	open_up()
end
This code makes my unit unpack while it is still being build.
How can/should it change it to accomplish the desired behaviour?
(Is there perhaps some sort of internal CreateFinished Callin?)

(Of course i have more trouble, but one step at the time. :-) )
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Mod Question Repository... Questions come in, answers go out

Post by FLOZi »

Traditionally in BOS this is done with a while loop and

Code: Select all

get BUILD_PERCENT_LEFT
So you can either use

Code: Select all

Spring.UnitScript.GetUnitValue(COB.BUILD_PERCENT_LEFT)

or, preferably use the LuaRules API and use

Code: Select all

select(5, Spring.GetUnitHealth(unitID))
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

Time and effort for feedback is always appreciated but, I think i'll have to embarrass myself some more, because i really want my (future) models to work.

I have no real programming background, I look a Lua tutorials and i think i understand them. But it doesn't teach me what it exactly should look like in a Spring-lua-unit-script.

I know that if i write this down...

Code: Select all

function script.Create(unitID)
   open_up()
end
...it starts when the unit (finished being build or not) turns op in game, an then this open_up stuff happens. This creation of a unit and other callins are clear triggers to me. But otherwise i still have to learn.

I understand that if i check if there is build percent left, the unit is not finished, and i can use that. But how? How do i translate that into a condition?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Mod Question Repository... Questions come in, answers go out

Post by FLOZi »

You need to use a while loop and the Sleep() function e.g.

Code: Select all

function script.Create(unitID)
  while (select(5, Spring.GetUnitHealth(unitID)) ~= 1) do 
  -- I don't recall if this is 1 or 0
    Sleep(500)
  end
  open_up()
end
That will check every 500ms if the unit is fully built and do nothing until it is.
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

I first copied from your reply but that didn't work. I tried a lot of combinations that i though might make sense, but no luck.
Then i saw "GetUnitValue(COB.BUILD_PERCENT_LEFT)" in a unit script from the cursed, and i got how it can be used.

Code: Select all

function script.Create( unitID )
	while GetUnitValue(COB.BUILD_PERCENT_LEFT) > 0 do
  	Sleep(500)
	end
	open_up()
end
Anyway. tanks FLOZi for pointing me in the right direction.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Mod Question Repository... Questions come in, answers go out

Post by CarRepairer »

FireStorm_ wrote:I first copied from your reply but that didn't work.
Please try Flozi's code again but replace

Code: Select all

function script.Create( unitID )
with

Code: Select all

function script.Create( )
and let us know if it works. unitID should be pulled from outside the scope of that function, so putting it as a parameter is breaking the code.

Using the lua health function is better than using the COB.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Mod Question Repository... Questions come in, answers go out

Post by FLOZi »

CarRepairer wrote:
FireStorm_ wrote:I first copied from your reply but that didn't work.
Please try Flozi's code again but replace

Code: Select all

function script.Create( unitID )
with

Code: Select all

function script.Create( )
and let us know if it works. unitID should be pulled from outside the scope of that function, so putting it as a parameter is breaking the code.

Using the lua health function is better than using the COB.
Good catch.
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

CarRepairer wrote:let us know if it works

Code: Select all

function script.Create( )
	while (select(5, Spring.GetUnitHealth(unitID)) ~= 1) do 
  	Sleep(500)
	end
	open_up()
end
Works fine. :-)
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

I have a unit (and to help visualise my problem i'll just say:) witch looks like 4 baskets close together, and the unit can transport up to 4 apples.
I have specified a point to attach an apple to, but then, not surprisingly, they all attach to that point, and all apples are in one basket.
(i tried specifying more points :-) , but then it just picks the first one.)

Of course I'd like all the apples to end up in a separate basket.
(And unload all the apples at once, but that's no problem.)

So i thought perhaps i can make the specified point change to another point, after something was picked up.
perhaps something like this:

Code: Select all

	
	if (slot1full == false) then
    trans = linkpoint1
	slot1full = true
	end

    if (slot1full == true) then
    trans = linkpoint2
	slot2full = true
	end
	
	if (slot2full == true) then
    trans = linkpoint3
	slot3full = true
	end
	
	if (slot3full == true) then
    trans = linkpoint4
	end

	--with this:

function script.TransportPickup ( passengerID )
	UnitScript.AttachUnit(trans, passengerID )
end

Or is that a bad approach? I also tried this:

Code: Select all

function script.TransportPickup ( passengerID )
	
	StartThread( pick_up )
	
	if (slot1full == false) then
    UnitScript.AttachUnit(linkpoint1, passengerID )
	slot1full = true
	end
	
	if (slot1full == true) then
    UnitScript.AttachUnit(linkpoint2, passengerID )
	slot2full = true
	end
	
	if (slot2full == true) then
    UnitScript.AttachUnit(linkpoint3, passengerID )
	slot3full = true
	end
	
	if (slot3full == true) then
    UnitScript.AttachUnit(linkpoint4, passengerID )
	slot1full = false
	slot2full = false
	slot3full = false
	end
	
	StartThread( stop )
end
I'm find myself not skilled enough to figure this out on my own in reasonable time. (or at all)
So once again i hope someone can be kind enough to supply some advise, or a reference to something that might help me.
(and be advised, i'm still learning... :-) )
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Mod Question Repository... Questions come in, answers go out

Post by FLOZi »

I'd do something like:

Code: Select all

local baskets = {}
local numBaskets = 4
local currBasket = 1

for i = 1, numBaskets do
  -- this assumes you have pieces basket1, basket2 etc
  baskets[i] = piece ("basket" .. i )
end

function script.TransportPickup ( passengerID )
  Spring.UnitScript.AttachUnit(baskets[currBasket], passengerID)
  currBasket = currBasket + 1
  if currbasket > 4 then currBasket = 1 end
end
No guarantees on plug and play. :P
User avatar
FireStorm_
Posts: 666
Joined: 19 Aug 2009, 16:09

Re: Mod Question Repository... Questions come in, answers go out

Post by FireStorm_ »

Took me some time before it worked. Mea culpa; I changed some stuff and i didn't do that correctly.
Figuring out what was wrong did help with understanding Lua and programming though. Yeey! :-) . So, thanks.

Next challenge is keeping my transport unit from starting a transport while doing the pickup animation. Now it runs 4 pickup animations sort of concurrently, while the objects-to-be-transported are attached with almost no time in between.

1)Could someone refer me to an example or provide advise how this might/can be fixed?

Challenge after that is keeping the unit from transporting while not in range, and move towards the target instead, until it is in range and then start transporting (but I'll save that one for now) :-)

Also i was skimming over the morph gadget, and i was wondering: Why is it so long? Isn't it mainly a matter of replacing a units appearance with the appearance of another unit? I imagined that not much more complicated than replacing a unit with another unit.

2)What is it the morph gadget does behind the scenes?
User avatar
BrainDamage
Lobby Developer
Posts: 1164
Joined: 25 Sep 2006, 13:56

Re: Mod Question Repository... Questions come in, answers go out

Post by BrainDamage »

may I suggest to let this obnoxious thread to die and start using instead http://answers.springlobby.info ?
this thread is really unmanageable, and a pain to search any previous answers because of the lack of tags and cathegorization:/
Locked

Return to “Game Development Tutorials & Resources”