0 registered members (),
1,459
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
References?
#292280
10/01/09 17:16
10/01/09 17:16
|
Joined: Oct 2009
Posts: 4
FakeTruth
OP
Guest
|
OP
Guest
Joined: Oct 2009
Posts: 4
|
Hello everybody, I have a question: How do references work in A7 C-Lite? I want to create something similar to this: (In C++ the &[ampersand] sign does this)
function Add (int& result, int a, int b)
{
result = a + b;
}
// You can call the function like this
int myResult = 0;
Add(myResult, 5, 7);
// myResult is now 12
What would this function look like in C-Lite? Many thanks.
|
|
|
Re: References?
[Re: FakeTruth]
#292282
10/01/09 17:33
10/01/09 17:33
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
function Add (int* myResult, int a, int b)
{
*myResult = a + b;
}
int myResult = 0;
Add(&myResult, 5, 7);
The reference operator isn't actually needed because lite-c can detect if a function needs a variable or a pointer to the variable.
Last edited by DJBMASTER; 10/01/09 17:37.
|
|
|
Re: References?
[Re: DJBMASTER]
#292294
10/01/09 19:25
10/01/09 19:25
|
Joined: Oct 2009
Posts: 4
FakeTruth
OP
Guest
|
OP
Guest
Joined: Oct 2009
Posts: 4
|
Thanks DJBMASTER! But now I have another question. This doesn't work when sending a scalar of a vector as a parameter:
VECTOR myVector;
Add(&(myVector.x), 5, 7);
No matter where I put the & sign, I always get the memory address of the vector, and not of the scalar x Could anybody help me with this? Thanks!
|
|
|
Re: References?
[Re: FakeTruth]
#292299
10/01/09 19:41
10/01/09 19:41
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
I think structs are always passed by a pointer, so you have to take the address of the whole struct, and manipulate it on the other side of the function rather than passing individual members.
Last edited by DJBMASTER; 10/01/09 19:42.
|
|
|
Re: References?
[Re: DJBMASTER]
#292305
10/01/09 20:16
10/01/09 20:16
|
Joined: Oct 2009
Posts: 4
FakeTruth
OP
Guest
|
OP
Guest
Joined: Oct 2009
Posts: 4
|
Thanks everybody, I have got it to work! In the function I first tried float* instead of int* because the scalars in the vector are floats (I think), but this did really weird stuff. So I changed it to var* and this worked perfectly! I can send each scalar along separately Thanks again guys! PS: If you know anything about the float* weirdness, could you please tell me what that's about?
|
|
|
Re: References?
[Re: FakeTruth]
#292308
10/01/09 20:34
10/01/09 20:34
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
Found this statement in the manual...
"Lite-C additionally supports standard C/C++ integer or floating point variables for cases where you need a higher or smaller range, or better precision. The functions described in this section however use only var as argument and return values."
I think the members of the Vector struct are in-fact vars, which in turn can be a int/float. Maybe that's why only var will work?
Last edited by DJBMASTER; 10/01/09 20:35.
|
|
|
Re: References?
[Re: DJBMASTER]
#292416
10/03/09 10:32
10/03/09 10:32
|
Joined: Apr 2009
Posts: 274
atari98
Member
|
Member
Joined: Apr 2009
Posts: 274
|
|
|
|
Re: References?
[Re: atari98]
#292422
10/03/09 12:05
10/03/09 12:05
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Your right DJBMaster, vectors are by nature a set of three vars. But the manual is 'blurry' because some functions are apparently "overloaded" to be capable of accepting a> integer-vectors (never seen one of these though...) b> standard-vectors (which are just three vars in a row in memory) c> float-vectors (Ive never seen one used, but look at "_vec()" in the manual)
For a better idea of how the different game structures are built, try taking a look at the .../GStudio7/includes/aTypes.H file. JUST DONT ACCIDENTLY CHANGE ANY OF IT! or its re-install GameStudio time.... This will give you a good look at the "guts" of many game object structures.
Have fun...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|