How to call global lua function from inside flowgraph custom lua?

Discussion in 'Programming' started by Jon Stephan, Aug 25, 2022.

  1. Jon Stephan

    Jon Stephan
    Expand Collapse

    Joined:
    Jul 21, 2022
    Messages:
    11
    Hi,

    I want to call spawnPrefab from within the custom lua flowgraph node.

    From the console, I can type "spawnPrefab("prefabname","prefabpath","0 0 0","0 0 0 1", "1 1 1",true) ", and it works.

    But when I use the same command inside a custom lua node in flowgraph, it complains that spawnPrefab is nil. I've tried "self.spawnPrefab(" and a couple other things, but can't find the exact syntax.

    What's the right syntax for calling a global function from within the flowgraph custom lua node?

    Thanks,
    -Jon

    (Oh, and BTW, I know there's a flowgraph node that spawns a prefab, but that does some other stuff too, and I'm trying to debug some performance issues)
     
  2. r3eckon

    r3eckon
    Expand Collapse

    Joined:
    Jun 15, 2013
    Messages:
    340
    Had a look at this since I'm likely gonna need to use prefab spawning in flowgraph, pretty sure I just found why this happens. Not sure if you figured it out yourself yet but I'll still post here in case anyone else is trying to do the same thing.

    The issue is that function not being passed to the environment used by the custom lua node. It was likely ignored since the spawn prefab node exists.
    You can fix this by modifying the code of the "Custom LUA" node itself to pass that function to the environment, like this:



    Then you can use it like it's a global in your custom lua nodes. You can pass other missing global functions the same way.
    But if you're like me and don't want to edit the core scripts to get this working, the other option is to create a userfolder script.
    Call it something like "myutils.lua" and write a module containing a function that calls the spawnPrefab global function like you would in console.



    Userfolder scripts are loaded in extensions and will work in the custom lua node as long as you add local extensions = require("extensions")
    Then to call it in the custom lua flowgraph node use extensions.myutils.spawnPrefab or the equivalent for whatever you name your file and function.



    Both options work fine for me but I much prefer the second option since it resides in the userfolder so it's easily packed with a mod.

     
    • Like Like x 1
  3. Jon Stephan

    Jon Stephan
    Expand Collapse

    Joined:
    Jul 21, 2022
    Messages:
    11
    Thanks! This is the right answer!

    BTW, the only thing missing was where to put the .lua file. I put it in a "lua" dir under the user folder. I don't know if that's the only place it can be, but it works.
     
  4. r3eckon

    r3eckon
    Expand Collapse

    Joined:
    Jun 15, 2013
    Messages:
    340
    I stored mine in a differently named folder inside the user folder so it sounds like every lua file in the user folder will be loaded as an extension no matter where it is.
    Could be wrong about this, also I don't know what happens with conflicts given the folder has seemingly no influence on the line used to call the function.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice