The best way to return structures
#1

The most common structure you use in SA-MP is a position (or a vector) Float:X, Float:Y, Float:Z. Now, all functions like SetPlayerPos and GetPlayerPos use the parameter way of accepting and returning (via by-ref parameters) this structure, but is there a better way?

It's occured to me during the writing of i_quat that I needed a function to return a quaternion (consisting of 4 Float numbers). Now I had multiple methods of returning this:

Code:
stock ReturnQuat(&Float:w, &Float:x, &Float:y, &Float:z)
stock ReturnQuat(Float:quat[4]) quat[0] = w, quat[1] = x //etc.
stock ReturnQuat() { Float:quat[4]; ... return quat; }
Which of these three methods you think is the best? The first method has the advantage of choosing a destination you wish for all returned values, be it a quadruple of variables or an array indices, but it's too long if you use it regularly. The second method is shorter and faster to use, but you have to remember which index means what (e.g. if it's WXYZ or XYZW). The third one can be used to easily pass its return to another function, but if you already have an array allocated for the return, it does another needless allocation. And you still cannot use the return like new Float:quat[4] = ReturnQuat();, because the expression has to be constant.

Which of these ways is the best one? I want my script to use standardized and most useful conventions for handling this.
Reply


Messages In This Thread
The best way to return structures - by IllidanS4 - 16.06.2016, 13:17
Re: The best way to return structures - by Misiur - 16.06.2016, 23:24
Re: The best way to return structures - by Gammix - 17.06.2016, 00:06
Re: The best way to return structures - by Macluawn - 24.06.2016, 13:32
Re: The best way to return structures - by Hiddos - 24.06.2016, 14:12
Re: The best way to return structures - by Kar - 27.06.2016, 06:25
Re: The best way to return structures - by Nero_3D - 27.06.2016, 13:28

Forum Jump:


Users browsing this thread: 1 Guest(s)