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

Is there something like prototypes in Squirrel?
Kelvin
#1 Posted : Wednesday, December 12, 2018 5:52:01 AM(UTC)
Rank: Advanced Member

Groups: Registered, Moderator
Joined: 3/26/2015(UTC)
Posts: 55
Man
Location: Georgia

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
My concern is what if I make two classes or something that are co-dependant; both of them need to know something about each other? Like, if a player object needs to do something when it collides with an item, the easy answer is to define the item first, but if an enemy needs to behave based on the player's position, but the player also needs to be able to act on the enemy, you can't define them in parallel. Something like this would be solved with a prototype, because it tells the system "don't worry; that doesn't exist yet, but it will". Is there some way to tell Squirrel the same thing?
absence
#2 Posted : Thursday, January 10, 2019 10:13:43 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 132
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
Squirrel doesn't work the way you assume, it is much more dynamic and hence does not need prototypes or forward declarations.
Wether a member or method is present gets checked at runtime, not at compile time. Simply access the member in question.
You may use try/catch, "instanceof" or "in" operators to programmatically handle dynamic cases (uncertaincy wether an object exists or not).

(However, I admit I sometimes do miss the C++ way of defining interfaces)

dumb example (by heart):
Code:

class foo() {
   moregoo=0 ;
   function add_goo(barinstance) { if (barinstance instanceof bar) moregoo+=barinstance.goo ; else throw("usage!") ; }
}

class bar() {
  goo=0 ;
  function reset(fooinstance=null) { goo=0 ; try { fooinstance.moregoo=0 ; } catch(x) { if (fooinstance) error("usage error:"+x+"\n") ;  } }
}





Kelvin
#3 Posted : Sunday, January 20, 2019 8:13:29 PM(UTC)
Rank: Advanced Member

Groups: Registered, Moderator
Joined: 3/26/2015(UTC)
Posts: 55
Man
Location: Georgia

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
Hmm, I wonder if my issue is because I'm using multiple files. Like, in one file, I'll have foo(), but bar() will be defined in a file after it. Cuz it seems like you're saying as long as they're defined in the same file, it'll be fine, but then that would mean having to clump much, much more code into the same file. What if I declared an empty bar() before foo() and then just let bar()'s own file redefine it so that by the time it gets to it in runtime, it'll call the correct one?
absence
#4 Posted : Friday, February 1, 2019 8:42:33 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 132
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
Hmmm. How do you load the file(s)? Using dofile or loadfile?

loadfile, put in simple terms, does this:

return function { ... content of file... };

and translates to something along:

local x=function { ...content of file... } ;
return x() ;

Hence, everything defined within the file is NOT put to the root table, but function local - including class definitions.
So the concepts of tables, "this" objects and environments might be your issue.

What is the exact error you get, can you put an example?
Kelvin
#5 Posted : Wednesday, February 6, 2019 1:25:19 AM(UTC)
Rank: Advanced Member

Groups: Registered, Moderator
Joined: 3/26/2015(UTC)
Posts: 55
Man
Location: Georgia

Thanks: 3 times
Was thanked: 0 time(s) in 0 post(s)
I use dofile, and the error is simply that what I'm trying to reference doesn't exist because it won't be defined until a later file.
absence
#6 Posted : Friday, February 22, 2019 8:51:47 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 132
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
then put everything into a function you call after loading the other file
-> Decouple file loading and execution of code referencing objects of different files

first load everything, then call their "main" functions.

let foo be defined in file2 and in file1 you have

local stuff=foo(...) ; -> foo not yet defined

instead, in file1 you do

function f1main()
{
local stuff=foo(...) ;

}

while in file2 you do for example

function foo(...) {...}

and then you can do

dofile(file1) ;
dofile(file2) ;
f1main() ;
f2main() ; //in case file2 references definitions from file1, you do the same in file2 of course

again, remember that the files get loaded and executed within a virtual function, like this:
(and this is meant literally, exact this is happening when you use dofile:)

function dofile(filename)
{
local tmp=function()
{
//CONTENT OF LOADED FILE filename
}
return tmp() ;
}

Note a definition in a file will overwrite any already defined by another, because the "this" object is the same, due to

function bar() {}

basicalliy resembling

bar<-function() {} ;

(and, also very important, anything local in the files will vanish after loading!)


Hope that helps
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.059 seconds.