# Moving an entity around

**URL:** <https://forum.choiceofgames.com/t/moving-an-entity-around/144935>\
**Category:** ChoiceScript Help\
**Tags:** coder\
**Created:** [November 17, 2023, 9:17am UTC](https://forum.choiceofgames.com/t/moving-an-entity-around/144935 "2023-11-17T09:17:29Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [November 18, 2023, 5:13am UTC](https://forum.choiceofgames.com/t/moving-an-entity-around/144935/21 "2023-11-18T05:13:47Z")

</div>

> [@RascaldeesV2](#):
>
> That’s what I was thinking. I haven’t started experiments yet but I was considering something like…
> 
> Monsters starts in X room so we assign X room a value (we’ll say 5 just for easy numbers)
> 
> Player position can be easily tracked (Monster variable = player variable means no more dialogue hints or environmental hints, que meet the monster scene and get nommed) and we’ll say they start in Y (1 for easy numbers)
> 
> When X = Y, player and monster are in the same room
> 
> Monster boyo needs to move between adjacent rooms but not be able to pass through walls. THIS is the part that I’m a little stuck on. But I may have a work around but it’s a bit… icky.
> 
> Let’s say rooms 3, 4 and 5 are all connected since they are, in our imaginary map, different sizes. We could do 3 = 1, 4 = 2 and 5 = 3, use \*rand dice 3 and \*if 1 then our mob moves into room 3, 2 for room 4 and 3 for room 5.
> 
> It’s a bit messy, but also random without being _too_ random. It’s also just a lot of work that there may be a better solution for.

You’ve got the start of your algorithm right there, now try and turn that into code. Don’t get too caught up in the perceived complexity of the system or any limitations of ChoiceScript. You don’t need anything anywhere near as complicated as a path finding algorithm (A\*) here.

Break the bigger problem down into smaller chunks. Make them small enough that you understand how to achieve each individual step in ChoicesScript – the system will follow. I think you’ll be pleasantly surprised how accessible implementing more complicated systems becomes if you approach the problem with the right mindset.

E.g.

1. We need to track which room the player and monster are in, how do we store values for later reference? Variables! Easy!

```choicescript
*create monster_room 5
*create player_room 1

```

1. We need to do a thing if the player plus monster ever end up in the same room. How do we check things? If statements! Nice.

```choicescript
*label player_moved
*if player_room = monster_room
  *goto monster_scene
*else
  *comment normal stuff

```

1. Now it does get trickier here; we need a way to randomise the route, but we don’t know which rooms are connected yet. How do we store information again? Variables! Okay, but each room can be connected to multiple other rooms. How do we associate a room with multiple values? Multiple variables? An array? A string of characters? There’s certainly a few choices, each with pros and cons, but all will do the job.

```choicescript

*comment or...
*create room_1_connections "153"

*comment or...
*create_array room_connections_1 3 1 5 3

etc.

```

1. Doing the randomising! ChoiceScript doesn’t have native loops, but you can repeat stuff using goto and labels. And we already know how to get random numbers in ChoiceScript…

```choicescript
*label loop_select_next_monster_room
*rand target_room min_room max_room
*comment if target_room is in connected rooms; implement via a nested loop? Depends on your data storage.
  *goto next_bit_of_game
*else
  *goto loop_select_next_monster_room

```

I really encourage people to try their hand at these things, it’s a great learning exercise. Think of writing code like writing a recipe. Getting a full dinner on a plate takes a lot of work, but almost anyone can do it if the steps are made small and detailed enough.

---

<div class="post-metadata">

**Author:** ![void\_mermaid](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/void_mermaid/32/199785_2.png) [@void\_mermaid](https://forum.choiceofgames.com/u/void_mermaid)\
**Post date:** [November 29, 2023, 6:15am UTC](https://forum.choiceofgames.com/t/moving-an-entity-around/144935/22 "2023-11-29T06:15:45Z")

</div>

I was intrigued by this concept so I made a map in paint:  
 ![image](https://global.discourse-cdn.com/choiceofgames/original/3X/9/3/935bd63d36392a734aabd4ca5f8591bf06440512.png)

And then I made a mini loop labyrinth-like game:

> **Code for a labyrinth loop mini-game**
>
> ```choicescript
> *title Monster
> *author You
> 
> *create monster_position "D"
> *create mc_position "A"
> *create random_room 0
> *create monster_next_door false
> *create luck 0
> *create fresh_start true
> 
> Welcome to the labyrinth. 
> *label start
> *rand luck 0 1
> *if not (fresh_start)
> *gosub move_monster
> *if (mc_position) = (monster_position)
> The monster enters the room where you are.
> *if luck = 0
> *goto dead
> *else
> It doesn't find you and it moves to the next room. You survived.
> *goto start
>     
> *label next
> You are currently in room ${mc_position}. 
> There is a monster somewhere.
> *gosub check_next_room
> *if monster_next_door
> You can hear a noise coming from room ${monster_position}.
> 
> What will you do?
> *choice
> #Stay and hide.
> *set fresh_start false
> You hide.
> *goto start
> #Go to another room.
> *set fresh_start false
> Alright, which room will you go to? 
> *if monster_next_door
> Remember that you can hear a noise coming from room ${monster_position}.
> *choice
> *if (mc_position = "B") or (mc_position = "C")
> #Go to room A.
> *set mc_position "A"
> *if (mc_position) = (monster_position)
> Boo! The monster eats you.
> *goto dead
> *goto start
> *if (mc_position = "A") or (mc_position = "D") 
> #Go to room B.
> *set mc_position "B"
> *if (mc_position) = (monster_position)
> Boo! The monster eats you.
> *goto dead
> *goto start
> *if (mc_position = "A") or (mc_position = "D") 
> #Go to room C.
> *set mc_position "C"
> *if (mc_position) = (monster_position)
> Boo! The monster eats you.
> *goto dead
> *goto start
> *if (mc_position = "B") or (mc_position = "C") 
> #Go to room D.
> *set mc_position "D"
> *if (mc_position) = (monster_position)
> Boo! The monster eats you.
> *goto dead
> 
> *label dead
> The monster found you, you died.
> 
> *ending
> 
> *comment SUBROUTINE TO MOVE THE MONSTER
> 
> *label move_monster
> *rand random_room 1 2
> *if (monster_position = "A") or (monster_position = "D")
> *if random_room = 1
> *set monster_position "B"
> *goto return
> *else
> *set monster_position "C"
> *goto return
> *else
> *if random_room = 1
> *set monster_position "A"
> *goto return
> *else
> *set monster_position "D"
> *goto return
> *label return
> *return
> 
> *comment SUBROUTINE TO CHECK IF THE MONSTER IS NEXT ROOM
> *label check_next_room
> *if (mc_position = "A")
> *if (monster_position = "B") or (monster_position = "C")
> *set monster_next_door true
> *goto return1
> *else
> *set monster_next_door false
> *goto return1
> *elseif (mc_position = "B")
> *if (monster_position = "A") or (monster_position = "D")
> *set monster_next_door true
> *goto return1
> *else
> *set monster_next_door false
> *goto return1
> *elseif (mc_position = "C")
> *if (monster_position = "A") or (monster_position = "D")
> *set monster_next_door true
> *goto return1
> *else
> *set monster_next_door false
> *goto return1
> *else
> *if (monster_position = "C") or (monster_position = "B")
> *set monster_next_door true
> *goto return1
> *else
> *set monster_next_door false
> *goto return1
> *label return1
> *return
> 
> ```

It works most of the time, but sometimes it gives unexpected results and I don’t know how to fix it. They are not quite noticeable though. I’m of the theory that the creature moves on its own… absolutely not my fault, of course 🤣 I share it in the hope that it will serve as inspiration, at least, of what not to do 😅

---

<div class="post-metadata">

**Author:** ![choicehacker](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/choicehacker/32/127936_2.png) [@choicehacker](https://forum.choiceofgames.com/u/choicehacker)\
**Post date:** [November 30, 2023, 4:25pm UTC](https://forum.choiceofgames.com/t/moving-an-entity-around/144935/23 "2023-11-30T16:25:01Z")

</div>

Huge kudos to @CJW ’ s mentoring skills!

My maze, and its more complex cousin below, are very advanced. I had to build tools to emulate loops, functions and local variables. Unfortunately, CS is not the best language to learn programming (I would suggest Python), so many solutions are more convoluted than they should be. But @CJW is right that you can start small and add complexity as you get more experienced. Good luck!

> [@Two-player 3D game demo: The Dungeon of Lamurloq (first-person POV dungeon crawler)](https://forum.choiceofgames.com/t/two-player-3d-game-demo-the-dungeon-of-lamurloq-first-person-pov-dungeon-crawler/83883):
>
> Hi all! @loudbeat, @plumbeast and I are happy to share our latest demo, [The Dungeon of Lamurloq](https://dashingdon.com/play/sciscidiego/first-person-maze/mygame/). With their last dying breath, your best friend told you about the treasure hidden in the perilous Dungeon of Lamurloq. Are you ready to leave all behind you and try your luck? UPDATE Aug 21: Introducing two-player mode! More details here: [Two-player 3D game demo: The Dungeon of Lamurloq (first-person POV dungeon crawler) - #47 by choicehacker](https://forum.choiceofgames.com/t/two-player-3d-game-demo-the-dungeon-of-lamurloq-first-person-pov-dungeon-crawler/83883/47) The games feat…

[Previous page](https://forum.choiceofgames.com/t/moving-an-entity-around/144935.md?page=1)
