Raised This Month: $209 Target: $400
 52% 

[ANY]Native function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
NiceT
Junior Member
Join Date: Mar 2019
Old 10-17-2019 , 07:53   [ANY]Native function
Reply With Quote #1

Maybe I can ask about the principle of SourcePawn for native function implementation ?
No community like AlliedModders for a coder who likes squirrels is very unfortunately.

I really enjoy using squirrel write the script of L4D2, I also use sourcepawn write plugins , although squirrel not as good as sourcepawn in many places, but this does not mean that squirrel is not good, just sometimes not so strong like sourcepawn.

But now I have an urgent problem to solve:
In L4D2's "include" folder, I find a file named "halflife.inc" and there are some print functions in this file such as:

Code:
/**
 * Prints a message to a specific client in the center of the screen.
 *
 * @param client		Client index.
 * @param format		Formatting rules.
 * @param ...			Variable number of format parameters.
 * @error				If the client is not connected an error will be thrown.
 */
native void PrintCenterText(int client, const char[] format, any ...);

/**
 * Prints a message to a specific client with a hint box.
 *
 * @param client		Client index.
 * @param format		Formatting rules.
 * @param ...			Variable number of format parameters.
 * @error				If the client is not connected an error will be thrown.
 */
native void PrintHintText(int client, const char[] format, any ...);
It adds a native tag in front of the return value type but I'm not really sure I understand the meaning of the tag, so I want to ask what the tag's effect.

And, I also want to know the underlying implementation principle of this function.
I browsed https://wiki.alliedmods.net/Creating_Natives_(SourceMod_Scripting).

But, I don't think I really understand it.
And I searched all the .inc files to find something about regarding implementation of these two functions but nothing I found.

I want to implement them using squirrel, but I don't know how to do it.

Functions are first class values like integer or strings and can be stored in table slots, local variables, arrays and passed as function parameters. Functions can be implemented in Squirrel or in a native language with calling conventions compatible with ANSI C.

Maybe someone can explain the meaning of the above sentence, it is hard for me to understand it.
Although I don't understand this sentence, I think squirrels can also implement native functions, but I can't.

Squirrel can display HUD on the screen, so there must be no problem in displaying the text, but now the problem is that displaying HUD is for all players, and I hope to find a way to display different information for each specific survivor.

If the community doesn't allow discussion about other languages, the administrator can delete this post directly, but I really hope to find a way to make squirrels work like sourcepawn.
NiceT is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 10-17-2019 , 08:21   Re: [ANY]Native function
Reply With Quote #2

I'm glossing over a bit to keep things simple and I'm not too familiar with Squirrel, but I'll try to explain it.

Declaring a SourcePawn function as native means the plugin must use externally-provided code that isn't written in SourcePawn itself; it calls into code provided by SourceMod (which can in turn be engine functions, functions provided by extensions, or functions exposed by other plugins, which is what your link refers to -- it allows plugins to provide functions for other plugins to use).

Those native functions you posted are implemented in SourceMod itself, calling the engine's functions to display text.

Squirrel can also have functions that call into code provided by the Squirrel VM (or what application embeds that VM). The application has to register functions in Squirrel, which can be done by registering functions based on this manual entry. In the case of, say, L4D2, Valve provides additional functions for script authors to interact with the game.

Both require machine "native" code (in particular C++ for SourcePawn VM, C for the Squirrel VM) to register new functions into their respective virtual machines (SourcePawn and Squirrel, respectively). You can't expose functions written in Squrrel to SourcePawn, or the other way around.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 10-17-2019 at 08:31.
nosoop is offline
NiceT
Junior Member
Join Date: Mar 2019
Old 10-17-2019 , 09:04   Re: [ANY]Native function
Reply With Quote #3

Quote:
Originally Posted by nosoop View Post
I'm glossing over a bit to keep things simple and I'm not too familiar with Squirrel, but I'll try to explain it.

Declaring a SourcePawn function as native means the plugin must use externally-provided code that isn't written in SourcePawn itself; it calls into code provided by SourceMod (which can in turn be engine functions, functions provided by extensions, or functions exposed by other plugins, which is what your link refers to -- it allows plugins to provide functions for other plugins to use).

Those native functions you posted are implemented in SourceMod itself, calling the engine's functions to display text.

Squirrel can also have functions that call into code provided by the Squirrel VM (or what application embeds that VM). The application has to register functions in Squirrel, which can be done by registering functions based on this manual entry. In the case of, say, L4D2, Valve provides additional functions for script authors to interact with the game.

Both require machine "native" code (in particular C++ for SourcePawn VM, C for the Squirrel VM) to register new functions into their respective virtual machines (SourcePawn and Squirrel, respectively). You can't expose functions written in Squrrel to SourcePawn, or the other way around.


Thank you very much for your reply.
Maybe I haven't fully understood squirrels. Although I have written a lot of scripts and used them to implement many functions of sourcepawn, my usage of squirrels is still superficial.

Maybe my ability is not enough. Although I have read squirrels' documents carefully, I still don't understand a lot. For example, you replied about the usage of squirrels' registration functions.I have seen them many times before, I am still full of doubts.

I don't understand the implementation of the PrintCenterText on GitHud you replied to. Sorry about that.
NiceT is offline
Silvers
AlliedModders Donor
Join Date: Aug 2010
Location: SpaceX
Old 10-17-2019 , 11:03   Re: [ANY]Native function
Reply With Quote #4

Quote:
Originally Posted by nosoop View Post
You can't expose functions written in Squrrel to SourcePawn, or the other way around.
You can: [L4D2 / CSGO / ANY] Execute Vscript Get Return
__________________
Silvers is offline
OhHai
Junior Member
Join Date: Aug 2019
Old 10-17-2019 , 11:49   Re: [ANY]Native function
Reply With Quote #5

Quote:
Originally Posted by Silvers View Post
Oh snap
OhHai is offline
NiceT
Junior Member
Join Date: Mar 2019
Old 10-17-2019 , 13:49   Re: [ANY]Native function
Reply With Quote #6

Quote:
Originally Posted by Silvers View Post
What I want to do is "just" complete these functions through scripts. If you want to get the return value through scripts and then use the plugin output, why not using the plugin directly to complete it. Nevertheless, thank you for your help.
NiceT is offline
dustinandband
Senior Member
Join Date: May 2015
Old 10-17-2019 , 18:00   Re: [ANY]Native function
Reply With Quote #7

Quote:
Originally Posted by nosoop View Post
Declaring a SourcePawn function as native means the plugin must use externally-provided code that isn't written in SourcePawn itself;
So would this be incorrect usage of the "native" keyword?
This example is from a plugin & include file, but there's no compiled c++ code

l4d2stats.inc
Spoiler


l4d2_stats.sp
Spoiler
dustinandband is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 10-17-2019 , 21:12   Re: [ANY]Native function
Reply With Quote #8

Quote:
Originally Posted by nosoop View Post
I'm glossing over a bit to keep things simple
I guess I was in over my head trying to get to the point that you can't just register a native in Squirrel and use it like you would any other function in SourceMod without various hoops that may or may not be written yet.

Quote:
Originally Posted by NiceT View Post
I don't understand the implementation of the PrintCenterText on GitHud you replied to. Sorry about that.
The function is implemented and registered with C++; that's why you couldn't find anything written in SourcePawn about the underlying implementation as you mentioned in the first post.

Quote:
Originally Posted by Silvers View Post
You can: [L4D2 / CSGO / ANY] Execute Vscript Get Return
You're using the game engine as a bridge for the functionality. Of course you'll require some sort of bridge in any case (the VScript Functions extension is another existing method), but it's far from direct interop between the two VMs.

Quote:
Originally Posted by dustinandband View Post
So would this be incorrect usage of the "native" keyword?
SourceMod exposes the ability to register plugin functions as natives (this isn't necessarily a feature provided by other applications that embed the SourcePawn VM, if any). The plugin that knows about the native (whatever includes l4d2stats.inc) has no idea what implements the native. All it knows is that it's not within its own plugin, and so SM has to handle it.

I believe the native specifier started from Pawn and predates the ability to register natives through other SourcePawn plugins. SourceMod's native registration means it's closer to an extern, though again, that's a very simplified way of putting it.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 10-17-2019 at 21:15.
nosoop is offline
NiceT
Junior Member
Join Date: Mar 2019
Old 10-18-2019 , 01:17   Re: [ANY]Native function
Reply With Quote #9

Quote:
Originally Posted by Silvers View Post
Quote:
Originally Posted by nosoop View Post
You're using the game engine as a bridge for the functionality. Of course you'll require some sort of bridge in any case (the VScript Functions extension is another existing method), but it's far from direct interop between the two VMs.

Maybe I didn't express the meaning clearly before. what I want is to implement the function of sourcepawn through vscript, not vscript through sourcepawn. but, thanks everyone.
NiceT is offline
Silvers
AlliedModders Donor
Join Date: Aug 2010
Location: SpaceX
Old 10-18-2019 , 01:43   Re: [ANY]Native function
Reply With Quote #10

I was just pointing out something, not an answer to your question.

I still don't understand what you're trying to do. Why don't you write in SourcePawn? If you really need to call some VScript functions you can do for example:

PHP Code:
        // Credit to Timocop on VScript function
        
static int iScriptLogic INVALID_ENT_REFERENCE;
        if(
iScriptLogic == INVALID_ENT_REFERENCE || !IsValidEntity(iScriptLogic))
        {
            
iScriptLogic EntIndexToEntRef(CreateEntityByName("logic_script"));
            if(
iScriptLogic == INVALID_ENT_REFERENCE || !IsValidEntity(iScriptLogic))
                
LogError("Could not create 'logic_script");

            
DispatchSpawn(iScriptLogic);
        }

        
char sBuffer[96];
        
Format(sBuffersizeof(sBuffer), "GetPlayerFromUserID(%d).Stagger(Vector(%d,%d,%d))"useridRoundFloat(vPos[0]), RoundFloat(vPos[1]), RoundFloat(vPos[2]));
        
SetVariantString(sBuffer);
        
AcceptEntityInput(iScriptLogic"RunScriptCode");
        
AcceptEntityInput(iScriptLogic"Kill"); 
__________________
Silvers is offline
Reply


Thread Tools
Display Modes

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 08:32.


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