miguel_o

miguel_o

How to create multiple consecutive commands with ECSx?

Hello, a question, participating in the ECSx (Ship) tutorial, but changing the dynamics of the game. How multiple consecutive commands of the following line should be implemented in the code:

  ECSx.ClientEvents.add(socket.assigns.player_entity, event)

For the purposes of managing both the movements and firing of the ship via orders entered with types from the interface?

Ex.

“move north 10 positions”, “launch 3 shots”, “move west 5 positions”

I’ll look forward for your answer!

Most Liked

APB9785

APB9785

Creator of ECSx

Hi Miguel! When using ClientEvents, try to make each event line up with a single input from the user. So, let’s say you have a button which can be clicked by the user to move north, and one button click always equals 10 “positions” on the map. The ClientEvent in this case would just be :move_north - and the distance moved is only a concern for the ClientEventHandler:

def Ship.Systems.ClientEventHandler do
  ...
  @move_distance 10

  defp process_one({player, :move_north}) do
    y = YPosition.get(player)
    YPosition.update(player, y + @move_distance)
  end
end

Now, if you have a button where sometimes a press moves 10 positions, and other times a press moves 5 positions, the question is: what determines this value? If the value comes from the frontend, such as a slider in the UI where the user adjusts the speed, then we’ll attach that value as metadata with the event:

ClientEvents.add(player_entity, {:move_north, 10})
ClientEvents.add(player_entity, {:move_north, 5})

and the handler:

defp process_one({player, {:move_north, value}}) do
  y = YPosition.get(player)
  YPosition.update(player, y + value)
end

But if the value is based in the backend (stored as a component somewhere) then metadata should not be used. For example, if one button press equals s positions, where s is the SailingSpeed of the player’s ship, simply add :move_north and fetch the value in the handler:

defp process_one({player, :move_north}) do
  y = YPosition.get(player)
  value = SailingSpeed.get(player)
  YPosition.update(player, y + value)
end

Hope this helps, happy to answer any other questions you might have!

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement