ability to use third party libraries with behaviours #2326
Replies: 63 comments
-
In gd4 I've copy paste socket.io inside js events , why I can't do same in
GD5 ? If socket.io is global (window.socket) I should have the possibility
to get in all JS events
Le sam. 1 juin 2019 18:15, Todor Imreorov <[email protected]> a
écrit :
… We now have this fantastic ability to create behaviours in the event sheet.
We also have the ability to write js code- straight in the event sheet.
But what use is writing js code for your game, when you cannot import any
third party js libraries? This is literally the biggest weakness in our api
atm imo. Compared to this:
https://www.construct.net/en/make-games/addons/1/javascript
Including third party libraries to extend a GD game is very difficult.
So the request here is simple- make it easy. As easy as typing import
MyClass from "myscriptsFolder/MyClass" in the monaco editor.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1077?email_source=notifications&email_token=AAMX4DUJBRAZ63RSVTYGPN3PYKOCBA5CNFSM4HSA2GOKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GXDICMA>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAMX4DTHHLUWRVGSQJEBVWLPYKOCBANCNFSM4HSA2GOA>
.
|
Beta Was this translation helpful? Give feedback.
-
So while I agree that adding support for "raw" JavaScript file is interesting, there is something that I want to clear:
All of these plugins require JavaScript to be written, aka, they are the same as GDevelop extensions right? And of course it's possible to add any arbitrary JS library/file to a GDevelop extension, that's how Physics or Tween extensions are working 😊 https://github.com/4ian/GDevelop/blob/master/Extensions/TweenBehavior/JsExtension.js#L56 So I think that you're slighlty confusing the fact that GDevelop allow you to use JavaScript (which is not yet the case for C3 - afaik it's being added right now) and the fact that C3 have plugins with third party libraries? Or is there something that I am missing?
So tell me again if I'm mistaken, but it's perfectly possible to re-develop all the extensions, that are on the page you linked, for GDevelop. I know that this is not the main point of the topic, but I want to get our facts straight 😊It's important because there are so much important things to do on GDevelop that I don't want to develop things "because C3 is ahead of GD" to discover later than in fact GD is on the same point (and even more advanced as allowing JavaScript in your events). To get back to what you're asking, is the solution of Bouh potentially usable? Best way to have me work on something for GD is to actually show me a proof of concept using for example a library embedded in a JS event that works. At which point I will have a valid, proven use case to allow arbitrary JS files to be included - which will need rework of the exporters and make things like minification more complex. (Again, I understand that this would be an interesting feature, it's just that I don't want arguments that are biased because it's a dangerous way to prioritise things) |
Beta Was this translation helpful? Give feedback.
-
Said in other words: if tons of JS plugins exist in Construct to re-use JS libraries, that they are done by written plugins in JS and knowing that we can do the same in GDevelop, then the question is:
Question for which there are multiple hypothesis:
If it's 1, we can surely make it easier to write extensions - or see why it's harder and how to improve that. If it's 3, we can enhance the doc. If it's 2, GDevelop is growing so hopefully we'll get more people contributing :) I know that I am a pain, but I really want us to explore all available options before risking to bloat the software. Simplicity is something that is hard to achieve, and supporting things like "import MyClass from "myscriptsFolder/MyClass"" is actually a very complex feature involving JS bundlers, syntax transformers and all kind of things that will make me spend 2 months of my time doing this - while maybe a line in the doc or a way to include files to Extensions made in GD will be enough :) |
Beta Was this translation helpful? Give feedback.
-
Turns out there is already a card on the roadmap: https://trello.com/c/jnT2J2Td/230-add-support-for-embedding-arbitrary-js-files-in-the-game I'm linking the discussion here. |
Beta Was this translation helpful? Give feedback.
-
There is a card yes. Sorry I totally forgot this was requested in the past. The motivation behind this is to make development of extensions in GD more and more enjoyable and encourage the development of extensions. From my point of view the ability to import js libraries is very important for creating extensions. Almost everything I am interested in making an extension of depends on it |
Beta Was this translation helpful? Give feedback.
-
Fair enough, for now I have no better solution than asking you to put the whole file in a JS event but I see the idea. Could be part of extensions that you can create in GD (so basically, that would call addIncludeFile, like in extensions). |
Beta Was this translation helpful? Give feedback.
-
If I create 2 js code events, one after the other, can I call functions declared in event A inside event B? |
Beta Was this translation helpful? Give feedback.
-
Not out of the box because declarations in JavaScript are scoped to the function where they are running (in other words, what you declare in event A stays in event A).
Historically, JS libraries used the third approach, exposing themselves in a global variable. Things have improved with the introduction of JavaScript bundlers, allowing you to require files and bundle everything inside a single JS file that you import in your index.html (this is what is done for GDevelop IDE). This requires libraries to "export themselves" using something called AMD or CommonJS. See https://medium.com/computed-comparisons/commonjs-vs-amd-vs-requirejs-vs-es6-modules-2e814b114a0b for what seems a good explanation. In practice, most libraries are still doing a dual approach of both exposing themselves to be used in a CommonJS environement (i.e: "require") AND can also expose themselves as a global on the window object (this is called UMD: universal module definition. If the library says it support UMD, then it can be used as a global in the browser. For example, shifty.js is UMD) The game engine and GD events/JS code is still using the "good old" approach of having a bunch of files without bundlers or anything - notably because this is simple (for preview it's especially important to be fast) and avoid bundlers adding weird stuff or applying transforms to the JS that would be unwanted. This means that everything is located under So at the end, what does all of this has to do with using libraries in a JS code event? If the library is exposing itself on window, you should be able to:
Let me know if you can get something working this way. |
Beta Was this translation helpful? Give feedback.
-
Thank you @4ian :) I will give this a try tonight |
Beta Was this translation helpful? Give feedback.
-
It works for something simple I wrote. For bondage.js I get this error:
https://raw.githubusercontent.com/jhayley/bondage.js/master/dist/bondage.min.js This is a very old minified compile of I assume this is related to babel, which is used in ALOT of libraries, to compileto a minified file for including in other projects |
Beta Was this translation helpful? Give feedback.
-
Do you have this issue when simply running the preview without using |
Beta Was this translation helpful? Give feedback.
-
when calling bondage.
when usually the case for libraries you import is:
You can try with the js version of |
Beta Was this translation helpful? Give feedback.
-
Actually I get it with a black screen, simply by declaring bondage as
no need to call it |
Beta Was this translation helpful? Give feedback.
-
What if you paste directly the minified content without adding the Can you share the project with me? Will be easier to debug. |
Beta Was this translation helpful? Give feedback.
-
black screen. I will share a project tonight :) Minified libraries seem to be all transpiled to nested functions emulating classes with methods. So you have something like this:
My Observation is that GD's monaco code can't handle that at all. As noted previously, you can't execute a nested function, unless it has been declared like this:
This kind of makes it impossible to import and use the code - even if you copy and paste it. |
Beta Was this translation helpful? Give feedback.
-
I started developing it as a pure js extension and there seem to be no problems loading bondage.min.js that way at all :) I am now thinking of adding some ways to make the pure extension development nicer. Some sort of a way to generate some boilerplate code in a folder would really lower the entry point for new devs The monaco editor coding is currently kind of broken anyway. You can't resize its area, it freaks out when you try to move it in the event tree, grabbing it is hard.. there are all sorts of gui related problems with it, not worth fixing when you can't even import js files. @jhayley sorry for alarming you. I will report any progress about this on GD's trello. Bondage.js in GD5 is on its way ;) |
Beta Was this translation helpful? Give feedback.
-
Now that it seems we are dropping the idea of including JS libs, only would like to add that in the trello board my request was about the possibility to include a JS library with the project. What I was referring to is a resource manager where I can add whatever I want to be included with the project at compile as in GD4. So in case we could add/remove/update files in the resource manager, all we need to do is put a comment in to the JS event //hey, this is depends on this.js and done, no confusion what is broken if something gets updated and doesn't work and all need to be done to update is to replace the lib in the resource manager with the new lib given that methods and functions have not been changed, if they changed then obviously someone need to update the JS event too. But just like with anything contributed to GD source, if there is nobody to maintain it it will break obviously, so the same risks apply. But the benefit is that we don't need to convince anyone about the usefulness of a feature and get an agreement to include it and share it, we can just do it on the forum, anywhere now that we can also export/import custom behaviors. Regarding the benefits being able to include libs with project and use them from JS events is simply that we do not need to touch the source of GD and compile GD from source. I'm ok with scripting gameplay, using methods and functions provided by 3rd party libs but the source of GD is alien to me and I also don't really have the motivation and interest to play with the source of GD to dig deep and learn how everything is works, but I could play around with some libs using the JS event. Maybe not optimal, but this is the only way I would consider to do it personally. My point is, not every GD user will become a JS dev eventually who seek the opportunity to improve and extend the source of GD with new features. JS events could be a nice play ground for those who just don't want to play with the source especially now with the ability to export/import behaviors and when we also be able to expose properties that can be changed in behavior settings, that could be huge with the ability to also include 3rd party libs. And as I mentioned, we don't need anyone to agree we can just share it and people can choose to use it or not. But there is an other bigger problem, the order of execution and scope. The ability to include JS libs on it own does n''t worth anything if the JS event is not being executed at the right time in the scope, so code generation also need to be more optimal, maybe even add customization by allowing to set order of execution of our JS events and the lib we include as I mentioned on Trello. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Oh no, I don't think we should drop the feature. In fact I strongly believe that having it will encourage people to write js in the event sheet, combine it with events. Its useful for prototyping without having to write tons of boilerplate code. For extensions that come bundled with gdevelop, it's better to write them as pure js extensions. I agree with @Wend1go and also am thankful, because his extensions served as better examples for me to learn howto set up an extension. Especially the screenshot extension imo is an excellent start to learn. There are still some things that can be done to encourage people to write native js extensions. The biggest one is imo having to compile gdevelop and run it in dev mode. All people who only want to make games with GD (not compile it) can't write pure js extensions or use external libraries without ugly workarounds. This sort of gives off the impression that Gd's js capability is very limited, which is strange for a html5 engine |
Beta Was this translation helpful? Give feedback.
-
I really like the idea of putting source code through the import of a file, indeed it is possible in GD4. I was able to make a proof-of-concept with socket.io. But well it's clearly not a priority, people who want this feature are not many. And we're getting around that lack for now. I made a pure JS extension too (Video), it took me a while to understand the role of each file despite the fact that they are not many. With the ability to make extensions, behaviors, Javascript code and pure JS extensions, I sometimes wonder where I will put my code in GD. |
Beta Was this translation helpful? Give feedback.
-
The other thing that is kind of a pain point, how the hell do I read a json file? gives me the result of a zero. Is it easier to do in a js event? We can't import or require there also note that when I want to hold it as a string variable - the expression editor has issues with the quotes in the json file contents (due to defaulting to
We can't have Now imagine I have a dialogue json file with thousands of lines I want to paste anywhere in gdevelop. This sort of re-formating is hardly something the user should be made to do, but GD requires it for some reason - making it stupidly hard to keep json data- even as a string in the expression editor. I tried the several actions to load a thing from a file, they all return a zero when giving them a relative path This stuff is easy and works when written as standard JS, but inside GD it kind of doesn't let you do anything or is harder If we have a way to include other types of files and use them in the game - pass them to actions? This is a hurdle that prevents me from getting bassoon tracker's mod files to be parsed by GD for example. GD has no idea what a mod file and and that can't be declared as a new resource type via the js extension api if I am not mistaken. Would be nice if we can bundle any file with the game, add it as a resource and when not recognised treat it as binary data or something. Unknown file type icon. Right now dealing with any non image/video/sound resource is a bit ridiculous |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Nice :)
You have to explain more the context about this array. What does it represents, what do you want to expose to the user? What is this array doing, what is it even composed of?
Beware of creating a Yarn behavior just because it's easier, I don't know the context but let's avoid to fit everything into behaviors if it's not actually related to an object.
I can add a resource type once we have something working. |
Beta Was this translation helpful? Give feedback.
-
In the case of the dialogue line being of type "Options", I get a string array with the options I can give to the player to use as an answer to a question. The string array needs to be passed to gdevelop's event sheet, where it can be used to create answer buttons or whatever the dev desires.
Once we have a json file resource type, I guess the user will be able to load the dialoguetree data from it - select it from a list of json files. And launching yarn-editor could be handled similar to how jfxr is launched to edit sound effects? I am currently trying to figure out what the best way to bundle yarn-electron is, so it can be used in another electron app such as gdevelop. |
Beta Was this translation helpful? Give feedback.
-
My first idea would be to store it in a variable structure maybe?
Could be yes, though it's not related directly to a resource contrary to jfxr/piskel so we need to think "what should be the entry point for the user for such an editor?"
Probably these will be stored as json resource type yeah. |
Beta Was this translation helpful? Give feedback.
-
how does one return a variable structure from a js extension to the event sheet? Is that something that needs to be created via gd.js or is it a standard js object? Will need to dig into it tonight :) |
Beta Was this translation helpful? Give feedback.
-
You can use Two things:
With this, you should be able to convert most JS objects into variables. Note that this should be a last resort to expose things to the events. Prefer specialized actions/conditions if possible to interact with things in memory. |
Beta Was this translation helpful? Give feedback.
-
@4ian so my idea for handling dialogue state - when loading and saving the game is to store it in a global variable. How does one convert the structured variable back to an object? |
Beta Was this translation helpful? Give feedback.
-
should I do: |
Beta Was this translation helpful? Give feedback.
-
Yeah, Depends on what you want to store in the global variable. |
Beta Was this translation helpful? Give feedback.
-
We now have this fantastic ability to create behaviours in the event sheet.
We also have the ability to write js code- straight in the event sheet.
But what use is writing js code for your game, when you cannot import any third party js libraries? This is literally the biggest weakness in our api atm imo. Compared to this:
https://www.construct.net/en/make-games/addons/1/javascript
Including third party libraries to extend a GD game is very difficult.
So the request here is simple- make it easy. As easy as typing
import MyClass from "myscriptsFolder/MyClass"
in the monaco editor. Or as easy as an event sheet action.WIthout third party libraries, our extension ecosystem is not going to get any of the cool things happening in opensource html5 gamedev world.
That is kind of where construct3's js is still ahead of gd. Most of their extensions are based on some library that the dev simply exposed to construct:
https://www.construct.net/en/make-games/addons/plugins
Beta Was this translation helpful? Give feedback.
All reactions