Hard Light Productions Forums

Modding, Mission Design, and Coding => The FRED Workshop => Topic started by: karajorma on March 04, 2005, 01:41:52 pm

Title: Associating Variables With A Ship
Post by: karajorma on March 04, 2005, 01:41:52 pm
Okay. Here's another FRED question :)

In the mission I'm currently writing I want to have a variable that is associated with a certain ship. Basically at one point in a mission I need to be able to choose a ship from alpha wing and then based on which ship I've picked alter a variable.

In other words if Alpha 1 is choosen at random I need to set the variable to be 1. 2 if Alpha 2 was choosen and so on.

Anybody got an idea on how you can do this without having to have an event for each ship like this?

When
-Check somehow that Alpha 1 is the choosen ship
-Set Variable
-1

When
-Check somehow that Alpha 2 is the choosen ship
-Set Variable
-2

etc?
Title: Associating Variables With A Ship
Post by: kasperl on March 04, 2005, 01:47:32 pm
I'm no FREDder, but can't you do this the other way and have the ship be picked on basis of the variable? You can give a var a random value, right? If so, can you select Alpha X instead of Alpha 1?
Title: Associating Variables With A Ship
Post by: karajorma on March 04, 2005, 01:58:38 pm
I don't think that one is any easier to be honest. In fact I think at best it's just the same set of problems in reverse. For my mission I think it would actually be harder.
Title: Associating Variables With A Ship
Post by: Blaise Russel on March 04, 2005, 02:06:39 pm
I may not be understanding the question correctly, but what's stopping you from just changing the variable randomly 1 to 4? Why this middle stage of associating a random variable generation to a ship and then changing another variable on the basis of the ship chosen? Does something happen in-game that requires this method, or...?
Title: Associating Variables With A Ship
Post by: karajorma on March 04, 2005, 02:59:30 pm
It's tricky to explain without giving away the entire mission. Everyone seems to like doing things the reverse way round so fair enough I'll explain things that way round.

 Suppose I did choose the variable at random.  I need to use this randomly selected variable to decicde which ship triggers a certain event. The variable will be used in a certain event later on.

Okay I've got the randomly selected variable. Lets say it's 3. How does that help me? The only way I can use the variable that I can think of is to have 4 events like this

when
- =
--Variable
--1
-Do something to alpha 1

when
- =
--Variable
--2
-Do something to alpha 2

etc.

What I'm wondering is if there is an easier way to do it. I want to know if there is a way to do it in one event because at the momnent I've got tonnes of places all over the mission where I'm having to use 4 events and it's just eating into my SEXP node count.

The best I've come up with so far (Which I thought up while posting this) was to create a subsystem. On alpha 1 I'd set it to 1. On alpha 2 I'd set it to 2. etc.

Then I could simply do this

when-arguement
any-of
Alpha 1
Alpha 2
Alpha 3
Alpha 4
-=
--Hits-left-subsystem
---Argument
---Counter Subsystem
--Variable
-Do-Whatever

Only problem is that for Alpha 1 the subsystem would appear on the HUD. It would also repair itself which means that after a few seconds the entire thing would stop working. It would also mean the appearance of a strange new subsystem on the ship.

So is there any way I can hide that? Do Bob's untagettable subsystems work yet? Can I get this subsytem to not auto-repair?

Failing all that is there anything else I could set to a number I could read in and out easily and which wouldn't get changed by the mission?
Title: Associating Variables With A Ship
Post by: Sesquipedalian on March 05, 2005, 06:41:55 pm
Use a string variable instead of a number variable.

Then set up the events somthing like this:
Title: Associating Variables With A Ship
Post by: karajorma on March 06, 2005, 03:38:55 am
I did try using string variables but abandoned the idea almost instantly because I couldn't test for them using = (At least FRED wouldn't let me. That might be a limitation of FRED rather than FS2_Open cause I do remember hearing that string variables were given various improvements soon after the source came out).

There might be another way of using them like you have there though so I think I should take another look at my mission and do some FRED testing. :)

EDIT : Just done a quick 2-3 minute test and only a few SEXPs I use that take a argument will change that to a string variable in FRED.

Did you do a lot of editing in notepad?
Title: Associating Variables With A Ship
Post by: Sesquipedalian on March 09, 2005, 12:48:47 am
No notepad editing whatsoever.  Do you mind showing the actual events you are using?
Title: Associating Variables With A Ship
Post by: karajorma on March 09, 2005, 12:29:03 pm
Actually Goober suggested a way (on the MG internal) to get the mission to work using the subsystem trick I'd suggested so I'm going to try that.

In case you're wondering though my quick tests found that you couldn't substitute in a string variable in the target, Set-ship-position and sabotage-subsystem SEXPs (I basically went through my mission and substituted every reference to a ship I could find to see which ones worked so there are probably loads more that wouldn't work either).

Thanks for the idea though. I had thought of using a string variable in this way but I tested it with set-ship-position, found it didn't work and assumed that it was the whole idea of using a string that was flawed rather than just using it with that SEXP.

It is possible to build my mission using a string variable in this way if I used every-time argument to resolve the ship I'm using but that's pretty much the same as using a ships subsystem to do the same thing so I think I'll stick to that for the time being.

Thanks a bunch anyway :)
Title: Associating Variables With A Ship
Post by: Cobra on March 09, 2005, 06:37:52 pm
This is very educational :D

Although what I would like to know is actually how they work and when to use them.
Title: Associating Variables With A Ship
Post by: pyro-manic on March 09, 2005, 06:54:36 pm
They're covered in the FRED walkthrough. Go and do it again, and mess around with them to see what happens. :nod:

Basically they're used to store information, eg. if you wanted to keep track of how many ships got destroyed in a convoy, you could set up sexps to add 1 to a variable every time one died, and another event to check the value of the variable and give you a "mission failed" message or whatever.

(Note: This is only a simple example. Variables are a very useful tool that get used a lot in complex missions. As I said, the best way to understand them is to do the walkthrough. They cover the "Sleipner" transport wing's docking to the Orion IIRC...)
Title: Associating Variables With A Ship
Post by: Cobra on March 09, 2005, 07:04:06 pm
Quote
Originally posted by pyro-manic
They're covered in the FRED walkthrough. Go and do it again, and mess around with them to see what happens. :nod:

Basically they're used to store information, eg. if you wanted to keep track of how many ships got destroyed in a convoy, you could set up sexps to add 1 to a variable every time one died, and another event to check the value of the variable and give you a "mission failed" message or whatever.

(Note: This is only a simple example. Variables are a very useful tool that get used a lot in complex missions. As I said, the best way to understand them is to do the walkthrough. They cover the "Sleipner" transport wing's docking to the Orion IIRC...)


Got it in one. I've done half of the walkthrough mission, yet I still don't get variables...
Title: Associating Variables With A Ship
Post by: karajorma on March 09, 2005, 07:47:48 pm
Variables?

The problem with variables is that it's hard to explain what you use them for. FRED's SEXPs are pretty powerful so variables tend to only be used when you come across a problem you can't solve any other way or which would be complicated to solve without them. That means that trying to come up with an example of how you use a variable means that you have to come up with a pretty detailed example.

The FRED2 Walkthrough has a good example of how to use a variable. In that mission you have 4 troop transports attempting to capture an orion. The transports will dock with the Orion. Spend a little while disgourging troops and then undock. If two of the troopships manage to successfully dock with the Orion then the orion is captured by the enemy forces. In the walkthrough they used variables as a way of achieving this. Basically they defined a variable called DockCount and added 1 to it every time one of the troop transports managed to successfully dock. When the value of the variable was 2 then the Orion could be captured and a simple when DockCount = 2 type event could be used to test this.

Although the walkthrough shows a good example of how to use a variable it's not the best example as though it would be a little tedious this example could have been made using normal SEXPs (basically a big OR checking that two transports made it)

So lets suggest something a little more complicated. Lets say that you want to say a certain number of soldiers are on the Orion. Let's say that it doesn't matter if the troop transport managed to get undocked safely since they are in battle armour. All that matters is the time the transports managed to stay docked to the Orion.

Now that is pretty much impossible without variables. With them. Well it's not easy but it's possible.

First you set up a variable called timedocked (with an inital value of 0). Then you set up a repeating event for each transport (Lots of repeats, repeat every second).

When
-and
--Has-Docked-Delay
---Transport x
---10 (lets say it takes 10 seconds after docking to open the airlock etc before troops start moving into the Orion)
--not
---has-undocked-delay
----Transport x
----0
-Modify-Variable
--TimeDocked
--- +
----TimeDocked
----1

Basically that event waits until the transport has been docked for 10 seconds and then adds 1 to the value of TimeDocked every second until the ship undocks.

Then you have an event like this

When
- =
--TimeDocked
--100
-do-whatever

That event triggers whenever timeDocked reaches 100 seconds regardless of whether each ship stayed docked for 25 seconds, one ship managed a full 100 seconds or any other combination you can think of. As you can see there is no way you could do that without variables :)

That's number variables. I've not used String variables because so far I've always wanted to use a SEXP with them that they don't like (For instance =. You can't test a String variable to see if it's equal to another string variable which basically rules them out for a lot of uses. Sesq may have a few good examples of their use.

If you're using FS2_Open you'll have access to persistant variables. These aren't reset at the end of a mission so they can be used in a campaign to do something like having something occur in Mission 12 based on a value from mission 4. (For instance have a cruiser take longer to appear depending on how badly shot up it was in the earlier mission). I've already written an example of how to use PV's here (http://www.hard-light.net/forums/index.php/topic,26927.0.html).
Title: Associating Variables With A Ship
Post by: Goober5000 on March 10, 2005, 11:36:19 am
Quote
Originally posted by karajorma
Thanks for the idea though. I had thought of using a string variable in this way but I tested it with set-ship-position, found it didn't work and assumed that it was the whole idea of using a string that was flawed rather than just using it with that SEXP.
This sounds like a good feature to have, though.  It shouldn't be difficult to modify sexps to accept string variables in this way.  Remind me in a week and you'll have it. :)

As for your variable example, the best illustration I've seen is using variables for scanning the Sathanas subsystems or destroying its turrets.  Much simpler to increment a counter than to test for all possible combinations.
Title: Associating Variables With A Ship
Post by: karajorma on March 10, 2005, 01:40:26 pm
I agree but I wanted an example of something that couldn't be handled using the new When-Argument SEXPs and those could :)

That said a tutorial on how to use When-Argument is one of the things on my list for FAQ v2. Most people haven't realised how powerful and useful those SEXPs can be :)
Next person who asks how to use them may just get a really detailed example just like Cobra did for variables cause I'm trying to kill two birds with one stone whenever I answer technical questions at the moment :D

As for the use of string variables do you want a list of SEXPs that don't work with them? Cause at the moment I've found that the assigning a variable to a subsystem method has one major flaw I overlooked (I.e you can't test if a ship is dead because the ship is dead and the subsystem\variable is therefore unreachable! :D ). This isn't an unsurmountable problem but I'm having fun SEXPing at the moment and I want to see if I can make the mission with the mimimum number of missions.

BTW that reminds me. I think I asked before but IIRC When-argument internally expands into normal SEXPs right? Do the internal SEXP nodes count against the maximum number of SEXP nodes? I seem to remember you telling me they didn't but I had a mission hit the maximum number of SEXPs allowed with only around 15 events in it (3/4 of them were every-time-argument though).
Title: Associating Variables With A Ship
Post by: Goober5000 on March 10, 2005, 09:25:53 pm
Quote
Originally posted by karajorma
As for the use of string variables do you want a list of SEXPs that don't work with them?
No, I just need to be reminded.  (I'm on spring break ATM.)  All I have to do is add the capability for string variables to be treated as strings in the node.  It's really quite simple - less work even than when I changed sexps that only accepted hard-coded numbers to also accept numbers returned by other sexps.
Quote
BTW that reminds me. I think I asked before but IIRC When-argument internally expands into normal SEXPs right?  Do the internal SEXP nodes count against the maximum number of SEXP nodes? I seem to remember you telling me they didn't but I had a mission hit the maximum number of SEXPs allowed with only around 15 events in it (3/4 of them were every-time-argument though).
Yes, you did ask me before and I gave a detailed explanation which I don't feel like repeating. :p Suffice it to say that the game treats it like multiple expanded sexps but doesn't actually create new sexps.  So no extra nodes are used besides the ones you see.
Title: Associating Variables With A Ship
Post by: karajorma on March 11, 2005, 04:41:58 am
Yeah. That's how I thought it worked. Odd that I run out of SEXPs with so few events. That said now that I think about it FRED did crash out about 10 minutes later so there might have been some other problem lurking.
Title: Associating Variables With A Ship
Post by: Goober5000 on March 15, 2005, 12:34:09 am
Hmm.  The string variable thing was easier than I thought.  So much easier, in fact, that it was already implemented. :wtf: What problems were you having with it, exactly?

I did implement some string comparison functions (equals, greater than, and less than) though.  It was apparent that those were needed.
Title: Associating Variables With A Ship
Post by: karajorma on March 15, 2005, 07:23:09 am
What I found was that it didn't work for some SEXPs so I just went back and tested it. I was assuming that it simply meant that certain SEXPs didn't work with a string variable. What I found was more interesting.

If you set up a variable like this ShipName(Alpha 1) All the SEXPs I tested worked.

The problem is that at the start of the mission I had a string variable which I didn't want to be assigned to any particular ship. So being a good programmer my variable looks like this  ShipName(Null). That doesn't work. You can only use a string variable in the SEXPs I tested (basically everything from the objectives sub menu) if the default value is a ship already present in the mission. You can't even assign it to Cancer 7 when there are two waves of cancer wing.

You can't assign it to be null in FRED. I have no idea what would happen if you assigned the variable to be null in the mission. Hopefully nothing cause I'm always going to be doing a null pointer check before using it. (So I shouldn't be doing anything stupid like assigning damage to null etc).

Anyway I don't know how fixable this is. If it's a five minute job then this is something I really want because the way my mission is currently set up I pretty sure I'm going to need variables set to null. If this is hard to do then just tell me and I'll see if I can reorganise the mission so that I don't need to do that. Now that I've got equivalence testing I suppose I could simply put in a ship very far away called Dummy and set the variable to that.
Title: Associating Variables With A Ship
Post by: Goober5000 on March 15, 2005, 12:37:18 pm
Quote
Originally posted by karajorma
The problem is that at the start of the mission I had a string variable which I didn't want to be assigned to any particular ship. So being a good programmer my variable looks like this  ShipName(Null). That doesn't work. You can only use a string variable in the SEXPs I tested (basically everything from the objectives sub menu) if the default value is a ship already present in the mission. You can't even assign it to Cancer 7 when there are two waves of cancer wing.
That's probably just FRED doing its error-checking.  Fire up Notepad and see what happens. ;)
Title: Associating Variables With A Ship
Post by: karajorma on March 15, 2005, 01:17:01 pm
Wouldn't surprise me if it was. The thing is that my mission is pretty heavy in variables and if I have to add them all in notepad then I might as well just write the entire mission there (and I'm certainly not that insane!). :)

Going into notepad for an edit or two is one thing. Trying to do it for 10 different events is a whole other thing.

I'll give it a try though.
Title: Associating Variables With A Ship
Post by: Cobra on March 15, 2005, 03:08:53 pm
editing a mission in Notepad is fun, especially when you want to fine-tune the coordinates :D
Title: Associating Variables With A Ship
Post by: Roanoke on March 16, 2005, 03:14:14 pm
I can hardly imagine making a mission with so many events it would be hitting the FRED limits. It'd be fascinating to get a look at one such missson.
Title: Associating Variables With A Ship
Post by: Cobra on March 16, 2005, 03:19:22 pm
try Project Road Block. :p
Title: Associating Variables With A Ship
Post by: Goober5000 on March 16, 2005, 03:35:59 pm
Try CLASSIFIED in Silent Treat: Reborn. ;)
Title: Associating Variables With A Ship
Post by: Cobra on March 16, 2005, 03:55:08 pm
:lol:

we want ST:R before we rebel. :D
Title: Associating Variables With A Ship
Post by: karajorma on March 16, 2005, 07:23:04 pm
Quote
Originally posted by Goober5000
That's probably just FRED doing its error-checking.  Fire up Notepad and see what happens. ;)


Okay. Finally got around to checking and it's not just FRED. FS2 crashes out. I suppose I shouldn't be surprised really. My mission only had three events

When
-Key-Pressed
-T
-Add-Goal
--Beta
--Ai-Warpout

When
-Key-Pressed
-V
-Modify Variable
--Ship(Null)
--Beta 1

When
-Has-Departed
--Ship(Null)
--0
-Send-Message
--Beta1 has gone

It's pretty obvious that FS2 would hate that cause as soon as the mission is loaded the code is going to try to check if Null has departed and then fall over when it can't figure out what the f**k Null is. (In fact the mission doesn't even load. It crashes out long before that with this message).

Code: [Select]

Error: Invalid ship/wing name.

In sexpression: ( when
      ( has-departed-delay 0 1[] )
   ( send-message
      "#Command"
      "High"
      "Beta 1 Gone"
   )
)
(Error appears to be: 1)
File:U:\src\cvs\fs2_open.testing\code\Mission\MissionParse.cpp
Line: 4517



I then tried this instead

When
-And-In-Sequence
--Event-True-Delay
---Was V Pressed
--Has-Departed
---Ship(Null)
---0
-Send-Message
--Beta1 has gone

In the hope that it would act like a shortcut  Logical and in Java (i.e ignore the rest of the expression until the first half is true) but it didn't work either.

To be honest I don't think allowing for string variables to be Null or any other value the SEXP isn't expecting is going to work without a hell of a lot of work for you Goober. You're welcome to prove me wrong of course. :D

That means that when I redesign the mission I'm going to have to use those new string variable equivalence testers. Is the code for them in CVS?
Title: Associating Variables With A Ship
Post by: Cobra on March 16, 2005, 08:23:31 pm
this is why i hate variables.
Title: Associating Variables With A Ship
Post by: Goober5000 on March 16, 2005, 09:23:41 pm
Quote
Originally posted by karajorma
To be honest I don't think allowing for string variables to be Null or any other value the SEXP isn't expecting is going to work without a hell of a lot of work for you Goober. You're welcome to prove me wrong of course. :D
Well, you can delay evaluation of sexps via chaining.  Clever sexping might work.
Quote
That means that when I redesign the mission I'm going to have to use those new string variable equivalence testers. Is the code for them in CVS?
Yep, it's in CVS.
Title: Associating Variables With A Ship
Post by: karajorma on March 17, 2005, 01:53:19 am
Quote
Originally posted by Cobra
this is why i hate variables.


I wouldn't take the problems I'm having here as a reason to avoid variables. They're an incredibly powerful tool. I'm running into problems cause I'm trying something right on the bleeding edge of their use. I doubt more than a handful of FREDders have ever used string variables let alone tried to assign them a null value like I'm doing. I'd certainly never given them more than a cursory glance before I tried using them in this mission.

Quote
Originally posted by Goober5000
Well, you can delay evaluation of sexps via chaining.  Clever sexping might work.


I don't think chaining will help. The crash is occuring on mission load not once the mission is running. I'd be surprised if the game checks the syntax of all the events on mission load but stops if it finds a chain.

EDIT : I realised that it would take me let time to check than it did to write that :) Yep. Chaining doesn't help null values can't be assigned to a SEXP. The mission must be able to make sense of the value during mission load. I'll test to see if you can assign a null value while the mission is running in a bit. My bet is that with chaining you can but an unchained mission will cause a spectacular CTD :)

I can see a way around the problem now that I can make equivalence tests though. I'll just assign the initial value of the SEXP to be an invisible far off ship called dummy and check if the variable = dummy before I do anything.

Quote
Originally posted by Goober5000
Yep, it's in CVS.


Thanks a lot for this once again. I'm sure only myself, Sesquipedalian and a small handful of other FREDders will care but I'm sure we'll find this very useful :D
Title: Associating Variables With A Ship
Post by: Sesquipedalian on March 17, 2005, 02:11:14 am
Indeed. :)