Raised This Month: $51 Target: $400
 12% 

New API and Syntax


Post New Thread Reply   
 
Thread Tools Display Modes
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 11-03-2016 , 15:33   Re: New API and Syntax
Reply With Quote #801

Quote:
Originally Posted by abrandnewday View Post
If people don't mind me asking, what exactly is this "goto" thing and why was it removed? Security reasons? I'm unfamiliar with it.
https://www.google.co.uk/search?q=why+is+goto+bad

This is pretty well covered already - it makes control flow difficult to reason about.
__________________
asherkin is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 11-03-2016 , 16:22   Re: New API and Syntax
Reply With Quote #802

Quote:
Originally Posted by abrandnewday View Post
You can reintroduce it into your own SourceMod build if I'm not mistaken. Just "undo" the changes listed here. Compile a build, then you should be able to use whatever this "goto" thing is.
FYI goto has been disabled in the compiler for a much longer time. The commit you linked there just removed the dead code from the compiler as a cleanup, since it's never used.
__________________
Peace-Maker is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 11-03-2016 , 22:40   Re: New API and Syntax
Reply With Quote #803

headline is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 11-03-2016 , 23:40   Re: New API and Syntax
Reply With Quote #804

Quote:
Originally Posted by abrandnewday View Post
Ahh, interesting. I'll give it the first couple sites a good read. Always love to learn new things about the field I'm going to be going to college for (computer programming).
The seminal work on the subject is Edsger Dijkstra's "Go To Statement Considered Harmful", which you can find reprinted on the web.

The TL;DR version is that "good" uses of goto can be replaced with conditionals; loops; and function calls, while "bad" uses of goto make the program impossible to follow... so why keep it?

Side note: It helps if you know that goto, loops, conditionals, and (to a lesser extent) function calls are all varying types of jump instructions behind the scenes.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 11-04-2016 at 22:14. Reason: function calls, not functional calls
Powerlord is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 12-06-2016 , 21:08   Re: New API and Syntax
Reply With Quote #805

curious question I'd rather not test out:

What would happen if I made a vector variable (float[3]) and made a function return float[] and said function returned a float[4]?

PHP Code:
float vecOrigin[3];
float[] ReturnVec()
{
    
vecError[4];
    return 
vecError;
}
vecOrigin ReturnVec(); 
will it only take the first 3 indices and garbage the 4th?
__________________

Last edited by nergal; 12-06-2016 at 21:09.
nergal is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 12-07-2016 , 03:56   Re: New API and Syntax
Reply With Quote #806

It shouldn't compile.
Fyren is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 12-07-2016 , 10:55   Re: New API and Syntax
Reply With Quote #807

I think this is why do not return strings on functions, and to use the approach `void ReturnVec( &vector[], vectorSize )`. Here the `&` seems not to compile or be acceptable, it is just to enforce pass by reference than by value.
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 12-07-2016 at 15:10.
addons_zz is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-07-2016 , 10:56   Re: New API and Syntax
Reply With Quote #808

Arrays are always pass-by-ref, it makes no sense to use & with an array param.
__________________
asherkin is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 12-07-2016 , 13:04   Re: New API and Syntax
Reply With Quote #809

abrandnewday, &bool:bVal in the old syntax equal bool &bVal in the new syntax
__________________
Grey83 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 12-07-2016 , 13:44   Re: New API and Syntax
Reply With Quote #810

Quote:
Originally Posted by abrandnewday View Post
Ah, so I had to move the ampersand. Shit. Guess I gotta go through some things I've converted and figure out where I've removed ampersands.

What does the ampersand even mean when used like that? Is it something that would result in bad things if I was to use a plugin without those ampersands?
It means that the parameter is passed by reference, not by value which is the default behavior.
PHP Code:
// Pass by value
void SetTo42(int val)
{
    
val 42;
}

// Pass by reference
void SetTo42Ref(int &val)
{
    
val 42;
}

int val 1337;

SetTo42(val);
// "val" is still 1337
SetTo42Ref(val);
// "val" is now 42 
Also, you should't pass by reference whenever you can, only where you need it.

Last edited by klippy; 12-07-2016 at 13:45.
klippy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:32.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode