AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Take the sum of a variable (https://forums.alliedmods.net/showthread.php?t=329485)

Dragos 12-26-2020 10:28

Take the sum of a variable
 
Hello,

I want to ask how to take the value of a variable, example:
PHP Code:

new SUM1 

and get the value it to a new public function
PHP Code:

 public takesum () new TAKESUM1 = ?? 


iceeedr 12-26-2020 11:07

Re: Take the sum of a variable
 
One of the easiest ways is to create the variable globally, so it is accessible in any function.

Dragos 12-26-2020 11:47

Re: Take the sum of a variable
 
What do you mean?
Can you show a example?

iceeedr 12-26-2020 12:41

Re: Take the sum of a variable
 
Quote:

Originally Posted by Dragos (Post 2730190)
What do you mean?
Can you show a example?

You asked a question in the "scripting help" area, keep in mind that if you don't know how to code you should post in the "suggestions / request" area.

PHP Code:

/* Sublime AMXX Editor v4.2 */

#include <amxmodx>
// #include <amxmisc>
// #include <cstrike>
// #include <engine>
// #include <fakemeta>
// #include <hamsandwich>
// #include <fun>
// #include <xs>
// #include <sqlx>

#define PLUGIN  "New Plug-In"
#define VERSION "1.0.0-8"
#define AUTHOR  "Author"

new GlobalVariable[32]
new 
GlobalVariable2

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_clcmd("say /test""Func")
}

public 
FirstFunc()
{
    switch(
random_num(02))
    {
        case 
0
        {
            
formatex(GlobalVariablecharsmax(GlobalVariable), "Hello World")
            
GlobalVariable2 0
        
}
        case 
1
        {
            
formatex(GlobalVariablecharsmax(GlobalVariable), "Hey wazzup")
            
GlobalVariable2 1
        
}
        case 
2
        {
            
formatex(GlobalVariablecharsmax(GlobalVariable), "BZZZ ERROR")
            
GlobalVariable2 2
        
}
    }
}

public 
Func(id)
{
    
FirstFunc()
    
client_print(id3"VariableString = %s | VariableInt = %i"GlobalVariableGlobalVariable2)
    return 
PLUGIN_HANDLED



Dragos 12-27-2020 09:36

Re: Take the sum of a variable
 
Ideea it's that my variable it's like this:


PHP Code:

public something (id) {
new 
test random_num(0,16)
}

public 
form (id) {
    new 
COREs test


I want to do like this. But it give some errors

Bugsy 12-27-2020 10:51

Re: Take the sum of a variable
 
Quote:

Originally Posted by Dragos (Post 2730297)
Ideea it's that my variable it's like this:


PHP Code:

public something (id) {
new 
test random_num(0,16)
}

public 
form (id) {
    new 
COREs test


I want to do like this. But it give some errors

This is because when you declare a variable within a function (local variable), it can only be used within that function. You will need to do what iceeedr suggested and declare it globally--outside of a function, towards the top under your #include's, (global variable), which allows any function in your plugin to access the value. Keep in mind that if one function changes it, it gets changed everywhere the variable is used across your plugin.

This is 'hello world' level stuff, I would do some more learning because you are going to have issues left and right until you understand the basic concepts.

Code:
//Declare variable globally new test public something (id) {     //Remove local declaration of variable     //new test = random_num(0,16)     //Instead you will use (instead of create) the variable that was declared globally     test = random_num(0,16) } public form (id) {     //This should now work     new COREs = test }

If you do not need the variable to be accessed by the entire plugin, you can instead declare it locally and pass it to another function so it can be used.
PHP Code:

public somethingid 
{
    new 
test random_num16 )
    
formid test )
}

public 
formid SomeValue 
{
    
//COREs will be assigned the 'test' value from function something()
    
new COREs SomeValue 




All times are GMT -4. The time now is 14:06.

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