Question about variables, values, and scripting.

Avatar
Goldenknighttim
182 Posts
Posted Feb 22, 2014
This is my first attempt at scripting for a portal 2 map, so I may be missing something obvious. In the class info for an entity, I want to make a value a variable. Once I've got that, my hope is to be able to change this value dynamically in the chamber. I've written a couple simple scripts to create the global variable and set it to whatever I want when the script is called with an appropriate argument. I tried just typing in the variable name in the box, that doesn't work. I also tried opening up the .vmf in notepad++ and deleting the quotes around the value, but it added them back automatically. My only experience with scripting with squirrel so far is a tutorial I found on gamebanana.

My script that I'm testing the concept with is this:
function onebyone(target) {
target1x1 = target;
}
And the other is this(triggered from a logic auto):
target1x1 <- target;

Does anyone know why this isn't working, and what I need to know to get it working? There may be a way to do this without scripting, but I want to use this to learn.

Advertisement
Registered users don’t see ads! Register now!
Avatar
FelixGriffin
2,680 Posts
Posted Feb 22, 2014
Replied 1 hour later
I can't really tell what you're trying to do. What is "target"? You haven't assigned a value to it, and it isn't a built-in variable.
Avatar
Goldenknighttim
182 Posts
Posted Feb 22, 2014
Replied 7 minutes later
The one in the first script is a local variable assigned as an argument. The one in the second script is a place holder. I was just trying to declare the global variable there. I'm not completely sure how that is done, but I've experimented by giving it a few different types of values.(Like "Track_1x1" both with and without the quotes(track_1x1 is the name of the entity I'm trying to switch the value to.)) For that second script, target is just a placeholder.
Avatar
FelixGriffin
2,680 Posts
Posted Feb 22, 2014
Replied 1 hour later
I still don't quite understand what the problem you're having is. But you can't pass strings to functions directly from the map in Portal 2, because the quote marks around the argument confuse Hammer. That might be the issue.
Avatar
Goldenknighttim
182 Posts
Posted Feb 22, 2014
Replied 47 minutes later
ok, I'm not sure what format it needs to be entered in. I wasn't putting in a string in the argument. I tested by taking out the argument making the function set the variable to to what I want in the format of a string. If I'm not explaining it right, this is what It looks like now:
function onebyone() {
target1x1 = "Track_1x2";
}

And the other one that I added to declare the variable as a global variable is this:
target1x1 <- "Track_1x1";

It still isn't working. When I test it in the map, I get this error:
AN ERROR HAS OCCURED [the index 'target1x1' does not exist]

I believe my main problem is that what I'm trying to use as a variable is not acting as a variable, but instead the specific target itself. Not in my code, but in hammer. Specifically, I am trying to be able to dynamically change the "next stop target" of a path_track by putting a variable in the place of the value. I'm not sure if it's possible, but if it is, I'm convinced that I'm not doing it right.

Avatar
FelixGriffin
2,680 Posts
Posted Feb 22, 2014
Replied 1 hour later
If you want to do that use AddOutput, or even just alternate paths. Don't bother setting up a script.

Variables have three types of scope: local, entity-global, and map-global (I'm sure there are better terms for this, but I don't know them).

Local (goes out of scope when its block ends):

local x=20;

Entity-global (available for all functions in an entity's script):

x &lt;- 20;

Map-global (available for everything in your map):

::x &lt;- 20;

I'm guessing your two functions are executed on different entities.

Avatar
Goldenknighttim
182 Posts
Posted Feb 22, 2014
Replied 12 minutes later
ok, Thanks for your help. I've been hoping for something easy I could work with to give me a start into scripting.(I really want to sometime soon), but I'm guessing this is not the time to do that. I'll try just using addoutput then.
Avatar
FelixGriffin
2,680 Posts
Posted Feb 22, 2014
Replied 13 minutes later
If you want an easy (but interesting) scripting problem, try implementing a simple minigame in VScript. Perhaps a cellular automaton with cube buttons for input and output?

To make it even more interesting, only use three entities in your VMF: a player start, a script, and a logic_auto. :wink:

Avatar
Goldenknighttim
182 Posts
Posted Feb 22, 2014
Replied 24 minutes later
Thanks, I'll look into that.
Avatar
FelixGriffin
2,680 Posts
Posted Feb 22, 2014
Replied 32 minutes later
It sounds really hard, but it's a lot easier than it looks. I suggested it because it would be nearly impossible in Hammer alone.
Avatar
Goldenknighttim
182 Posts
Posted Feb 22, 2014
Replied 23 minutes later
I've got an idea on where to start with that. I just want to finish what I'm already working on first. I tried using addoutput with a parameter of: "target Track_1x2"(Track_1x2 is what I want the next stop target to be). It's not working. Do path_tracks have something against addoutput? I feel like I'm missing something.
Avatar
RustyDios
154 Posts
Posted Feb 23, 2014
Replied 9 hours later
So you have something (a train) moving along a path_track and you want to dynamically (ie: based on what's going on in your map at that moment) change the direction the train goes in (the next stop target)?

If I'm getting this right you should be able to use ToggleAlternatePath and EnableAlternatePath to switch to new route. And use a filtered trigger to send these inputs to your path_track ...

Alternatively if you want more than two directions why not play with the parenting options? If your platforms are parented to the train, have multiple paths and trains and "on pass" set the platform to parent the next train... maybe some logic_case/branches to set an AddOutput>OnPass>SetParent>Platform>New Train on the path track kinda stuff .... not sure how this would work out but it might be a good start to solve your problem using a hammer based solution instead of scripting.. because I've got no experience with squirrel so can't help you there. ...

Avatar
Goldenknighttim
182 Posts
Posted Feb 23, 2014
Replied 2 hours later
In my experience, parenting path_tracks has always lead to crashes. I'd rather not open that can of worms. I am trying to set up up to 4 alternate paths per track. Putting 4 paths in each spot would work, but I would prefer not to use so many entities. Ill go with that though if I can't find an easier way.
Avatar
FelixGriffin
2,680 Posts
Posted Feb 23, 2014
Replied 17 minutes later

You'd only need three.

&nbsp; &nbsp; &nbsp;|
&nbsp; &nbsp; &nbsp;1
&nbsp; &nbsp; / \
&nbsp; &nbsp;2&nbsp; &nbsp;3
&nbsp; / \ / \
&nbsp; | | | |

Advertisement
Registered users don’t see ads! Register now!
Avatar
Goldenknighttim
182 Posts
Posted Feb 23, 2014
Replied 15 minutes later

FelixGriffin wrote:
You'd only need three.

```
     |
     1
    / \
   2   3
  / \ / \
  | | | |

```

oh, That would be a lot more effective than what I was imagining.