Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: Stinkhorse on January 09, 2016, 12:34:35 AM

Title: Implementing Movement Between Points on an Array [SOLVED]
Post by: Stinkhorse on January 09, 2016, 12:34:35 AM
Hi there, I'm a little nervous to ask this because it seems like it should be pretty basic, but I'm having some trouble understanding how to set up an array, create a position variable from its various contents, and then use that information to move a player between the set positions on a button press.

I'm very new at this, I  started using Unity a few weeks ago and I was so bad at understanding the syntax and calls for scripting that I opted into playmaker. All of that might have been a foolish decision but in for a penny, in for a pound.

I've attached a picture of what I'm trying to build for reference. Each of the tiles would be a spot the player can move to and they would move one space per button press.

Feel free to give me an explanation that's as ground level basics as you like because my level of understanding is poor enough that reading the manual for Playmaker is a bit greek.
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 09, 2016, 01:44:21 AM
Not sure I totally understand the question but to store positions in an array you would create an array and add Vector3 variables to it to store the positional data.

When it comes to moving the player to one of the positions there's several ways you could do that depending on what kind of controls you want-

You could make the player a navmesh agent and use that position as a destination for the agent-

You could use the itween move actions to move the player to the position

You could add force to the player in the direction of the position

You could use a controller move action to move the player if it has a character controller-

There's many ways to do it- just depends on what you want  :)
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 09, 2016, 02:02:00 AM
Ah awesome! Let me see if I can explain this better.

Each of those white floor tiles is a game object, and the player character should move from tile to tile on a button press. So if the player presses right the character will check his current tile, find the next tile in the array, and then lerp from his current tile to the next one in sequence. Every move is absolute, like a piece in a board game.

I have a general idea of the implementation I need, but what I don't understand is how to create a variable referencing the array. All the variables I've made so far require an object reference, but the array itself isn't an object, it's a script on a game object.

I think I'm simply missing or misunderstanding some fundamental aspect of Playmaker, and likely Unity itself.
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 09, 2016, 03:50:18 AM


You referred to array scripts- you know Playmaker 1.8 can use arrays as native variables- so you can do array lists right inside of Playmaker- for additional array+hash table functions or for older versions of Playmaker install the free add-on Arraymaker-

If the tiles are Game Objects you can store those in an array and use those to move too as well-

If you use arrays in Playmaker- you would store your game object or positions in an array- then you would use an array GET action to retrieve something from the array-

So basic workflow-

Store items in array-
Retrieve from array-
Save it in a var-
Do something to/with it

Hope that helps
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 09, 2016, 11:30:57 AM
Yeah I think that does help, thank you!

I'll try implementing it and report back.
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 09, 2016, 06:49:17 PM
Whelp I'm still completely stuck.

I can build the array, I can create a 'get' action, but I can't figure out how to take a step from there. A lot of the fields in the various actions that want variables don't populate when I open them up. I suspect this is because I don't have the right type of variable for it.

I've created a local variable on my player called Current Position, but I can't figure which action would allow me to say:

1. Get the list of objects from the Tile Array
2. Find the next Tile in the sequence from the one the player is standing on
3. The player should now translate to his Current Position +/- 1 depending on the button press.
4. Update the Current Location variable with the new position.

My next step is going to be building a way too complicated state machine that moves the player forward and backward by specific increments for every single tile, rather than the way I know it SHOULD be done with the array and a set of variables.
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 09, 2016, 07:21:12 PM
I see- I'm in the business of creating Playmaker tutorials  :)

I'd be glad to kick out a fast free array tutorial-

Just let me know what it is about arrays and the process that you don't understand and I'll try to cover that-
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 10, 2016, 03:12:03 AM
Well I suppose it's a combination of arrays, pulling information, converting it into a variable and then using that variable to do something else.

How do you even know that the variable has the information?
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 11, 2016, 02:54:23 AM
I made a video tutorial going over the basics of arrays in Playmaker 1.8 hope its helpful https://www.youtube.com/watch?v=yOZK8yr3IKo (https://www.youtube.com/watch?v=yOZK8yr3IKo)  :)
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 13, 2016, 02:20:55 AM
Fantastic! I'm going to watch at least part of it tonight, I'm sorry I didn't see this post sooner mdotstrange. I'll try and implement based on what I learn from it and report back.

Really thank you so much for going to such lengths to answer a beginner question. I'm very grateful!
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 13, 2016, 03:03:36 AM
First wide-eyed-knowledge moment:
"So we're going to make a global variable array so everything can access it"

I did not know that was a variable type.

Back to the vid...
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 13, 2016, 05:43:29 AM
No prob- yeh I didn't know about global variables and even multiple FSM's on a single object until I had been using Playmaker for about a month  :)
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 13, 2016, 09:40:50 PM
Sat down to finish watching the video and implement along with it, but I've discovered I don't have "array" listed as a global variable option on the drop down. I also don't have Enum which is also on your list. Am I on the wrong version of Playmaker, and is there a way to update it without rebuilding my entire project?
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 13, 2016, 09:45:49 PM
Ah, I was on 1.7. Updating to the beta!
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 13, 2016, 10:22:13 PM
If you want to work with arrays and 1.7 and earlier you would use Arraymaker- its a free add-on- its a little more work to use than the native arrays in 1.8 but there are more actions for Arraymaker at this time

In the video I'm using 1.8 and the native arrays since they are new and a much more elegant solution if you don't need the additional actions that Arraymaker has available-
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 14, 2016, 12:11:48 AM
It worked! To a point.

I've dynamically pulled the Tile Array by getting my tile objects. They came in backwards starting with Tile7, so I used another state to shuffle them around using an Array Reverse. Next I've set up the global variable of Current Tile by associating it with element 2 (Tile3) in the array. I then tell the player character to move it's position on start to Current Tile and then update that variable.

That all works!

Where I'm stuck now is getting the player to move between tiles. I want to lerp the player between the vertex variable of CurrentTile, a the vertex variable of NextTile, but I can't seem to find a way to relate the two with one another via the array. I'm trying to use Array Get Next, but it spits out an Int, and doesn't seem to want to accept anything else. Once again I feel like I'm missing something basic.

I know what I have to make happen, somehow translating that int into a vector2 position, I just don't know the tool to use to make it happen.
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 15, 2016, 12:00:06 AM
Just got home from the gym and am starting to work at the conversion problem. I've created a loop event in my game controller to run through the contents of the array. I'm really stabbing blindly here, but based on your tutorial video MDotStrange I might be able to create a variable off of the listed contents rather than just selecting a specific tile, as I did with Current Tile.

I need an int (which slot in the array)
I need a game object (which tile)
And I need a vector (the tile's location)

I feel like the solution is no more than three feet away...
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 16, 2016, 12:15:45 AM
IT'S WORKING! Partially.

I started walking the problem backwards, reasoning out what variables I was going to need to move the character to another space. When I got to the point where I was looking at what the move action would be I realized that iTween's Move To action would accept a GameObject. That meant that one of the three variables I thought I would need, a Vector, wasn't actually needed.

I started by building the Game Controller FSM. I collect the Tile Game Objects to set up the TileArray, associate a slot on the Array (#2, which holds Tile 3) with a Game Object (CurrentTileObject), then associate that Game Object with an Int variable (CurrentTileInt), and finally establish NextTileInt as just being 1. I then used iTween to snap the player to the variable CurrentTileObject. He popped right over to it. So far so good.

I then spent the next three hours building and rebuilding the actual Player movement script. On a key press the script does an Array Get Next and starts with CurrentTileInt and ends on NextTileInt. It finishes by associating NextTileInt with NextTileObject. The next state adds the CurrentTileInt to NextTileInt, sets CurrentTileInt to itself immidiatly after, then do an Array Get to associate CurrentTileInt with CurrentTileObject. The last state is another iTween, which now moves the player from wherever they are to the new position of CurrentTileObject.

I had the FSM done about two hours ago, but when the player moved, he would get to the correct tile, and then snap back to his original position. I reworked the whole thing a dozen times or more, trying a bunch of configurations, and getting the same result every time. I was pulling my hair out until a friend who had given me some early help asked if the script he gave me to run the player early on was still active. Sure enough, it was, and once I unchecked that the player moved and stayed in place.

Of course the next thing to do was to reverse the process. I figured I could use a -1 for the NextTileInt to move back as space on the Array. Wrong. I get locked in the first Array Get Next state.

I'm not entirely sure how I'm going to fix it, but this is a huge step forward!

Thanks again Mdotstrange!
Title: Re: Implementing Movement Between Points on an Array
Post by: mdotstrange on January 16, 2016, 01:22:16 AM
Sounds about right  :)

No problem- if you have any other tutorial requests let me know- always looking to add new learning stuff to the Playmaker scene.
Title: Re: Implementing Movement Between Points on an Array
Post by: Stinkhorse on January 16, 2016, 01:34:24 AM
YEAH!!! Movement is working!

I was able to dump an entire state, I didnt need to do an Array Get Next, or the NextTileInt. I was able to flush a bunch of variables as a result.

I ran into the problem of my math increasing with each key press at the last tile in the array, so even though the player couldnt move past it, if you wailed on the button while at the wall you would have to press it an equal number of times to get moving in the opposite direction.

Next step was a check, so a Bool saying you are or arent at the wall. another state to circumvent the whole movement system if it was true, and an action on the opposite movement direction to deactivate the AtWall Bool. I started by using the Array Get's Out of Range Event to fire off the Bool activation, but that proved problematic because it wouldnt activate until the player tried to move beyond the Array. So you would have to get to the wall and then try to move out of bounds before it activated. This let the counter get one number higher than it was supposed to, meaning one extra button press to start moving back the other way.

The solution here was to learn about Int Checks, and then use that, set to the last tile, to fire off the Bool instead of using Out Of Range. Dirt simple.

I'm incredibly excited, overly proud of myself, and super exhausted at the same time, I want to leap right into camera stuff, but a cursory glance suggests I'll be learning about sending info between FSMs, and while that might be easy it's well beyond my energy levels at this point.

This problem is officially SOLVED at this point.