Running a lua script multiple times... errors on 2nd,3rd etc

Nibbie
Posts: 1
Joined: 2010.11
Post: #1
I'm trying to run a lua file in C, and the first time I run it, it runs fine, but the next time I run it, it gives me:
[crap]
attempt to call a table value
attempt to call a string value
attempt to call a string value
attempt to call a string value...
[/crap]

here's the code I use to load the file:
Code:
strcpy(filename,RootPath);
strcat(filename,"/Data/LUA/");
strcat(filename,theLevel.objects[temp].luaName);
printf("Loading Level Lua Script from:\n%s\n",filename);
theLevel.objects[temp].lua = lua_open();
luaopen_base(theLevel.objects[temp].lua);
luaopen_io(theLevel.objects[temp].lua);
luaopen_string(theLevel.objects[temp].lua);
luaopen_math(theLevel.objects[temp].lua);
luaopen_draw(theLevel.objects[temp].lua);
luaopen_madtak(theLevel.objects[temp].lua);

if (luaL_loadfile(theLevel.objects[temp].lua, filename)) printf("Cannot load lua file: %s\n",lua_tostring(theLevel.objects[temp].lua, -1));

and to run it:

Code:
if (lua_pcall(theLevel.objects[which].lua, 0, 0, 0)) printf("Cannot run lua level script: %s\n",lua_tostring(theLevel.objects[which].lua, -1));

As stated before, it works the first time, then when I run it again, it sends an error.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
I suspect the bug lies in the lua -- perhaps you forgot that variables are global unless declared otherwise?
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #3
I didn't use any variables in the lua code...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
show the lua code then.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #5
It's just a homemade function
Code:
madtak.print("it works!\n")
The function is fine and it returns nothing.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
then I guess madtak has some unbalanced stack operations in it or something.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
Hey, where did RootPath come from I want that!

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #8
Why dont you just Null you lua state and reopen the file?

Or

have a main() function in the script and call that again instead of doing the file from scatch

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #9
reloading the file means recompiling it and that takes a bit of time... I want this to be fast.
By the way, RootPath is:
Mac:
Code:
#include <Carbon/Carbon.h>

extern char RootPath[2048];

void loadPlatformDep()
{
    printf("Mac platform detected...\n");
    //Get the application's location
    CFBundleRef Bundle;
    CFURLRef URL;
    CFStringRef String;
    FSRef FileRef;
    FSRef ParentRef;
    /* Find the application's Bundle */
    Bundle = CFBundleGetMainBundle();
    /* Get the URL of the bundle */
    URL = CFBundleCopyBundleURL(Bundle);
    /* Get the string for the URL */
    String = CFURLGetString(URL);
    /* Get the FSRef for the URL */
    CFURLGetFSRef(URL, &FileRef);
    
    FSGetCatalogInfo(&FileRef, kFSCatInfoNone, NULL, NULL, NULL, &ParentRef);
    FSRefMakePath(&ParentRef, (unsigned char *)RootPath, 1024);
    printf("The URL: %s\n",RootPath);
}

and Windows:
Code:
#include <windows.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

extern char RootPath[2048];

void loadPlatformDep()
{
    printf("Windows platform detected...\n");
    getcwd(RootPath, 2048);
    printf("The URL: %s\n",RootPath);
}

That is the only platform dependant code in my game... I could make it ambiguous by having it chop off the bundle to before /Appname.app/ but I was a bit lazy...

This also probably is not the OSC way, it's the "make it like MetaL used to work because I really don't know C and only started this thing 2 weeks ago" way.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
looks good, aside from all the unnecessary messing with FSRefs on the Mac side Rasp
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #11
Yeah, thanks a lot.
Did you try having the lua script having an initialize() function. That might solve your problems if not you could put a destroy() function too.
Just do
Code:
if(!L)
   callFunction(L, destroy)
callFunction(L, initialize)

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Running a .APP from within a shell script (osx) Jamie W 3 3,422 Dec 8, 2009 03:06 AM
Last Post: Jamie W
  java script using UNITY imaumac 6 6,239 May 16, 2009 08:07 AM
Last Post: imaumac
  What do you think about this free cute doggie script :) Polycount Productions 1 2,418 Apr 7, 2006 04:04 AM
Last Post: Taxxodium
  Lua: Script glue resources Fenris 5 4,276 Oct 7, 2004 07:24 AM
Last Post: AnotherJake
  Lua: Script help hyperzoanoid 5 5,037 Dec 15, 2002 12:49 PM
Last Post: OneSadCookie