Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a script retain its values through different script loading in Lua?
    primarykey
    data
    text
    <p>My current problem is that I have several enemies share the same A.I. script, and one other object that does something different. The function in the script is called AILogic. I want these enemies to move independently, but this is proving to be an issue. Here is what I've tried.</p> <p>1) Calling <code>dofile</code> in the enemy's constructor, and then calling its script function in its <code>Update</code> function which happens in every game loop. The problem with this is that Lua just uses the script of the last enemy constructed, so all of the enemies are running the same script in the <code>Update</code> function. Thus, the object I described above that doesn't use the same script for it's A.I. is using the other enemies' script.</p> <p>2) Calling <code>dofile</code> in the <code>Update</code> function, and then calling its script function immediately after. The problem with this is that <code>dofile</code> is called in every object's update function, so after the AILogic function runs and data for that script is updated, the whole thing just gets reset when dofile is called again for another enemy. My biggest question here is whether there is some way to retain the values in the script, even when I switch to running a different one.</p> <p>I've read about function environments in Lua, but I'm not quite sure how to implement them correctly. Is this the right direction? Any advice is appreciated, thanks.</p> <p>Edit: I've also considered creating a separate place to store that data rather than doing it in the Lua script.</p> <p>Edit2: Added some sample code. (Just a test to get the functionality working).</p> <pre><code>-- Slime's Script local count = 0; function AILogic( Slime ) --Make the slime move in circles(err a square) if count &lt; 4 then Slime:MoveDir( 0 ); elseif count &lt; 8 then Slime:MoveDir( 2 ); elseif count &lt; 12 then Slime:MoveDir( 1 ); elseif count &lt; 16 then Slime:MoveDir( 3 ); else count = 0; end count = count + 1; end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload