How do i use variable from another function

how do i use x_size y_size z_size from permanent_var_value in xyzvaluetest and
xyvaluetest (i only need to access variables in this file no bp or other file)

1 i dont know any c++ exept for what is below.
2 i use visual studio 2015 if you need to know.
3 i searched here and google and found nothing that works.
4 i tried to make a public variable in permanent_var_value (got info from varius places)
{
public:
  int32 newvar = x_begin;
}
but public: get an error and say it expected an expresion

.h--------------------------------------------------------------------------------------
#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "variable_share_test.generated.h"

/**
 * 
 */
UCLASS()
class DUNGEON_MAKER_API Uvariable_share_test : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, meta = (DisplayName = "permanent var value", Keywords = "permanent var value"), Category = "test node")
	static void permanent_var_value(const int32 x_size, const int32 y_size, const int32 z_size);

	UFUNCTION(BlueprintPure, meta = (DisplayName = "test xyz 3darray pos2", Keywords = "xyz xzy yxz yzx zxy zyx 3darray array self made math"), Category = "test node")
	static void xyzvaluetest(int32 curent_x, int32 curent_y, int32 curent_z, int32 &array_position);

	UFUNCTION(BlueprintPure, meta = (DisplayName = "test xy 2darray pos2", Keywords = "xy yx 2darray array self made math"), Category = "test node")
	static void xyvaluetest(int32 curent_x, int32 curent_y, int32 &array_position);

};
.h--------------------------------------------------------------------------------------
.cpp----------------------------------------------------------------------------------
#include "dungeon_maker.h"
#include "variable_share_test.h"

void Uvariable_share_test::permanent_var_value(const int32 x_size, const int32 y_size, const int32 z_size)
{
	
}

void Uvariable_share_test::xyzvaluetest(int32 curent_x, int32 curent_y, int32 curent_z, int32 &array_position)
{
	array_position = ((curent_z*(y_size*x_size)) + (curent_y*x_size) + curent_x);
}

void Uvariable_share_test::xyvaluetest(int32 curent_x, int32 curent_y, int32 &array_position)
{
	array_position = ((curent_y*x_size) + curent_x);
}
.cpp----------------------------------------------------------------------------

If you put the public: outside of a class than it will result in an error, public: is how you define if a portion of the class is public when used in conjunction with private: or protected:

public: will result in variables underneath it being public to other classes
private: will result in variables underneath it being only usable whether reading or writing by the class they are contained in
protected: will result in variables underneath it being only usable by the class itself or classes derived from the initial class

As for your question on how to use variables that are local to other functions, you can’t use local variables in other functions, however if your class were to contain the x_size, y_size, z_size variables inside it, you would be able to assign values to them inside of permanent_var_value() and use them in later functions.

In here, you should declare (ie. tell that it exists) the variables in the .h and initialise (give an initial value to the variable) in the .cpp. It is also possible to initialise variables in .h, but you shouldn’t do that, because it might lead to multiple instances of that variable existing (or so big boys have told me).

So in the .h below GENERATED_BODY() you would write for example (also shown here is the proper way to declare it public):

public:
int32 newvar;

Initialising is done in the .cpp constructor, which would be the first “function” in your .cpp after the includes. Like this:


variable_share_test::variable_share_test() 
{  
newvar = 65;  
}

This variable can now be used in any of the functions and also it’s value can be changed. However, what I believe you want to do is to use the values used in permanent_var_value(). In this case you would declare three different variables in .h, as shown above - you can name them whatever, like newvar1, newvar2, newvar3 for example. It is suggested you use names that actually mean something so you know later what they are. After you have declared them, you also need to initialise them in the constructor as shown above. Then you would assign the values you want to use for them in the permanent_var_value function, like so:


newvar1 = x_size;
newvar2 = y_size;
newvar3 = z_size;

In general, variables that are declared in a function can not be used in other functions.

Also to note: The size_x you see in the permanent_var_value function is not actually a declared variable, but a function parameter. This means the value comes from somewhere when the function is called. With the code above every time permanent_var_value function is called, it stores the values in your parameters. With only this one class to work with, it’s difficult to say when the permanent_var_value function is actually called so it’s impossible to say when/if you are getting your variables set. You need to find out what function calls it (in visual studio this is easy, as you can highlight and right click and all kinds of tools sprung up) and what variables it uses to call it. If it isn’t called, you will be always using your default values with those other functions.

Ah, dale.clarke was faster. Essentially the same stuff!

i thought (permanent_var_value(const int32 x_size,) was to put the variables in the class from a bp.

i got to have missunderstod something.
.h
GENERATED_BODY()
public:
int32 newvar;
//no error
.cpp
void Uvariable_share_test::permanent_var_value(const int32 x_size, const int32 y_size, const int32 z_size)
{
newvar = x_size;
}
//error C2597: illegal reference to non-static member

got it working thanks for the help.
.h
GENERATED_BODY()
public:
static int32 ymax;
static int32 xmax;
static int32 zmax;
.cpp
#include “variable_share_test.h”

int32 Uvariable_share_test::xmax;
int32 Uvariable_share_test::ymax;
int32 Uvariable_share_test::zmax;