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

Return array from embedded function?
Kelvin
#1 Posted : Thursday, June 22, 2017 8:36:55 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 3/26/2015(UTC)
Posts: 46
Man
Location: VA

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
Decided to try this out before asking, but it didn't work. I thought if I pushed multiple values before returning, it would put them into an array, but it only let me use the last value pushed. What I'm trying to do is push an array of strings to Squirrel, like for example, a list of files in a folder. Is there any way to do this?
absence
#2 Posted : Friday, June 23, 2017 12:31:02 PM(UTC)
Rank: Advanced Member

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

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
The correct answer to your question is simply "yes" :-)

Have a look at:

sq_newarray(...)


then either (when array created with correct size set, corresponds to arr[index]="string" in squirrel):

sq_pushstring(...)/sq_pushobject(...)
sq_pushinteger(...)
sq_rawset(...)


or (when empty array created, corresponds to arr.append("string") in squirrel):

sq_pushstring(...)/sq_pushobject(...)
sq_arrayappend(...)



I'd prefer the latter, though it usually implies some few extra memory allocations







matty
#3 Posted : Friday, June 23, 2017 2:54:02 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/12/2017(UTC)
Posts: 5

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Section 3.10 "Tables and arrays manipulation" of the Squirrel Docs might help you out.

Quote:
A new array is created calling sq_newarray, the function pushes a new array in the stack; if the parameters size is
bigger than 0 the elements are initialized to null.:'

Code:
void sq_newarray (HSQUIRRELVM v,SQInteger size);


Quote:
To append a value to the back of the array:

Code:
SQRESULT sq_arrayappend(HSQUIRRELVM v,SQInteger idx);


From there just return like you normally would.
Users browsing this topic
Guest (2)
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.172 seconds.