Jump to content
Malaka135

Speedometer Tutorial & Car Chassis' Top Speeds

Recommended Posts

NOTICE :

I am including images & gifs in this tutorial but I reduce usage of visual aids as we progress down the steps.

As you perform certain steps such as placing tiles and accessing menus, I will assume you understand that step and won't clutter this tutorial with too many visual aids.

Sorry ?

Game Logics Required :

  • Spawn Point - Player needs to spawn somewhere ?
  • Player Role - To handle Player OnConnect & OnDisconnect
  • Global Storage - To store our connected Players
  • Custom HUD - To draw the speed
  • Time Trigger - To calculate and update the speed

1st Step - Adding our Game Logics : 

Game_Logics.png.6b3c5f32c19194603d963f1cfb831f32.png

Go ahead and place down one of each "Required Game Logics" listed above. We will work on them one by one.

Spawn Point should be already placed down by default. If you do not have one, just place one down.

2nd Step - Configure our Global Storage : 

We don't need to touch any of the properties aside from it's scripting and name. Just make sure "Enabled" is also checked. I named mine "Connected_Players" since we are using this to store our connected players.

When you click on "Edit" for the Script, it will bring you into the scripting window. On the left side you will see all the available tiles. We are only interested in VARIABLES. Click on VARIABLES.

Connected_Players_1.png.f21ffcb624b8356725612123b64cd589.png

 You should see on the right side a button titled "CREATE NEW VARIABLE". Click that.

Connected_Players_2.png.013498dbe97d0dec9612ee173b7d1355.png

A new window will appear labeled "CREATE VARIABLE". We want to name this something that would describe it's purpose. In this case, I named mine "ARRAY Connected Players".

It is IMPORTANT to check "Array" since we want this global storage variable to store multiple objects. Go ahead and click "Confirm" to create our new variable.

You might be wondering, "What is an array?". Essentially an array is a list of items which each item has a corresponding Index.

Index always starts at 0 so if we have an array with 5 items inside, the size of the array is 5, however the 5th item in the array will be Index 4. This is because the first item is at 0, the second item is 1, the third item is 2...and so on.

3rd Step - Configure Player Role : 

This isn't important, but I went ahead and named our Player Role game logic to "Player".

Please ensure that "Spawn Place" is setup. If not, click on that little dropper and then click on our spawn point to link them. Make sure "Enabled" is also checked.

We also want to edit the script for this game logic, so go ahead and click on "Edit".

Just like our previous step, we want to take a look on the left side and search for EVENTS. Click on EVENTS to see what kind of events we can handle with this game logic.

We are only interested in ON PLAYER CONNECTED and ON PLAYER DISCONNECTED. Go ahead and drag each one to the right into the empty space.

GIF_Player_Role_Events.gif.cd14e9e914914c207d896331bd52d9c8.gif

We now have two events we want to handle. Starting with ON PLAYER Connect, let's add that player into our Global Storage variable we made in Step #2.

Under ARRAY, we want to find a script tile labeled PUSH array. Go ahead and drag that just underneath our ON PLAYER CONNECTED tile. This should link them together like a puzzle.

PUSH essentially adds a new object at the end of our array. This tile has 2 empty slots which wants to accept: Array and Object.

Under VARIABLES, you should see THIS, Connected Players(if you named your global storage to this), GAME, and GAME SET. We want to click on Connected Players(or whatever you named yours).

You should see two tiles: The black tile with two horizontal bars sticking out the left side is our array we made earlier. The blue tile which says "SET" is used to overwrite/store our variable.

Since PUSH automatically saves the array, we are not interested in the "SET" tile for this example.

Go ahead and drag the black tile into the matching empty slot labeled "Array" in our PUSH. This is to tell PUSH to store whatever object we give it into our array.

Object is still empty, we want to PUSH our Player into our array. You might be wondering, where is "Player"? Player is provided to us by the ON PLAYER CONNECTED event tile.

Go ahead and drag the red tile PLAYER into our empty slot labeled "Object". 

What we just accomplished was setting up a script to add our players to our array of connected players. This is very important for a later step so we can point to our players.

Lastly, we want to handle ON PLAYER DISCONNECT. Just like we put players into our array on join, we want to remove them from our array when they leave.

Under ARRAY, look for the SPLICE tile. Go ahead and drag that underneath our ON PLAYER DISCONNECT tile. This tile takes in 3 tiles: Array, Index, & Count.

This tile will essentially remove an object at the index we instruct it to.

Instead of having to click on VARIABLES again to find our array which we already placed, we can copy it by dragging it while holding down CTRL on our keyboard.

Go ahead and supply SPLICE with our array. Now we need to tell it the Index and Count. Index is the number corresponding to our Player we want to remove and Count is how many objects we will remove starting at the Index.

Go ahead and set Count from 0 to 1, since we only want to remove one single Player from our array.

Index is a bit tricky because we don't know where our PUSH tile stored the Player in our array. Especially if we have multiple players, we wouldn't know who is what at what Index.

Using the INDEX OF tile found under ARRAY, we can search the array and retrieve the Index of our disconnecting player.

Go ahead and drag the INDEX OF tile into the Index slot of our SPLICE tile. Now CTRL + Left click drag our array into the empty Array slot of our INDEX OF.

Lastly we need to supply INDEX OF the Object it needs to search with. Go ahead and drag Player into this empty slot.

Our script should look like the image below.

Player_Role_Script.png.b0b0ce14a4e2e719c6bfd7c301f79b91.png

Edited by Malaka135
  • Like 1

Share this post


Link to post
Share on other sites

4th Step - Build our Custom HUD : 

We want to be able to display information to our players. We can do this by using the Custom HUD game logic. 

Make sure the Custom HUD's "Enabled" is checked and also "Visible To New Player"(not sure if necessary but doesn't hurt to have it on).

We won't be scripting for this one, instead we are interested in the "Edit" button of USER UI. Click that.

You should see something very familiar. This is our HUD. On the top right we are presented with 3 possible items to add to our Custom HUD.

We are only interested in TEXT boxes so ahead and click it once. I won't go into much detail here about designing HUDs and various options, we are just interested in the ID/NAME.

Go ahead and drag the newly created box wherever you want onto the screen.

The important step is to name our new TEXT box something we will recognize. For mine, I named it "TXT_SPEED".

Since our speed can potentially be a very big number, and we wouldn't want to run out of space, I'd recommend changing the Width & Height to enlarge our TEXT box.

I personally set Width to 500 and Height to 100. I also set SIZE to EXTRA and changed the color to red. What you do with your HUD is entirely up to you. Feel free to customize it however you'd like.

Go ahead and clear the default "text" since we want it to be blank initially. We are going to change the text as we refresh the HUD using our timer in the next step.

5th Step - Configuring our Timer Trigger to Calculate Speed & Refresh our HUD : 

We want to update the HUD and also calculate our speed to set the TEXT box we made earlier

The properties found in our Timer Trigger are very important. Since we are looking for a fast refreshing HUD, we can set TIME to 0.03. This is the lowest number allowed at the moment.

TYPE should be set to DURATION IN REAL TIME SECONDS.

REPEAT COUNT should be set to -1.

And also make sure Enabled is checked ? 

TIME is how long the timer would wait before triggering. This is in seconds, so you can imagine 0.03 is very short period of time. We want this because we want to trigger this timer as many times as we can within a second.

REPEAT COUNT is how many times the timer should run. Since we want this to run infinitely and never stop, we can use -1 to instruct it to repeat indefinitely.

Let's go ahead and jump into Scripting so we can now tell the timer what to do.

Under EVENTS, we are interested in ON TRIGGER. This means that every 0.03 the Timer Trigger will....you guessed it... TRIGGER!

A important thing to know is HUD is drawn and refreshed independently for each Player. This means we need to update each Player's HUD separately. We can achieve this easily with a loop.

Under LOOPS, look for FOR EACH. Go ahead an drag that under our ON TRIGGER tile. We have one empty tile to supply for our FOR EACH tile, go ahead and put our connected players array.

So the idea here is FOR EACH Item in our array, this will run the script tiles we place inside the FOR EACH tile. Go ahead and click on Item so we can rename it to something nicer like "Player".

Now we only want to update the HUD on our Player only IF they are in a vehicle, otherwise we wouldn't want to run any code at all. We can achieve this using the IF tile found under LOGIC.

This is what we call an IF STATEMENT. Notice the empty tile to the right of IF? That is our condition. If our condition is TRUE than the code inside our IF statement will run. If it is FALSE, then it won't run.

TRUE & FALSE are booleans but we don't need to worry about that. For now, just know that anything put into the IF statement would always return true unless it is 0 or empty.

For our case, we want to make sure our Player is in a vehicle. We can do this using the GET CONTROLLED ENTITY tile. 

Did you know you can search tiles? Well this will be useful for this step since finding our tiles might be a bit hard. You can search by typing into the "TYPE HERE" field located on the left side of our script window.

Once you find GET CONTROLLED ENTITY character, go ahead and place it into our condition empty slot of our IF statement. The character we are getting the controlled entity of is our Player.

If you renamed Item to Player, you would find that above in our FOR EACH tile. Go ahead and drag that down into our GET CONTROLLED ENTITY tile.

You might be wondering why we are using GET CONTROLLED ENTITY to fill in our missing TRUE/FALSE value. This is because the GET CONTROLLED ENTITY tile would actually provide us with the Entity our Player is currently controlling which can either be NULL(as in player is not currently controlling an entity) or Entity(in this case the vehicle the player is controlling). NULL will translate to FALSE so we will be able to avoid running code if our player isn't in a vehicle.

Now we want to be able to calculate our Player's vehicle speed. Let's make a LOCAL variable so we can name it and be able to resuse it throughout our code. 

Under VARIABLES, you will see two tiles aside from the CREATE button. The first one is a regular variable and the second one with the two horizontal bars is for arrays. We just want the regular LOCAL variable.

Go ahead and drag one inside our IF statement tile. You can click on Variable to rename it to something that would make sense to us. Since we are going to use this one to store our vehicle, we can name it "Player's Vehicle".
We already made the code to GET our player's vehicle above for our IF statement's condition. Go ahead and CTRL + Drag that tile into our Default Value empty slot.

This means our variable named "Player's Vehicle" is now going to be GET CONTROLLED ENTITY of our Player. Essentially we now have a variable which is our vehicle.

To calculate the speed, we want to get the velocity of our vehicle. There is a tile for that which is called GET VELOCITY Vehicle. This will give us a VECTOR of the vehicle's velocity.

I would go into much detail about VECTORS but think of it as 3 numbers: X, Y, & Z. To put it simply, think of a graph, but add a height to it. X and Z stand for our horizontal plane and Y is for height.

For the vehicles speed, we only care about X and Z for now.

Let's make 3 new LOCAL variables: one for X_Velocity, one for Z_Velocity, and one for Speed. I named mine respectively.

For our LOCAL variable X_Velocity we want to get the X velocity value. We can use the GET X Vector tile for this.

Go ahead and drag a GET X Vector tile and place it inside the DEFAULT VALUE empty slot. Now we are missing the Vector for which it will get X from.

Go ahead and drag a GET VELOCITY Vehicle tile into our GET X Vector's empty slot. Lastly, we need to tell GET VELOCITY what vehicle to get the velocity of.

We already defined Player's Vehicle as a LOCAL variable above so we can go ahead and drag our local variable into our GET VELOCITY tile.

Repeat the same steps above for the Z_Velocity. Make sure to use the GET Z Vector tile instead.

Now for a bit of math, using both our X and Z velocity variables, we want to come out with a result....our speed.

The algorithm to figure this out is simple, we want to square X and square Z and add them together.

To script this, its always best to start at the last step and work our way to the first step, this just makes placing tiles less confusing.

Since we need to add our `not yet` squared X & Z values, let's drag over a Addition tile found under NUMBER. You can't search for this tile. Look for the tile with two empty slots with a "+" in between.

Place this addition tile in the DEFAULT VALUE of our LOCAL variable Speed tile. Now we need to square our X and Z.

Find the POWER tile and place one in each side of the addition tile. Base is the number we are squaring and Exponent is the "to the power of" value. Since we want to square, we will set Exponent to 2.

X_Velocity and Z_Velocity can be placed into the Base slots of each POWER tile.

Great, now Speed should be calculating using the following logic:  ( (x^2) + (z^2) ) = speed.

Finally we just need to SET TEXT of the TEXT box(WIDGET) within our custom HUD to the speed we just calculated.

Find and place down a SET TEXT Text widget tile. This tile wants us to supply it with a Widget, which is our TEXT box.

To provide the SET TEXT tile a Widget, we need to GET WIDGET Custom UI tile. Place this tile within the empty Text widget slot of our SET TEXT tile.

Notice that the Custom UI empty slot has "NONE (GAME LOGIC)" with a little dropper to the right. This is expecting a CUSTOM HUD game logic. We can click that dropper and then click our CUSTOM HUD game logic that we physically placed into the world during the first step.

Widget ID will now have options for you automatically. Go ahead and click on "NONE" and you should find your TEXT box you named earlier. Select it.

Let's provide it with the Player for which it should get the TEXT box of. Remember, the HUD is independent and unique for each player, so we must instruct it using the Player tile from our FOR EACH loop.

Lastly, we are missing what the SET TEXT tile should set the text too. We can drag and drop our LOCAL variable Speed.

Timer_Trigger.thumb.png.10f8fc94e5e6f13ba3f0fa7c936c0836.png

 

That is it! All you have to do is launch the game using F5 and test it out. Remember, it will only work if you are in a vehicle so you should probably make one real quick ?

Test_Drive.gif.750081db5569d47f8fe88a6145b52ee1.gif

Edited by Malaka135
  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

VEHICLE TOP SPEEDS

Car Chassis

1 Engine - 477.5

2+ Engine - 1084

Large Car Chassis

1 Engine - 139

2 Engine - 594.5

3+ Engine - 941

Race Car Chassis

1 Engine - 812

2+ Engine - 1839

Off-Road Car Chassis

1 Engine - 477

2+ Engine - 1083

 

NOTES : 

  • The speeds are calculated using the script explained above.
  • I am rounding off to the nearest 0.5
  • Car Chassis and Off-Road Car Chassis are almost identical, however Off-Road is very slightly slower.
  • Large Car Chassis is the only one that benifits from a third engine.
  • No difference found between Steam vs Ylandium engines.

 

 

Edited by Malaka135
  • Like 1
  • Thanks 2

Share this post


Link to post
Share on other sites

This was really helpful.  One question, though.  Could the Get Vector and Set Vector functions be used to throttle the speed of a vehicle?  For example, let's say a race had a "tar trap" that, if one went through it, would slow your vehicle down for a specified period.  Could this be done by somehow affecting vectors?  Obviously, it would be better to affect speed directly, but I don't see a way to do that.

Share this post


Link to post
Share on other sites
1 hour ago, belrathius said:

This was really helpful.  One question, though.  Could the Get Vector and Set Vector functions be used to throttle the speed of a vehicle?  For example, let's say a race had a "tar trap" that, if one went through it, would slow your vehicle down for a specified period.  Could this be done by somehow affecting vectors?  Obviously, it would be better to affect speed directly, but I don't see a way to do that.

Nevermind, I found the answer to my question in another discussion.  The approach is to simply use scaling with velocity.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×