jarkonik / jarkonik/bevy_scriptum
Entity local methods
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 171
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
If two or more entities are spawned with scripts using the same runtime the state of the runtime is shared between them. This makes it hard to create a function with different logic per entity. Tested in Lua.
The following or similar should be supported:
`dog.lua`
```lua
function speak()
print("woof")
end
```
`cat.lua`
```lua
function speak()
print("meow")
end
```
If two entities would be spawned - one with `cat.lua`, one with `dog.lua` - both would either print "woof" or "meow" depending on which script has been evaluated first. That is because function `speak` would get redefined by every script eval.
A solution has been proposed to keep functions in namespaces based on entity id by @zzhgithub
In lua this could be accomplished by doing the following:
```lua
local ns = "entity_" .. entity.index;
_G[ns] = {}
_G[ns].speak = function()
print("woof")
end
```
However, with the current state of the API as of v0.8.1 there is no easy way to call the function `_G[ns].speak` from Rust. `bevy_scriptum` users would need to retrieve the value of `_G[ns].speak` somehow and call it by themselves by acquiring engine handle using `with_engine_mut`. Probably this should also be possible using `call_fn_from_value` (?)
This also needs to be generic enough to support every currently implemented runtime and whatever may come in the future.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the API around with_engine_mut and the proposed call_fn_from_value, then compare how the currently implemented runtimes expose function values. The work is complete when entity-specific functions can be defined and called independently through a generic API across the supported runtimes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua, rust
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100