hello everyone, i'm a new user to angel script, and am trying to get the bare minimum up and running for a hello world.
this is what i have so far, as i'm trying to keep it down to the absolute minimum, and do not want to use the string addon(i want to learn the inner functions, instead of just passing it off.):
their's alot of debugging code, so please bare with it. anyway, the problem i'm getting is:
FistScript (1, 1) : INFO : Compiling void HelloWorld()
FistScript (1, 30) : WARN : Argument cannot be assigned. Output will be discarded.
i assumed the problem was because i didn't have an operator for assignment, and added that, only to be treated to the same problem.
now then, this is what i'm getting as debugging:
Not!
**Debug info above**
Construct Called
Print Called
'(null)'
SFactory Called
s: Hello World!
Done...
Destruct Called
Destruct Called
as you can see, the "Hello World!" portion is constructed after the script is executed, and is needed.
can anyone help me on figuring out what i'm doing wrong?
alright, so i have another question, i'm working on asOBJ_REF object's, and much of my internal engine already works on the conecpt of referencing, my only problem is that much of it works like so:
class x{
public:
x *AddRef(void);
};
the problem, is that when i try to register this as the behavior like so:
Engine->RegisterObjectBehaviour("x", asBEHAVE_ADDREF, "x@ x()", asMETHOD(x, AddRef), asCALL_THISCALL)
and i get the error:
(0, 0) : ERR : Failed in call to function 'RegisterObjectBehaviour' with 'x' and 'x@ x()'
You can register the method as returning a void, even though it really returns a pointer. As the method is returning a pointer this would be placed in the cpu registers and can safely be ignored by AngelScript without any negative impact.
hey, thanks, so, i'm moving on with learning a bit more, and am running into another problem, essentially, much of my engine's vector's return float pointers to access the members, so, i thought of writing a simple wrapper and wrap the values into the simple struct representing the data, like so:
however, when i check the pointer value of o in xRVec function, it's reporting a diffrent value for the created pointer, and consequently, is not receiving the correct input of values.
here's the current output:
Construct x: 0x5925c8
Preparing
Executing
Construct x: 0x5990a8
o: 0x598538
Release called
Deconstruct x
y: -431602080.000000 -431602080.000000 -431602080.000000
Release called
Deconstruct x
The problem is that your xRVec function is implemented to return the vec type by value, but you registered it with AngelScript as returning the vec type by reference.
Because the xRVec is returning the type by value the C++ compiler adds a hidden pointer as the first argument. This pointer must be passed by the caller so the function can return the vec value in the location, but as AngelScript doesn't know this it doesn't pass the hidden pointer.
This will let AngelScript know that the vec type is really a C++ class with a structure, and can then properly treat the class when passing it by value to the application or receiving it by value in return values.
hey, so, i did what you suggested, and got the following error:
(0, 0) : INFO : vec x::GetPos()
(0, 0) : ERR : Can't return type 'vec' by value unless the application type is
informed in the registration
(0, 0) : ERR : Invalid configuration. Verify the registered application interf
ace.
so, i instead appended the reference to the function instead, like so:
vec &xRVec(x *o){
printf("o: 0x%x\n",o);
return vec(o->GetPos()[0], o->GetPos()[1], o->GetPos()[2]);
}
...
if(Engine->RegisterObjectMethod("x", "vec &GetPos()", asFUNCTION(xRVec), asCALL_CDECL_OBJLAST)<0) printf("Error registering GetPos()\n");
...
this works fine, and compiles fine, but i get an compiler warning that i'm returning an temporary address, as i am.
basically, i'm surprised that it's still working when i'm clearly returning temporary addresses, which can't be guranteed to be valid.
i've tried playing around a bit to make sure doing a few calls to the function won't corrupt the value returned, and it seems to work fine. I assume angelscript copy's the returned value into it's own structure, so the temporary address isn't used longer than a copy in the end.
still i feel unsafe about doing this, is their another approach to what i'm doing?
otherwise so far i'm loving angelscript, much better than lua imo.
Can't return type 'vec' by value unless the application type is informed in the registration
is because AngelScript doesn't have enough information about the type to know how it is treated in the native calling convention in order to return it by value.
Did you add the asOBJ_APP_CLASS_C flag that I mentioned in the last post? That is what AngelScript needs.
What platform are you using anyway? Is it 64bit Linux or Mac with gnuc/clang compiler? If it is then you'll also need to add the asOBJ_APP_CLASS_ALLFLOATS flag to let AngelScript know the vec type is composed of only floats. If you're not using 64bit Linux or Mac then you may still add the flag, but it is not needed as it doesn't make a difference what the class is composed of.
How to register a value type and what flags is described in the manual.
Please let me know if the above article is not clear enough and I'll do my best to improve it.
Returning a reference to a local variable is not something you want to do. While it compiles it will most likely not give the result you want in all cases. If you need to return a reference to a new value, then store the value in a static variable and return the reference to that value. At least that way you're guaranteed the value will still be there when the caller uses the reference.
Can't return type 'vec' by value unless the application type is informed in the registration
is because AngelScript doesn't have enough information about the type to know how it is treated in the native calling convention in order to return it by value.
Did you add the asOBJ_APP_CLASS_C flag that I mentioned in the last post? That is what AngelScript needs.
What platform are you using anyway? Is it 64bit Linux or Mac with gnuc/clang compiler? If it is then you'll also need to add the asOBJ_APP_CLASS_ALLFLOATS flag to let AngelScript know the vec type is composed of only floats. If you're not using 64bit Linux or Mac then you may still add the flag, but it is not needed as it doesn't make a difference what the class is composed of.
How to register a value type and what flags is described in the manual.
Please let me know if the above article is not clear enough and I'll do my best to improve it.
Returning a reference to a local variable is not something you want to do. While it compiles it will most likely not give the result you want in all cases. If you need to return a reference to a new value, then store the value in a static variable and return the reference to that value. At least that way you're guaranteed the value will still be there when the caller uses the reference.
Regards,
Andreas
ok, thanks for the information, i didn't notice the app_class_c flag in the other post, i added that and it works without any warnings. thanks for the help.