# Help with functions and velocity

**URL:** <https://forum.makecode.com/t/help-with-functions-and-velocity/33107>\
**Category:** Help\
**Created:** [January 14, 2025, 4:00am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107 "2025-01-14T04:00:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Unique](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unique/32/32724_2.png) [@Unique](https://forum.makecode.com/u/Unique)\
**Post date:** [January 14, 2025, 4:00am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/1 "2025-01-14T04:00:20Z")

</div>

Currently working on a football project, but I’m having trouble. I want the function be ran multiple times simultaneously, when it makes the football player NPC follow a specific route. Routes are formatted like this in an array:

```auto
[
[duration, Yvelocity], <— wait for duration, than set velocity
[duration, Yvelocity]
]

```

The way the function is supposed to work is the NPC follows a specific route, but sometimes the velocity will act erratically. And only one route can run at a time it seems.

> **Code**
>
> ![Screenshot 2025-01-13 at 12.46.24 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/5/f/5fc5ac185cc994e5f977b043b771648918acd9e3.png)

> **Positions**
>
> ![Screenshot 2025-01-13 at 12.46.46 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/8/3/8356b14acb72e3b0553c0d96418d9878b4a614b7.png)

RESULT:  
 ![IMG_2531](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/7/b/7bd3ef349cc3bb6464d7aa0dea60e4fc38aa12ac.gif)

@richard or anyone else’s help is greatly appreciated 😁.

---

<div class="post-metadata">

**Author:** ![platformermaster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/platformermaster/32/16102_2.png) [@platformermaster](https://forum.makecode.com/u/platformermaster)\
**Post date:** [January 14, 2025, 4:11am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/2 "2025-01-14T04:11:59Z")

</div>

THE LEGEND IS BACK!!! I HAD THOUGHT WE LOST YOU. BUT YOUR BACK!!!

(sorry this is very unhelpful)

---

<div class="post-metadata">

**Author:** ![platformermaster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/platformermaster/32/16102_2.png) [@platformermaster](https://forum.makecode.com/u/platformermaster)\
**Post date:** [January 14, 2025, 4:12am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/3 "2025-01-14T04:12:12Z")

</div>

Could you share the link? I cant think / properly solve problems without way more info than you’d think is necessary. Well I guess my brain works like that.

Anyways link plz?

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [January 14, 2025, 4:37am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/4 "2025-01-14T04:37:08Z")

</div>

Great mechanic, @Unique !

You’re running into an issue with global variables. Instead of using your variables `routeDuration` and `routeVelocity`, access the array directly.

 ![arcade-screenshot (13)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/b/2/b244904e7045cc57ccc88dc5bd1201ca1efada32.png)

Take a look here:

> **[FB Routes Playground](https://arcade.makecode.com/43620-74631-78934-65924)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

Holler if you still need help.

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [January 14, 2025, 4:46am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/5 "2025-01-14T04:46:12Z")

</div>

A more detailed explanation of why the global variables posed a problem:

> **Open for more detail**
>
> The values for the global variables are set before the pause runs. After the pause finishes, those global variables may have been replaced with different values with a separate call to `run route`.
> 
> You could try moving the block that sets the value of `routeVelocity` after the pause block, and that might work just fine. Still, you risk this same kind of clash when the function is called multiple times.
> 
> The function holds on to a reference to the array until the call has finished. (It’s a parameter, so it’s a local variable specific to the function call.) So, when calling this function multiple times in parallel, accessing the values of the array directly (in other words, accessing the parameter) is the safest way to go.

---

<div class="post-metadata">

**Author:** ![Unique](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unique/32/32724_2.png) [@Unique](https://forum.makecode.com/u/Unique)\
**Post date:** [January 14, 2025, 4:47am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/6 "2025-01-14T04:47:48Z")

</div>

Awesome, thanks @AlexK 😁!  
Switched from using sprite velocity to angles, works perfectly.

> **Finished Mechanic Code**
>
> ![Screenshot 2025-01-13 at 11.36.43 PM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/c/8/c87a29c142df94d8d4cae07fcc4ad90dba113763.png)

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [January 14, 2025, 4:49am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/7 "2025-01-14T04:49:58Z")

</div>

I can’t wait to see where you take this, @Unique ! It looks great!

---

<div class="post-metadata">

**Author:** ![Unique](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unique/32/32724_2.png) [@Unique](https://forum.makecode.com/u/Unique)\
**Post date:** [January 14, 2025, 6:29pm UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/8 "2025-01-14T18:29:32Z")

</div>

Only for a little while, but yes!

This has been a side-project of mine for a while, so I’ve been working on it incrementally. But it’s coming together nicely, so basically, more posts are coming soon.

---

<div class="post-metadata">

**Author:** ![XDgamer8967](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/xdgamer8967/32/15497_2.png) [@XDgamer8967](https://forum.makecode.com/u/XDgamer8967)\
**Post date:** [January 15, 2025, 1:03am UTC](https://forum.makecode.com/t/help-with-functions-and-velocity/33107/9 "2025-01-15T01:03:56Z")

</div>

yo is that techmo bowl
