[Tutorial] 0AB1: call_scm_function

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
919
Likes
4
Points
18
#1
Hi, I was searching for any tutorial about it but didn't find any which would be made using simple language.
There is one here but I found it difficult to understand when I saw it for the first time.

Why is it useful?
Cleo is limited to 31 variables (+2 timers) which may cause some confusion while making complex scripts. By correct usage of 0AB1 it's possible to make a complex script look clean and organised. Each function has its own "internal variables" and changing them doesn't affect the script where the function is called, that's why it's so useful. By using it you can:
-make calculations using a lot of variables without affecting your base script
-pass a result of your calculations back to your base script
-set something (e.g. your position, your car's position)

Example of use:
Script below is using a function to set maximum speed of the car driven by the player. The function not only sets the maximum speed but also passes his current speed back to the base script. (I advise to copy the following code to your sanny builder and analyse it there, it's going to be more clear because of the colours)
Code:
{$CLEO}
0000: NOP

while true //constantly
wait 0
    if 00DF:   actor $PLAYER_ACTOR driving //check for driving
    then    
    0811: [email protected] = actor $PLAYER_ACTOR used_car //store your car to [email protected] variable
    0AB1: call_scm_func @set_max_speed 2 carHandle [email protected] speedValue 10.0 _returnedSpeedValue [email protected] //the code goes now to the ":set_max_speed" label, look there now to see what it does there
    //your first returned parameter from the function which was your current speed ([email protected]) becomes [email protected]
    0AD1: show_formatted_text_highpriority "your current speed= %f" time 1000 [email protected] //your current speed is displayed. If you used here [email protected] instead of [email protected] it wouldn't work because [email protected] is function's internal variable
    end
end


:set_max_speed
// your first parameter which is carHandle ([email protected]) becomes [email protected]
// your second parameter (10.0) becomes [email protected]
// if you passed any more parameters to this function they would become the following: [email protected], [email protected], [email protected], [email protected] etc...
02E3: [email protected] = car [email protected] speed //stores current speed to [email protected]
    if 0025:   [email protected] > [email protected] 
    then
    04BA: set_car [email protected] speed_to [email protected]
    end
0AB2: ret 1 [email protected] //[email protected] which is your current speed is sent back to your base script, now the code jumps to the point where the function has been called
0AB1: call_scm_func @set_max_speed 2 carHandle [email protected] speedValue 10.0 _returnedSpeedValue [email protected]
2 - number of parameters you'd like to pass to the function
[email protected] 10.0 - parameters you're passing to the function, first one becomes [email protected](function's internal variable), second becomes [email protected]
[email protected] - parameters you return to your base script by using 0AB2: ret 1 [email protected]

0AB2: ret 1 [email protected]
1 - number of parameters you'd like to return back to the base script
[email protected] - function's "internal variable" you return back to the base script, it becomes [email protected] or any variable you wish



Returning boolean
Function can also return "true"/"false" which means that it can be used as a condition. The script below displays a message if the specified actor is driving.
Code:
{$CLEO}
0000: NOP

while true
wait 0
    if 0AB1: @is_driving 1 $PLAYER_ACTOR
    then
    print "Specified player is driving" 100
    end
end

:is_driving
if 00DF:   actor [email protected] driving
then
0485:  return_true
else
059A:  return_false
end
0AB2: ret 0
0485:  return_true - causes the function to return true
059A:  return_false - causes the function to return false
Note that it's still neccessary to use "0AB2: ret" at the end of the function which returns boolean despite of the true/false opcodes above.

How not to use function returning boolean. (For some reason the "if and" condition is true even if only one of the actors is driving)
Code:
if and
0AB1: @is_driving 1 $PLAYER_ACTOR      //example of incorrect usage
0AB1: @is_driving 1 [email protected]      //example of incorrect usage
0AB1: @is_driving 1 [email protected]      //example of incorrect usage
then
 
end


Additional tips:
1. Sometimes, for example if someone would like to improve his already made function, he might want to add more parameters to it. That's why I used [email protected] instead of [email protected] to get the speed of the car. It's better to use higher variable than lower within a function when it's not neccessary. If I used [email protected] to get the car speed and later added 3rd parameter I'd have to change [email protected] variable to any other to get the speed of the car (or use something like "0085: [email protected] = [email protected]" at the begining of the function but personally I think it's better to avoid that and keep it simple)
2. Adding "_" or "_returned" to the titles of returned parameters (e.g. _carSpeed [email protected])  is a good way to distinguish which ones are which if you'd like to avoid counting them. It's not an issue with simple functions but it can save time and effort while working with functions which have like 15 or more different parameters.
 

Codex1337

Well-Known Member
Joined
Mar 1, 2014
Messages
170
Likes
0
Points
16
#2
Re: 0AB1: call_scm_function

Good job.
Expecting more good tutorials from you :)
 

0B36

Well-Known Member
Joined
Jan 6, 2014
Messages
1,331
Likes
0
Points
36
#3
Re: 0AB1: call_scm_function

By 31 variables limit, you can't use [email protected], [email protected], [email protected] and so on, right ?

Edit - 31 doesn't seem to be the limit, I can use 32 and upto 33. But I can't use 34. I'm receiving a Sanny Builder error.

So I searched and found this tutorial - http://gtag.gtagaming.com/forums/index.php?showtopic=310

Very enlightening.
 

0B36

Well-Known Member
Joined
Jan 6, 2014
Messages
1,331
Likes
0
Points
36
#5
Re: 0AB1: call_scm_function

What about additional variables as mentioned in the tutorial I linked ? I guess we can use them as local variables with Deji's opcodes.
 
OP
OP
monday

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
919
Likes
4
Points
18
#6
Re: 0AB1: call_scm_function

Hmm look at the last comment of that tutorial. He also posted another thread about it here. I have no idea how it works exactly nor tested it to be honest
 

springfield

Well-Known Member
Staff member
Joined
Feb 18, 2005
Messages
2,931
Likes
6
Points
38
Website
www.ugbase.eu
#7
Re: 0AB1: call_scm_function

Nice tutorial  :urtheman:

About more vars, if you don't wanna bother with cleo memory, use this PLUGIN by 0x.
And in SF just use arrays.

simple example;
Code:
repeat
    wait 40
until samp.Available()
alloc [email protected] = 1024


WHILE TRUE
WAIT 0
    0226: [email protected] = actor $PLAYER_ACTOR health
    0C0F: array [email protected] element 1 = [email protected]
    04DD: [email protected] = actor $PLAYER_ACTOR armour
    0C0F: array [email protected] element 2 = [email protected]
    
    0C0E: [email protected] = array [email protected] element 1    
    0C0E: [email protected] = array [email protected] element 2
    
    0AD1: "%d %d" 100 [email protected] [email protected]
END
I remember doing a rather big script and only using vars from [email protected] to [email protected]  :me_gusta:
 

Codex1337

Well-Known Member
Joined
Mar 1, 2014
Messages
170
Likes
0
Points
16
#8
Re: 0AB1: call_scm_function

Can't we just use these variables? "$PlayerHealth, $POS_Y"
What is the difference between 0[member=21661]S[/member] and $XYZ?
 

0B36

Well-Known Member
Joined
Jan 6, 2014
Messages
1,331
Likes
0
Points
36
#9
Re: 0AB1: call_scm_function

Codex1337 link said:
Can't we just use these variables? "$PlayerHealth, $POS_Y"
What is the difference between 0[member=21661]S[/member] and $XYZ?
Yes you can. Variables starting with "$" are termed as global variables; they can be used anywhere in the script, while variables having "@"  are defined as local variables, only to be used inside the script area where they're previously defined.
 

springfield

Well-Known Member
Staff member
Joined
Feb 18, 2005
Messages
2,931
Likes
6
Points
38
Website
www.ugbase.eu
#10
Re: 0AB1: call_scm_function

0B36 link said:
Yes you can. Variables starting with "$" are termed as global variables; they can be used anywhere in the script, while variables having "@"  are defined as local variables, only to be used inside the script area where they're previously defined.
Global means global, so every $ var will be the same in every cleo script. So if you assign a value to $30, and some other non-related script uses the same $30 it will fuck things up. Plus, they don't always work right in 4.1.

Codex1337 link said:
Can't we just use these variables? "$PlayerHealth, $POS_Y"
What is the difference between 0[member=21661]S[/member] and $XYZ?
[email protected] mean short string, you use it to store short strings(under 8bytes). It will also occupy the next variable.

[email protected] = 4 bytes
[email protected] = 8 bytes = [email protected], [email protected]
[email protected] = 16 bytes = [email protected], [email protected], [email protected], [email protected]

[email protected] = 'Codex69'
[email protected] = 'Code'
[email protected] = '69'
 

4changesLeft

Well-Known Member
Joined
Apr 10, 2015
Messages
365
Likes
0
Points
16
#11
RE: 0AB1: call_scm_function

@monday great tutorial, helped me a lot, i have one question tho. Can you use "wait" on a called function? Or it will bypass it just like if you use it on a command?
 
OP
OP
monday

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
919
Likes
4
Points
18
#12
RE: 0AB1: call_scm_function

@TehArgis
I have no idea to be honest
 
Top