[Discussion] Generic X3TC S&M questions II

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

User avatar
Ketraar
EGOSOFT
EGOSOFT
Posts: 12166
Joined: Fri, 21. May 04, 17:15
x4

Post by Ketraar »

Argonaught. wrote:Variables are killing me and giving me nightmares
Best would be that you explain what you are after, then one can tell how to proceed.
Repeat this step until most stuff is known.

I find this the best way to learn MD, including some try&error of course.

MFG

Ketraar
Image
Rednoahl
Posts: 1400
Joined: Sat, 3. Jan 09, 15:48
x4

Post by Rednoahl »

Argonaught. wrote:
ScRaT_GER wrote:the MD is very picky about errors...
I've discovered that alot :S
So have I...three days to cure the last error only for a fresh one to pop up!

@Ketraar - thanks for the examples. I'll be putting some code up tomorrow that I'd like you to have a look at if you get chance - it's far too long to post here. I'd really appreciate any feedback - I've definitely done a number of things wrong even though most of it works!
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Well. I'm having another loop problem...

I've got a loop in a loop this time, and it's got me in a bit of a loop. I hope I can just find a loophole to get through the loop. (I seem to be looping through endless problems this week) :roll:

Anyway. This is the code:

Code: Select all

001   $M3s =  get ship array: of race Boron class/type=M3
002   
003   
004   $Count =  size of array $M3s
005   while $Count
006   |dec $Count = 
007   |$Ship = $M3s[$Count]
008   |$Ship -> set name to 'Done!'
009   |$HardW = $Ship -> get ship hardware as array
010   |$Count2 =  size of array $HardW
011   |while $Count2
012   ||dec $Count2 = 
013   ||$Hardware = $HardW[$Count2]
014   ||
015   ||if $Hardware == Plasma Burst Generator
016   |||= $Ship -> add -1 units of Plasma Burst Generator
017   |||$Ship -> set name to 'LessPBG'
018   ||else
019   |||$Ship -> set name to 'NOPBG!'
020   |||
021   ||end
022   |end
023   end
024   return null
The second loop to check ship harware doesn't run. I don't get the ship called LessPBG or NOPBG, and I eventually found line 10 is where it stops working, so what have I done wrong (it'll be a stupid mistake this time...)
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

Did it ever come to you mind to check what $Hardware really is?
I'm not sure whether it's the correct command to get the ships weapons, maybe try "get tradable ware arrays from ship" instead.

Plus: Better use waits in each loop, even if it's just 1 ms. Even if it might not be necessary in this case, it's good practice to always use them. ;)

Greetings,
ScRaT
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

I'm not sure whether it's the correct command to get the ships weapons, maybe try "get tradable ware arrays from ship" instead.
Surely that's the tradeable wares though... Laser -> Installed -> Not tradeable?
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

I just re-read you post:
I don't get the ship called LessPBG or NOPBG
Well, in this case, $HardW is sure to be an empty array, so the command is definetely not the one you need.

There is no command to directly get every ware, including installed weapons, etc., on a ship. I think Cycrow wrote a library for that purpose, so maybe you shoud check that.

Greetings,
ScRaT
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

I don't have to get every ware... I just need to get installed weapons. Grr, why do the SE commands have to be so confungleing! :P .
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
mark_a_condren
Posts: 1468
Joined: Wed, 3. Aug 05, 05:05
x3tc

Post by mark_a_condren »

EmporerJon

Just a question, but why get an array at all? just check if it has any in the cargobay. Save the second loop as well.

Code: Select all

001   $M3s =  get ship array: of race Boron class/type=M3 
002    
003    
004   $Count =  size of array $M3s 
005   while $Count 
006   |dec $Count = 
007   |$Ship = $M3s[$Count] 
008   |$Ship -> set name to 'Done!' 
009   |PBG.Onboard = $Ship -> get amount of ware: 'Plasma Burst Generater' in cargobay 
010   |
011   |
012   |
013   |
014   |If $PBG.Onboard >0
015   ||$Remove.Amount = - $PBG.Onboard
016   ||= $Ship -> add $Remove.Amount units of Plasma Burst Generator 
017   ||$Ship -> set name to 'LessPBG' 
018   |else 
019   ||$Ship -> set name to 'NOPBG!' 
020   |end 
021   | 
022   | 
023   end 
024   return null
I don't know if this would do what you want, Just askin is all.

MarCon
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

just check if it has any in the cargobay
Are you sure that this also finds installed weapons? If so, that would be the best solution.

Greetings,
ScRaT
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Except I want to cycle through every weapon it's got, with a random chance of removing it or not based on certain rules... hmm...

I'll update the coding.

Ignore silly Debug ship names...

Code: Select all

001   $M3s =  get ship array: of race Pirates class/type=M3
002   
003   $Count =  size of array $M3s
004   while $Count
005   |dec $Count = 
006   |$Ship = $M3s[$Count]
007   |$Ship -> set name to 'Done!'
008   |$Race = $Ship -> get owner race
009   |$GCheck = $Ship -> get local variable: name='ej.weapon.change'
010   |skip if $GCheck != [TRUE]
011   ||gosub End
012   |skip if $Race != Player
013   ||gosub End
014   |$Ship -> set local variable: name='ej.weapon.change' value=[TRUE]
015   |
016   |$HardW =  get warearray for $Ship
017   |$Count2 =  size of array $HardW
018   |while $Count2
019   ||dec $Count2 = 
020   ||$Hardware = $HardW[$Count2]
021   ||
022   PBGChecks:
023   * Not Pirate or Yaki M3 - 1 in 4 chance of keeping PBG
024   ||if $Hardware == Plasma Burst Generator AND $Race != Pirates AND $Race != Yaki
025   |||$Rand1 =  = random value from 0 to 4 - 1
026   |||if $Rand1 > 0
027   ||||= $Ship -> add -1 units of Plasma Burst Generator
028   ||||$Ship -> set name to 'LessPBG'
029   |||else if $Rand1 == 0
030   ||||$Ship -> set name to 'KeptPBG'
031   |||else
032   ||||$Ship -> set name to 'It had no PBGs'
033   ||||gosub PBEChecks
034   |||end
035   |||
036   * Pirate or Yaki M3 - 1 in 2 chance of keeping PBG
037   ||else if $Hardware == Plasma Burst Generator AND $Race == Pirates AND $Race == Yaki
038   |||$Rand2 =  = random value from 0 to 2 - 1
039   |||if $Rand1 > 0
040   ||||= $Ship -> add -1 units of Plasma Burst Generator
041   ||||$Ship -> set name to 'LessPBG'
042   |||else if $Rand1 == 0
043   ||||$Ship -> set name to 'KeptPBG'
044   |||else
045   ||||$Ship -> set name to 'It had no PBGs'
046   ||||gosub PBEChecks
047   |||end
048   |||
049   PBEChecks:
050   |||
051   End:
052   |||$Ship -> set name to 'Wah'
053   ||end
054   |end
055   end
056   return null
EDIT: using get ware array now, as you can see. Same result.
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

I just checked, "get amount of ware in cargobay" does exactly what you want, so it finds installed lasers, too.
using get ware array now
This is an entirely different command, which returns the wares defined in the warelist for a ship in the TShips.

Greetings,
ScRaT
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Ok, well I changed the whole lot:

Code: Select all

001   $M3s =  get ship array: of race Pirates class/type=M3
002   
003   $Count =  size of array $M3s
004   while $Count
005   |dec $Count = 
006   |$Ship = $M3s[$Count]
007   |$Ship -> set name to 'Done!'
008   |$Race = $Ship -> get owner race
009   |$GCheck = $Ship -> get local variable: name='ej.weapon.change'
010   |skip if $GCheck != [TRUE]
011   ||gosub End
012   |skip if $Race != Player
013   ||gosub End
014   |$Ship -> set local variable: name='ej.weapon.change' value=[TRUE]
015   |
016   |
017   PBGChecks:
018   * Not Pirate or Yaki M3 - 1 in 4 chance of keeping PBG
019   |$Amount = $Ship -> get amount of ware Plasma Burst Generator in cargo bay
020   |if $Amount != 0 AND $Race != Pirates AND $Race != Yaki
021   ||$AmountTake = $Amount + 1
022   ||$Rand1 =  = random value from 0 to $AmountTake - 1
023   ||$Take = - $Rand1
024   ||= $Ship -> add $Rand1 units of Plasma Burst Generator
025   |else if $Amount == 0
026   ||display subtitle text: text='No PBGs onboard' duration=300 ms
027   |end
028   |gosub PBEChecks
029   |
030   * Pirate or Yaki M3 - 1 in 2 chance of keeping PBG
031   |
032   PBEChecks:
033   |$Amount = $Ship -> get amount of ware Plasma Burst Generator in cargo bay
034   |if $Amount != 0 AND $Race == Pirates OR $Race == Yaki
035   ||$AmountTake = $Amount + 1
036   ||$Rand2 =  = random value from 0 to $AmountTake - 1
037   ||$Take = ( - $Rand2 ) / 2
038   ||= $Ship -> add $Rand1 units of Plasma Burst Generator
039   |else if $Amount == 0
040   ||display subtitle text: text='No PBGs onboard' duration=300 ms
041   |end
042   |gosub PBEChecks
043   |
044   End:
045   |$Ship -> set name to 'Wah'
046   end
047   return null
Still... not working. Ship stays as 'Done'

Anyway, another problem. You'll see it gets an array of Pirate M3s. I want all M3s. If I say:

Pirates Null - it gets all pirate ships
Null M3 - gets nothing. I want all M3s of all races, how?
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Note:

Above code contains various inaccuracies which have been fixed.

The question about getting all M3s still stands.

And, it also appear to remove all PBGs on Pirate craft, and none on non Pirate craft (I remebred to change the array for them, that's not the problem.

Hm. The mystery thickens.

EDIT: 'ere we goes!

Code: Select all

001   $M3s =  get ship array: of race Split class/type=M3
002   
003   $Count =  size of array $M3s
004   while $Count
005   |dec $Count = 
006   |$Ship = $M3s[$Count]
007   |$Ship -> set name to 'Done!'
008   |$Race = $Ship -> get owner race
009   |$GCheck = $Ship -> get local variable: name='ej.weapon.change'
010   |skip if $GCheck != [TRUE]
011   ||gosub End
012   |skip if $Race != Player
013   ||gosub End
014   |$Ship -> set local variable: name='ej.weapon.change' value=[TRUE]
015   |
016   |
017   PBGChecks:
018   * Not Pirate or Yaki M3 - Loses random amount of PBGs
019   |$Ship -> set name to 'Checks'
020   |$Amount = $Ship -> get amount of ware Plasma Burst Generator in cargo bay
021   |if $Amount != 0 AND $Race != Pirates AND $Race != Yaki
022   ||$AmountTake = $Amount + 1
023   ||$Rand1 =  = random value from 0 to $AmountTake - 1
024   ||$Take = - $Rand1
025   ||= $Ship -> add $Rand1 units of Plasma Burst Generator
026   |else if $Amount == 0
027   ||display subtitle text: text='No PBGs onboard' duration=300 ms
028   |end
029   |gosub PBEChecks
030   |
031   * Pirate or Yaki M3 - Loses new random amount of PBGs, divided by 2.
032   |
033   |$Amount = $Ship -> get amount of ware Plasma Burst Generator in cargo bay
034   |if $Amount != 0 AND $Race == Pirates OR $Race == Yaki
035   ||$AmountTake = $Amount + 1
036   ||$Rand2 =  = random value from 0 to $AmountTake - 1
037   ||$Take = ( - $Rand2 ) / 2
038   ||= $Ship -> add $Rand1 units of Plasma Burst Generator
039   |else if $Amount == 0
040   ||display subtitle text: text='No PBGs onboard' duration=300 ms
041   |end
042   |gosub PBEChecks
043   |
044   PBEChecks:
045   |
046   End:
047   |$Ship -> set name to 'Wah'
048   end
049   return null
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
ThisIsHarsh
Posts: 1135
Joined: Sun, 19. Oct 08, 18:46
x3tc

Post by ThisIsHarsh »

EmperorJon wrote:Ok, well I changed the whole lot:

<snip>

Still... not working. Ship stays as 'Done'
There's some pretty bad practice going on in that there code. You don't seem to grasp the use of gosub subroutines yet. Line 15 should have an 'end', to end the while loop, then line 16 a 'return null'.

Your subroutines "End:", "PBEChecks:" and "PBGChecks:" should end with 'endsub' so they return to the point where they were called from once finished, otherwise the script just continues running.

I'm not sure if that's your problem exactly, but certainly I would recommend you start by tidying this code up.

EDIT: on closer inspection it looks like you mean to use 'goto label' rather than 'gosub'. I would still recommend some code tidying here. Using labels like that to skip sections of processing is bad practice. Use something like

Code: Select all

$valid = [TRUE]
skip if $GCheck != [TRUE] 
| $valid = [FALSE]
skip if $Race != Player 
| $valid = [FALSE]

if $valid
| gosub PBGChecks
end
EmperorJon wrote: Anyway, another problem. You'll see it gets an array of Pirate M3s. I want all M3s. If I say:

Pirates Null - it gets all pirate ships
Null M3 - gets nothing. I want all M3s of all races, how?
I don't think you can get an array of all races like that. In my own script I've had to do each race individually, but its not that bad if you know how to use gosub properly :wink: .
Last edited by ThisIsHarsh on Thu, 8. Apr 10, 15:47, edited 1 time in total.
There are 10 types of people in the S&M forums - those who understand binary, and those who don't.

Black holes are where God divided by zero.
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

Your code is erroneous. For example the following infinite loop: (edit: Okay this problem does not exists in you last post anymore, but it's still not better ;) )

Code: Select all

PBEChecks:
* Some code
gosub PBEChecks
And what are all these gotos and gosubs for? Try to avoid these.
Take lines 10 to 12 for example. You could combine these statements and avoid the "gosub End" by simply putting this procedure there (btw: "End" is a irritating name, because it implies that the script is ended here).
If you need this procedure more often, it is of course okay to put it into a label.
And you are using goto and gosub wrong. A "gosub" jumps to a piece of code and jumps back, when it hits an "endsub". A "goto" does not jump back. So in your case if $GCheck is [TRUE] it would simply end the script, because it does not jump back as you expect.

On top of that, your code only is accepted by the game, because you used "end" instead of "endsub" in line 48. If you used "endsub" there would be one "end" missing before.
Null M3 - gets nothing. I want all M3s of all races, how?
The command unfortunately does not work this way, so fastest possibilty might be to use another loop, which loops through all races. Or you could try the "find ship" command with a Find.Multiple - Flag.

Greetings,
ScRaT
Last edited by ScRaT_GER on Thu, 8. Apr 10, 15:41, edited 1 time in total.
mark_a_condren
Posts: 1468
Joined: Wed, 3. Aug 05, 05:05
x3tc

Post by mark_a_condren »

EJ

To get all the races you will have to make an array of all the races you want to check and then cycle (loop) through that array and check each race.

Code: Select all

$Races.Array = array alloc size:= 0
Append: Pirates to array: $Races.Array
Append: Yaki to array: $Races.Array
Append: Argon to array: $Races.Array
Append: Boron to array: $Races.Array
Append: Split to array: $Races.Array
ETC ......

Then cycle through it.

Code: Select all

$Races.Array.Count = size of array: $Races.Array
Counter = 0
While Counter < $Races.Array.Count
$Race = $Races.Array [Counter]

Get ship array of race blah blah

do your check here ...

inc Counter
end
You cant chech all races at once with the command your using.

MarCon
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

ThisIsHarsh:

Thanks!


Scrat:

Changed first bit.

Not good with gotos.

GCheck = TRUE does end the script, thats the point. End is the end.


Mark:

Thanks!


Taking a bit of a break now...
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
ThisIsHarsh
Posts: 1135
Joined: Sun, 19. Oct 08, 18:46
x3tc

Post by ThisIsHarsh »

Whoa! I post my response, go to edit it, and 4 replies come in that time! I wish my questions were answered so quickly/thoroughly in this forum!
There are 10 types of people in the S&M forums - those who understand binary, and those who don't.

Black holes are where God divided by zero.
mark_a_condren
Posts: 1468
Joined: Wed, 3. Aug 05, 05:05
x3tc

Post by mark_a_condren »

@ThisIsHarsh
I wish my questions were answered so quickly/thoroughly in this forum!
Be nice wouldn't it.

@EJ
Taking a bit of a break now...
Ahh ... The brain has gone into overload shutdown mode :wink:

I'll post a link to the code i use for my lib script 'Get Races Array', very handy to be able to call if you need the races array for anything.

EDIT:
Races array script

If you wish to exclude any races from the returned array, just build an array as described earlier and reference it in the 'Exclude Array' argument, leave it as 'null' to return all races.

My advice would be to start building yourself some lib scripts that do the things that you might want do often, it saves a lot of multiple scripting.

MarCon
Last edited by mark_a_condren on Thu, 8. Apr 10, 17:07, edited 1 time in total.
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

I wish my questions were answered so quickly/thoroughly in this forum!
I guess your questions are harder to answer. =)

Greetings,
ScRaT

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”