this post was submitted on 15 Aug 2024
2 points (100.0% liked)

Perchance - Create a Random Text Generator

448 readers
14 users here now

⚄︎ Perchance

This is a Lemmy Community for perchance.org, a platform for sharing and creating random text generators.

Feel free to ask for help, share your generators, and start friendly discussions at your leisure :)

This community is mainly for discussions between those who are building generators. For discussions about using generators, especially the popular AI ones, the community-led Casual Perchance forum is likely a more appropriate venue.

See this post for the Complete Guide to Posting Here on the Community!

Rules

1. Please follow the Lemmy.World instance rules.

2. Be kind and friendly.

  • Please be kind to others on this community (and also in general), and remember that for many people Perchance is their first experience with coding. We have members for whom English is not their first language, so please be take that into account too :)

3. Be thankful to those who try to help you.

  • If you ask a question and someone has made a effort to help you out, please remember to be thankful! Even if they don't manage to help you solve your problem - remember that they're spending time out of their day to try to help a stranger :)

4. Only post about stuff related to perchance.

  • Please only post about perchance related stuff like generators on it, bugs, and the site.

5. Refrain from requesting Prompts for the AI Tools.

  • We would like to ask to refrain from posting here needing help specifically with prompting/achieving certain results with the AI plugins (text-to-image-plugin and ai-text-plugin) e.g. "What is the good prompt for X?", "How to achieve X with Y generator?"
  • See Perchance AI FAQ for FAQ about the AI tools.
  • You can ask for help with prompting at the 'sister' community Casual Perchance, which is for more casual discussions.
  • We will still be helping/answering questions about the plugins as long as it is related to building generators with them.

6. Search through the Community Before Posting.

  • Please Search through the Community Posts here (and on Reddit) before posting to see if what you will post has similar post/already been posted.

founded 1 year ago
MODERATORS
 

It creates objects for the import, but when accessing it it throws the error Cannot read properties of undefined (reading 'getSelf') from line in the getter:

let moduleRoot = window.moduleSpace[moduleName].getSelf;

So it seems like it thinks it's there, but it hasn't actually loaded in at all.

Short-term, a more useful error message would be useful for creators. But ideally, it would actually load in the import when createPerchanceTree is called.

you are viewing a single comment's thread
view the rest of the comments
[–] wthit56 1 points 3 months ago (1 children)

Cool... So, is there a way of just dynamically adding that import? Something like window.root.blah="{import:foo}"? Or is that stuff only ever checked onload?

I'll look into integrating that dynamic importer! 👍

[–] wthit56 1 points 3 months ago* (last edited 3 months ago) (2 children)

@[email protected] Okay I've added dynamic import to my plugin. Took me hours to figure out how to get it to actually work with createPerchanceTree though. Pretty much because for some reason it doesn't add the imported tree to the moduleSpace. So I had to figure out what that was, where it was, how to add it there... but that was only after I'd been trying like 10 other things 😂

I feel like it would just make sense it would automatically add whatever it loaded to moduleSpace honestly. That's where all imports live anyway, I think. And it would make this stuff "just work" without needing more deep knowledge about how it works internally.

Anyway... here's the code I've come up with:

var context = createPerchanceTree(context);
 for (var node of context.$allNodes) {
  if (node.$imports) {
    for (var name  of node.$imports) {
      window.moduleSpace[name] = dynamic_import(name);
    }
  }
}

And now it just works. Anything tricky about how I've done it? Anything I should avoid or add for any reason?

(Actually I think the above code could be used to chain through imports, in fact... right? So you could support that in that plugin itself maybe?)

[–] wthit56 1 points 3 months ago

I got it working! Made my own wrapper for createPerchanceTree, that does this recursive loading at the same time. A touch hacky, as for whatever reason it seems running dynamic_import tries to get those imports? Or something? So I wrap that part in a try-catch everything else (seems to) work just fine.

https://perchance.org/advanced-createperchancetree

No problem if you want to take these ideas and work it into the engine itself by the way--all good if I can help improve the platform in my own way. 👍 @perchance

[–] perchance 1 points 3 months ago (2 children)

I feel like it would just make sense it would automatically add whatever it loaded to moduleSpace honestly. Oh, hmm, I think that makes sense if it were to recursively load all sub-imports (i.e. if the module is "properly" loaded, then it should be added to the module space).

I've just added a second input param to createPerchanceTree so you can specify the generator/module name, and if you do, then it adds it to the module space with that name: createPerchanceTree("foo=123", {name:"my-module-name"}). With that, I can now upgrade the dynamic import plugin to do as you suggest - recursively import all sub-imprts. Here's a fork for you to test:

https://perchance.org/dynamic-import-plugin-test1#edit

Can you please test this and let me know if there are any issues? If there are no issues, I see no reason why the actual dynamic-import-plugin shouldn't be updated with this code. I doubt this change would break anyone's generator, especially since the dynamic-import-plugin is quite new.

Example: https://perchance.org/4auik0v09f#edit

Thanks!

[–] wthit56 1 points 3 months ago* (last edited 3 months ago) (1 children)

you can specify the generator/module name

Oh that's pretty sweet! Not what I was talking about though 😅

(Edit: Ah I think I see what you mean. You've made it so that it's createPerchanceTree's responsibility to add it to the moduleSpace. In mine I just added it from what was imported, on the import side. But I guess either way gets the job done.)

I was talking specifically about calling dynamic_import(name). It returns the imported thing, but doesn't save it to window.moduleSpace with that name. Looks like it caches it for its own use, but doesn't put it in the moduleSpace for everything else to use after it's imported.

I actually made my own recursively loading thing (not really recursive but looping through all import names it finds), and built it into my own createPerchanceTree function: https://perchance.org/advanced-createperchancetree

Has demos within that page too so you can see it working.

[–] perchance 1 points 2 months ago* (last edited 2 months ago) (3 children)

If you can give feedback on https://perchance.org/dynamic-import-plugin-test1 that would be great. You should be able swap out your {import:dynamic-import-plugin} for {import:dynamic-import-plugin-test1} and if it doesn't do something that you want it to, or if it breaks something, or whatever, let me know.

Btw, I don't think this is correct: imported = dynamic_import(name); window.moduleSpace[name] = imported; since the dynamic import plugin returns $output of the module if it has one.

And in general, making your own edits to moduleSpace is probably not a good idea. If there's something like this that you want to do (i.e. "messing with internals" to some degree), then you should always make a feature request. Otherwise all your stuff could break at some point due to engine implementation-detail changes. But if something is documented in perchance.org/examples or perchance.org/advanced-tutorial then it's safe to use.

[–] wthit56 1 points 2 months ago

By the way, with my createPerchanceTree, I'm now setting the moduleSpace property to the equivalent of dynamic_import().$root.getSelf, and it seems to still work. Does that look right to you?

[–] wthit56 1 points 2 months ago

Oh I've put it back to using the original importer now by the way, so you can delete that test if you wish--it's safe.

[–] wthit56 1 points 2 months ago (1 children)

Yes, it works fine by the looks of it. 👍 I've integrated it into my createPerchanceTree version too.

I wasn't sure if I was doing it right, but it did work anyway, so I left it at that. I know I'm playing with fire doing stuff like this but it's either that or sitting on my hands doing nothing. I find it more interesting trying to do this stuff myself. And I always chat to you about it here anyway, to find out if there's a way I'm "supposed" to do it, or if there's just no official capability yet.

That's how I even found out about that importer. I just needed it extended to fit my needs.

I have also read through both of those, so I'm up to speed. There's just a lot of stuff the engine can do, just not through official means. And I like to dance on the edge 😜

[–] perchance 1 points 2 months ago* (last edited 2 months ago) (2 children)

or sitting on my hands doing nothing [...] And I always chat to you about it here anyway

I really love how you just dig in and solve the problems yourself - this is a very useful trait to have in life. So long as you let me know so I can create an "official" way I'm happy 😅 Thanks for pushing at the edges of the engine - it's very helpful! I'm going to switch https://perchance.org/dynamic-import-plugin to use the new recursive import code now.

[–] wthit56 1 points 2 months ago

Great, I've updated my createPerchanceTree with that now 👍

[–] wthit56 1 points 2 months ago

It's nice you're cool with it. I could imagine someone else just being annoyed--this is better I think 😁

[–] wthit56 1 points 3 months ago

I just edited your demo page and tried it with my own chain of test imports and seems to work fine 👍

How'd you get it to work without throwing because it had stuff that wasn't already imported?