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

habi

  • Sr. Member
  • Posts: 389
Returning multiple values from squirrel vm embedded in C to squirrel script
«  »Last edited
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

Athanatos

  • Hero Member
  • Throwing random words to increase my post counter
  • Posts: 1,023
Re: Calling CPP function from squirrel
« Reply #1,  »
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.
Developer of RCNR, Zombie Survival, VLSM(C++) and Infected City Survival

KrOoB_

  • Full Member
  • Keeper
  • Posts: 171
Re: Calling CPP function from squirrel
« Reply #2,  »
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

  • Sr. Member
  • Posts: 389
Re: Calling CPP function from squirrel
« Reply #3,  »
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.

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Calling CPP function from squirrel
« Reply #4,  »
You can also use a table to mimic this behavior. See tables and arrays manipulation.

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;
.

habi

  • Sr. Member
  • Posts: 389
Re: Calling CPP function from squirrel
« Reply #5,  »Last edited
Ah, so the problem is now solved.
Thanks for providing this example. I will make use of this in my project.