Vice City: Multiplayer

Server Development => Community Plugins => Topic started by: habi on February 24th, 2022, 04:02 AM

Title: Returning multiple values from squirrel vm embedded in C to squirrel script
Post by: habi on February 24th, 2022, 04:02 AM
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
Code: [Select]
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
Code: [Select]
class Position
{
float x;
float y;
float z;
};
Position a;
...
sq_pushobject(v, &a);
sq_newclass(v, true);
Something like this?
Thanks
Title: Re: Calling CPP function from squirrel
Post by: Athanatos on February 24th, 2022, 05:40 AM
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.
Title: Re: Calling CPP function from squirrel
Post by: KrOoB_ on February 24th, 2022, 06:57 AM
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
Title: Re: Calling CPP function from squirrel
Post by: habi on February 24th, 2022, 07:59 AM
Quote from Athanatos on February 24th, 05:40 AM
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.
sqrat. that is what they are using there. i am going to give a try.
Quote from KrOoB_ on February 24th, 06:57 AM
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
I understood.
sq_pusharray(..) and then returning 1. It also might work.
Title: Re: Calling CPP function from squirrel
Post by: . on March 2nd, 2022, 07:44 PM
You can also use a table to mimic this behavior. See tables and arrays manipulation(http://www.squirrel-lang.org/squirreldoc/reference/embedding/tables_and_arrays_manipulation.html).

Code: [Select]
// 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;
Title: Re: Calling CPP function from squirrel
Post by: habi on March 3rd, 2022, 05:06 AM
Ah, so the problem is now solved.
Thanks for providing this example. I will make use of this in my project.