Wick Editor with Lua

I’m making a fork of Wick Editor that uses the Lua language for scripts. It’s at https://github.com/pkhead/we-lua. I don’t know how to make the Github Pages work, so for now you’ll have to install it on your computer and run the node npm gulp stuff.
EDIT: The page is here: Wick Editor 1.19.3 (pkhead.github.io)

At the time of this topic's creation:

I have the global APIs as well as the APIs for the Wick objects. So, for example, you can write this in an update script:

self.x = mouseX()
self.y = mouseY()

And the clip will follow the mouse until the application crashes because of rampant memory leaks. In fact, anything with an update loop will eventually crash.

Currently I’m having some trouble with implementing inheritance for the API. And maybe fixing the memory leak. That too.

Relevant files:

  • engine/lib/lua.js (Lua Javascript API minimized)
  • public/corelibs/wick-engine/lua.wasm (Lua runtime)
  • engine/src/LuaAPI.js (Javascript helper functions for Lua)
  • engine/src/base/Tickable.js (Wick class that handles scripts)
3 Likes

I assume that you like to program in Lua… interesting. A python alternative should be also very useful… I haven’t research for an API.

Embedding Python would probably be more practical since Python is more widely used. But I chose Lua because it was made for embedding into applications. So I’m guessing Lua would be more easier to embed than Python.

I figured out how to use the Github Pages so here’s the link to the mod:

Don’t worry I fixed the memory leak. I also added basically every documented API method, and basically all the Wick classes. Except hits, I don’t think that works right now and you can’t call it with options.

I also added an attributes property to Wick objects. It’s how you can put custom data onto objects. It’s a Lua table, so you can put any type of Lua object in the keys or values. I’m thinking of renaming this to attrs next build.

Also I don’t know how to modify the Ace code editor (yet), so it’s still gonna do syntax highlighting and static code analysis like in JavaScript. I’ll try to figure it out. Maybe it won’t be too complicated?

Another thing, when there’s an error it appears 3 times. I should try fixing that.

Here’s an example project:
shiplua.wick (2.3 KB)

2 Likes

I clicked on there, but the following screen appears…
Screen Shot 2022-03-05 at 6.42.14 PM

(I think that I tried Lua a long time ago, like 13 years ago… but it was to try LOVE)

woops it should be .io instead of .com

1 Like

Yup, it worked. Is that a web host service?

Yes, it is owned and maintained by Github.

1 Like

Really? Oh my… I didn’t know that… I started a custom editor a while ago, and I was thinking on buying / rent a web service for that… I think we could work together in that editor… I’ll talk to you later to see if you are interested…

Update:

  • The editor does Lua syntax highlighting and (terrible) syntax error checking. (But at least it’s there.)
  • Moved the console out of the script editor and into a panel, where it rightfully belongs. And learned what the heck React was along the way.
  • I think I wanted to write a third item but I forgot what it was. EDIT: I remember now. I renamed attributes to attrs

Here’s another example project, it’s a tic tac toe game I never finished, rewritten in Lua.
xolua.wick (10.1 KB)

But for some reason it doesn’t work. The rematch button doesn’t appear for some reason. It isn’t my fault this time! I don’t even know what’s happening. Why does the identifier turn into null?

Also fun fact, you can see where I put the working directory on my computer – in the sources tab. Dunno why it names it that. As you can tell I was using Windows Subsystem for Linux for the command line.

Anyway my grind is over so I will sleep. Here lies the non-minified version of lua.js

2 Likes

Ok so i have a funny story. Not sure if any of you would care or understand what I’m saying, but it’s very funny so I’ll post it anyway.

So turns out coroutines created in Lua scripts don’t work because of the way I implemented the lua state class. Which is a big problem.

You see, I wanted it so the code would call luaState.pushNumber(3) with luaState being a lua state object, instead of having to write luaPushNumber(luaState, 3) with luaState being the memory address to the lua state. However to do this I made it keep track of all the Lua states created to map the memory address to the corresponding LuaState class.

Now a problem arises when Lua code calls coroutine.create, where it creates a new lua state. And the JS code doesn’t know. So when the lua code calls a JavaScript function, it doesn’t recognize the LuaState pointer so it uses undefined instead of a LuaState instance. Which will obviously cause problems.

So these are some options I thought of:

  • Make the C code tell JavaScript that a new thread was created. This would mean modifying the Lua source code to do so, and to make it tell JavaScript the thread was garbage collected as well.
  • Make a temporary LuaState when an address that represents a thread is used. This would be a fine solution if I didn’t also keep track of the userdata pointers. However, getting rid of the userdata tracking would mean that the code would have to use peek/poke memory addresses instead of using names.
  • Get rid of all this object-oriented crap just use pointers like a Real Programmer™

Ok so I have calmed down at this point and I thought of a pretty solid solution. It’s basically #2 of the list of options I thought of. It makes a new LuaState wrapper everytime a JS function is called. The only thing that differentiates different states is its pointer, so I only need to store that.

For userdatas, I define a userdata type and give it a name. When a userdata is created, I call it with the name of the userdata type. When JS code is called with a userdata as an argument, I cast it into the expeted userdata type. Isn’t it a very innovative and creative solution? (Not like it’s the only way to do if it were in C code)

Update:

  • Should save the script bytecode instead of recompiling everytime the script is ran
  • Reworked lua.js, fixing coroutines
  • Added more apis
  • Wick object userdatas can now be garbage collected (I had every userdata stored in the registry, preventing it from being gc’d)

Here are some projects I made in this:
A basic platformer engine: platformerlua.wick (3.8 KB)
A quadtree demo (use keys to advance): quadtreelua.wick (13.1 KB)

1 Like

Definitely, most of us will not understand this, specially because I don’t use lua. It is interesting though, and opens new doors for the editor to use different programming languages.

This was the funny part to me, but to be honest, I have to program using both methodologies, and there is a place for both of them… Definitely I don’t see myself doing game dev without OOP.

1 Like

Yep, OOP is pretty useful. But some people say it’s bad. I’m guessing that’s because it’s easy to misuse to add unnecessary complexity and degrade performance.

Update:

I figured out why my tic tac toe game wasn’t working. It was because I defined self + the API functions in the global scope. So after some research and debugging I figured out how to make them local only to the script. (the base wick editor could do it like this. but they don’t because of performance reasons?)

This does change the behavior of global variables though, making it so you can only define them from the _G table instead of writing varname = value (compared to local varname = value). So some of my demos that take advantage of this feature may be broken.

Here is my updated tic tac toe game: xolua.wick (10.1 KB)
Here is a stress test demo: stresstestlua.wick (2.1 KB)
(Fun fact: The stress test runs at 40 FPS in JS compared to Lua’s 20 FPS on my machine. But to be fair, V8 uses a JIT compiler while Lua does not.)

Also I was wondering how Lua and Python compared, so I searched it up (cough and looked at one site) and here’s what I got:

  • Lua is faster than Python
  • The Lua runtime is smaller in file size than Python

I guess it makes sense, since Lua is a simpler language and doesn’t include more than 200 standard libraries.