YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Script Reloading
chryan
#1 Posted : Sunday, November 28, 2010 11:06:27 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/28/2010(UTC)
Posts: 2
Location: United States

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi, I was hoping to get some help regarding script reloading.
I would like my scripts to be reloaded on demand to allow for the scripts to be edited and 'refreshed' at runtime.
However, I can't seem to be able to unload the previous script and reloading the script still reflects the old one before editing.

Here is the code I've used:

Code:

// Loading the script.
int top = sq_gettop(vm);
if(SQ_SUCCEEDED(sqstd_loadfile(vm, file, true))) {
    sq_getstackobj(vm,-1,&obj);
    sq_addref(vm, &obj);
    sq_settop(vm,top);
}

// Run the script to register the functions
if(!sq_isnull(obj)) {
    int top = sq_gettop(vm);
    sq_pushobject(vm, obj);
    sq_pushroottable(vm);
    if(SQ_SUCCEEDED(sq_call(vm, 1, false, true))) {
        sq_settop(vm,top);
    }
}

// Unload the script
sq_release( vm, &obj );


I'm not sure if I'm doing anything wrong, but when I try to load the same file again after unloading, it still uses the old scripting code, as if I never reloaded it.

I would greatly appreciate any help or advice!
Thanks!

EDIT: Forgot to mention that I'm using Squirrel 2.2.4.
fagiano
#2 Posted : Tuesday, November 30, 2010 7:05:42 AM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 825

Thanks: 0 times
Was thanked: 36 time(s) in 29 post(s)
Hi, consider taht in squirrel both functions and classes are first class objects and they are refcounted.

So let's say I load this script(let's call it x.nut)

Code:

class X {
function PrintVersion()
{
  ::print("I'm the fist version 1");
}
}


then I run this code
Code:

MyGlobalInstanceOfX <- X();


then I run this code
Code:

MyGlobalInstanceOfX.PrintVersion();

this will print version 1

then I change x.nut like this and reloadit
Code:

class X {
function PrintVersion()
{
  ::print("I'm the fist version 2!!!");
}
}


then I run this
Code:

MyGlobalInstanceOfX.PrintVersion();

and it will still print version 1.

This is because MyGlobalInstanceOfX is referencing to a instance created with the old class(the old value of X). If now we would create a new instance of X, that would print "version 2". Classes,Instances and function behave just like data. Reloading a the script will assign a the new code to X but not touch previous instances that are referenced by some variable.

I hope this helps
Alberto
chryan
#3 Posted : Friday, December 03, 2010 5:13:59 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/28/2010(UTC)
Posts: 2
Location: United States

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Thanks for the info Alberto.

The only definitions in my script files are functions.
I'm currently not using any classes in those loaded script files.

Is there a way to undefine the previously loaded script functions?
jeremy_c
#4 Posted : Thursday, January 05, 2012 7:09:18 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/1/2012(UTC)
Posts: 7

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
chryan wrote:
Thanks for the info Alberto.

The only definitions in my script files are functions.
I'm currently not using any classes in those loaded script files.

Is there a way to undefine the previously loaded script functions?


I also have this same question. Any thoughts?

Jeremy
jeremy_c
#5 Posted : Friday, January 06, 2012 4:19:04 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/1/2012(UTC)
Posts: 7

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I am not sure if this is a good idea or not, but what I am currently doing is:

Code:
sq_newtable(v);
sq_setroottable(v);


Then run my setup procedure again to redefine all the functions/methods/user code.

Jeremy
fagiano
#6 Posted : Monday, January 09, 2012 1:43:36 AM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 825

Thanks: 0 times
Was thanked: 36 time(s) in 29 post(s)
If it is that what you want, there isn't anything wrong in what you are doing.

Alberto
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.095 seconds.