Returning multiple values from squirrel vm embedded in C to squirrel script

Started by habi, Feb 24, 2022, 04:02 AM

Previous topic - Next topic

habi

I am creating a console application(.exe). I have embedded squirrel in it.
I have successfully loaded a .nut file. I can call functions in .nut file. No problems.

But the problem is i want to return three values when a C++ function is called from squirrel.
Usually, we do
sq_pushinteger(val);
return 1; //because we  are returning something;
But i want to return three values. How? They are float x,y,z;
Can anyone tell me a simple way to do it. Perhaps like creating a class
class Position
{
float x;
float y;
float z;
};
Position a;
...
sq_pushobject(v, &a);
sq_newclass(v, true);
Something like this?
Thanks

NicusorN5

Have you considered looking up VC:MP's source code? There's also functions that return a structure, for example player.Speed's getter function.

KrOoB_

this is probably not a good idea, not even an idea but have you tried to put them in to an array then return the array

habi

Quote from: Athanatos on Feb 24, 2022, 05:40 AMHave you considered looking up VC:MP's source code? There's also functions that return a structure, for example player.Speed's getter function.
sqrat. that is what they are using there. i am going to give a try.

Quote from: KrOoB_ on Feb 24, 2022, 06:57 AMthis is probably not a good idea, not even an idea but have you tried to put them in to an array then return the array
I understood.
sq_pusharray(..) and then returning 1. It also might work.

.

You can also use a table to mimic this behavior. See tables and arrays manipulation.

// New empty table is now on top of the stack
sq_newtable(vm);
// -- X
sq_pushstring(vm, _SC("x"), -1); // Key (will be popped by sq_newslot)
sq_pushinteger(vm, 69); // Value (will be popped by sq_newslot)
sq_newslot(vm, -3, false); // table is 3 elements down the stack
// -- Y
sq_pushstring(vm, _SC("y"), -1); // Key (will be popped by sq_newslot)
sq_pushinteger(vm, 42); // Value (will be popped by sq_newslot)
sq_newslot(vm, -3, false); // table is 3 elements down the stack
// -- Z
sq_pushstring(vm, _SC("z"), -1); // Key (will be popped by sq_newslot)
sq_pushinteger(vm, 77); // Value (will be popped by sq_newslot)
sq_newslot(vm, -3, false); // table is 3 elements down the stack
// Specify that we're returing the object at the top of the stack (i.e. the table)
return 1;
.

habi

#5
that solved the problem.
Thanks for providing this example. I will make use of this in my project.