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

[L4D2 / CSGO / ANY] Execute Vscript Get Return


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 06-29-2019 , 11:53   [L4D2 / CSGO / ANY] Execute Vscript Get Return
Reply With Quote #1

About:
  • Heres an example plugin for returning values from a VScript code for use within SourcePawn.
  • I cannot find any other good method for returning strings from VScript code.
  • This uses a ConVar to transfer the data instantaneously.
  • ConVars are limited to 255 bytes max length.
  • Also the code max length is 255 but I have not tested.
  • Remove the <RETURN> </RETURN> if statement from the stock for optimization if not required.


Stock:
PHP Code:
// ====================================================================================================
//                    START
// ====================================================================================================
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

ConVar gCvarBuffer;

public 
void OnPluginStart()
{
    
gCvarBuffer CreateConVar("sm_vscript_return""""Buffer used to return vscript values. Do not use.");
}

// ====================================================================================================
//                    FUNCTION
// ====================================================================================================
/**
* Runs a single line of VScript code and returns values from it.
*
* @param    code            The code to run.
* @param    buffer            Buffer to copy to.
* @param    maxlength        Maximum size of the buffer.
* @return   True on success, false otherwise.
* @error    Invalid code.
*/
stock bool GetVScriptOutput(char[] codechar[] bufferint maxlength)
{
    static 
int logic INVALID_ENT_REFERENCE;
    if( 
logic == INVALID_ENT_REFERENCE || !IsValidEntity(logic) )
    {
        
logic EntIndexToEntRef(CreateEntityByName("logic_script"));
        if( 
logic == INVALID_ENT_REFERENCE || !IsValidEntity(logic) )
            
SetFailState("Could not create 'logic_script'");

        
DispatchSpawn(logic);
    }

    
// Return values between <RETURN> </RETURN>
    
int pos StrContains(code"<RETURN>");
    if( 
pos != -)
    {
        
strcopy(buffermaxlengthcode);
        
ReplaceString(buffermaxlength"</RETURN>"");");
        
ReplaceString(buffermaxlength"<RETURN>""Convars.SetValue(\"sm_vscript_return\", ");
    }
    else
    {
        
Format(buffermaxlength"Convars.SetValue(\"sm_vscript_return\", \"\" + %s + \"\");"code);
    }

    
// Run code
    
SetVariantString(buffer);
    
AcceptEntityInput(logic"RunScriptCode");
    
AcceptEntityInput(logic"Kill");

    
// Retrieve value and return to buffer
    
gCvarBuffer.GetString(buffermaxlength);
    
gCvarBuffer.SetString("");

    if( 
buffer[0] == '\x0')
        return 
false;
    return 
true;



Examples:
PHP Code:
// L4D2 VScript examples:

// Execute a single code block, avoid adding a semicolon ; at the end of the code statement. eg:
GetVScriptOutput("Director.GetGameMode()"buffermaxlength);
// Returns for example "versus"

// Get flow through map
float vPos[3];
GetClientAbsOrigin(clientvPos);
Format(buffersizeof buffer"GetFlowDistanceForPosition(Vector(%f, %f, %f))"vPos[0], vPos[1], vPos[2]);
GetVScriptOutput(bufferbuffersizeof buffer);
// Returns a float from 0 to GetMaxFlowDistance() for example "3550.3"

// Example stock: GetRandomPlayerName()
// Execute several code blocks, must wrap the section you want to return within <RETURN> </RETURN> otherwise the script will fail.
// Omit the trailing semicolon within <RETURN> block.
GetVScriptOutput("local player = null; while(player = Entities.FindByClassname(player, \"player\")) { if(player.IsSurvivor()) { <RETURN>player.GetName()</RETURN> } }"buffermaxlength); 


Changes:
Code:
0.1 (29-Jun-2019)
    - Initial release.
Attached Files
File Type: sp Get Plugin or Get Source (sm_vscript_return.sp - 462 views - 6.8 KB)
__________________

Last edited by Silvers; 06-30-2019 at 10:59.
Silvers 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 05:54.


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