I'm struggling with issue mentioned in the title.For now I'm setting default one in constructor and then i want to change it with my function, however it doesn't seem to work.
void AMyGameModeBase::SwapGameState(AGameStateBase* GameStateVariable)
{
GameStateClass = GameStateVariable->StaticClass();
}
How can I do this properly? Good explanation would be awesome :)
EDIT:
So this is my whole code:
MyGameModeBase.h
#pragma once
#include "GameFramework/GameMode.h"
#include "MyGameModeBase.generated.h"
/**
*
*/
UCLASS()
class PROJECT_API AMyGameModeBase : public AGameMode
{
GENERATED_BODY()
public:
AMyGameModeBase();
UFUNCTION(BlueprintCallable, Category="GameState")
void SwapGameState(AGameStateBase* GameStateVariable);
};
MyGameModeBase.cpp
#include "Project.h"
#include "MyGameModeBase.h"
AMyGameModeBase::AMyGameModeBase()
{
GameStateClass = AGameStateBase::StaticClass();
}
void AMyGameModeBase::SwapGameState(AGameStateBase* GameStateVariable)
{
GameStateClass = GameStateVariable->StaticClass();
}
What I'm doing then is:
1.Open GameMode blueprint
2.Drag from Event begin play and call SwapGameState
3.I'm creating variable that is reference to MyGameState.
4.Then I'm printing with print string name that i get with node "GetGameState" and see that it's not changed.
What i want to achieve is:
1.CreateGameState in runtime
2.Set it to used default game state also in runtime.
The StaticClass function is a static function that you call like this MyStateClass::StaticClass()
What you want is this
void AMyGameModeBase::SwapGameState(AGameStateBase* GameStateVariable)
{
GameStateClass = GameStateVariable->GetClass();
}
Related
I'm trying to create a laser beam actor that spawns when the player presses the "Fire" action mapping. I can't get TSubclassOf<> to work so that I can spawn the actor that creates the laser. I can declare the variable and compile the project, but I can't seem to use the template anywhere else in the project because the variable isn't initialized??? I'll include my code to help explain what's happening.
Here is my LaserTagCharacter.h file. I've declared the LaserClass variable and I can compile this code with no problems.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "LaserTagCharacter.generated.h"
UCLASS()
class LASERTAG_API ALaserTagCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values
ALaserTagCharacter();
UPROPERTY()
TSubclassOf<class ALaserTagLaser> LaserClass;
protected:
// Called when player fires a laser beam
void OnFire();
};
Moving over to the LaserTagCharacter.cpp file, I've included some debug code and my main objective with creating the template class. I created the template class to use it in a SpawnActor function that will spawn an instance of LaserTagLaser.
#include "LaserTagCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "LaserTagLaser.h"
#include "Engine/World.h"
// Sets default values
ALaserTagCharacter::ALaserTagCharacter()
{
}
// Called when player uses fire action binding
void ALaserTagCharacter::OnFire()
{
UWorld* World = GetWorld();
FVector SpawnLocation = GetActorLocation();
FRotator SpawnRotation = GetControlRotation();
if (LaserClass)
{
UE_LOG(LogTemp, Warning, TEXT("Yes"))
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No"))
}
World->SpawnActor<ALaserTagLaser>(LaserClass, SpawnLocation, SpawnRotation);
}
The 2 messages I get from the output log when I compile, play, and press the "Fire" action mapping are:
LogTemp: Warning: No
and...
LogSpawn: Warning: SpawnActor failed because no class was specified
If you have any insight on how I can fix this and get TSubclassOf<> to work properly, that would be awesome!
Your code seems ok, but you should add in the header file a specifier inside UPROPERTY, and maybe a Category just give it a title
UPROPERTY(EditDefaultsOnly, Category = "Setup")
TSubclassOf<class ALaserTagLaser> LaserClass;
above the TSubclassOf so that UPROPERTY will be editable in the blueprint editor inside unreal as a drop-menu. Then you must a create a new blueprint based on the ALaserTagLaser. After that, open the drop-menu and you will see only classes and childs of classes of the ALaserTagLaser. Select the blueprint you created, click the compile button inside the blueprintEditor, and then it should be working...
I am trying to make functions repository. I have created four files:
Function.hpp, Function.cpp, FunctionsRepository.hpp, FunctionsRepository.cpp
I want to keep pointers to functions in vector of pointers.
//FunctionsRepository.hpp
#ifndef FUNCTIONSREPOSITORY_HPP
#define FUNCTIONSREPOSITORY_HPP
#include <vector>
using namespace std;
class FunctionsRepository {
private:
static vector<double *> pointerToFunctions;
public:
static void addFunction(double * wsk);
};
#endif
//FunctionRepository.cpp
#include "FunctionsRepository.hpp"
void FunctionsRepository::addFunction(double * wsk) {
pointerToFunctions.push_back(wsk);
}
//Functions.hpp
#ifndef FUNCTIONS_HPP
#define FUNCTOINS_HPP
#include "FunctionsRepository.hpp"
int constFunction(int numberOfVehicles);
void linearFunction();
void stepFunction();
#endif
//Funcctions.cpp
#include "Functions.hpp"
double constFunction(double numberOfVehicles){
return numberOfVehicles/2;
}
double (*funcConstant)(double) = constFunction;
//ERROR HERE
FunctionsRepository::addFunction(funcConstant);
I want to add new functions to program as easily as its possible and use it leater in other parts of program.
But I dont get it. Why i am getting this error. The addFunction() method is static, that means I can use it in other classes or parts of program. Vector is static to make sure that is the only one copy for whole program.
Use function wrapper. std::function can stores callable objects. So, your code will contain something like this:
class FunctionsRepository {
private:
// void() - function prototype
static std::vector<std::function<void()>> pointerToFunctions;
public:
static void addFunction(std::function<void()> wsk)
{
pointerToFunctions.push_back(wsk);
}
};
for more information consult official documentation: http://en.cppreference.com/w/cpp/utility/functional/function
I solved It. I received an error because I was calling the FunctionsRepository::addFunction(funcConstant); expression out of any scope. I just created new function to execute this command and thats all.
I am using gdal to do some raster works, well it has a GDALWarpAppOptionsSetProgress function which gets an static function to show its progcess. here you can find its link :Link
and this link
http://gdal.sourcearchive.com/documentation/1.6.0/gdal_8h_5703b651695c0cbe6f3644a0a18dda8b.html
well I know I must write an static function to use it, here is my function
static int My_FN_GDALTermProgress( double dfComplete, const char *pszMessage, void *pData)
{
if(progressBar){
progressBar->setValue(FN_GDAL_PROGRESS_VALUE);
}
double FN_GDAL_PROGRESS_VALUE = dfComplete * 100;
return TRUE;
}
well i have a class named gdal_dem which is like this
#include "gdal_dem.h"
#include "gdal_wrap.h"
#include <qdebug.h>
#include <iostream>
#include "cpl_string.h"
#include "gdal_priv.h"
#include "ogr_spatialref.h"
#include "gdal_utils_priv.h"
#include "cpl_error.h"
#include <QString>
#include "commonutils.h"
#include <QFile>
gdal_dem::gdal_dem(QString SrcFilename):
SrcFile(SrcFilename)
{
}
float FN_GDAL_PROGRESS_VALUE = 0.0f;
static int My_FN_GDALTermProgress(double dfComplete,
CPL_UNUSED const char * pszMessage,
CPL_UNUSED void * pProgressArg )
{
FN_GDAL_PROGRESS_VALUE = dfComplete * 100;
printf("Progress: %f\n",FN_GDAL_PROGRESS_VALUE);
return true;
}
////
int gdal_dem::colorrelief(QString Dstanationfile,QString colorfile){
.....
if(!(psOptionsForBinary->bQuiet))
{
prgFunc=My_FN_GDALTermProgress;
GDALDEMProcessingOptionsSetProgress(psOptions, prgFunc,NULL);
}
......
}
In above code I can set the above mentioned function in processing option and it works fine. but my problem is when I want to update a progress bar. I have a QProgressBar and it is in my main class. How can I pass it into the static function? I tried these ways:
1- I tried to get progressbar in my gdal_dem and also defined a static variable in gdal_dem and tried to set its value and update it in My_FN_GDALTermProgress, the problem is because progressbar is also static I can see it in wrap.cpp's contractor,
2-I tried to define a new My_FN_GDALTermProgress function in my main apps class but it must be static and I faced this error cannot declare member function to have static linkage
3- I also tried this method but it does not work
https://www.badprog.com/c-errors-warnings-cannot-declare-member-function-static-void-myclassmymethod-to-have-static-linkage
Well, How can I pass a parameter to my gdal_dem class and update its value in an static class in it?
Use the pData argument. You can pass anything you want to it when registering the static function. In this case, you can pass a pointer to your QProgressBar object:
QProgressBar* qProgBarObj = // ...
GDALDEMProcessingOptionsSetProgress(psOptions, prgFunc, qProgBarObj);
The static function will then receive it as the third argument:
static int My_FN_GDALTermProgress(double dfComplete, const char *pszMessage, void *pData)
{
auto progBar = reinterpret_cast<QProgressBar*>(pData);
progBar->setValue(/* ... */);
// ...
}
I'm trying to create a game in C++.
It has a "Session" class that kind of manages everything. It contains things like a GraphicsManager, a SoundManager, and the current world. It also contains a static pointer to an instance of itself. This way, I want the world to be available for the GraphicsManager so it can be rendered, for example.
Here is a simplified version of my code:
main.ccp
#pragma once
#include "Session.h"
int main() {
Session::getSession()->run(); //Starts a new session and runs it
return 0;
}
Session.h
#pragma once
#include "GraphicsManager.h"
#include "World.h"
class Session; //Forward declaration so it can have a pointer to itself
class Session {
private:
Session();
static Session* s;
World* w; //Pointer because no world is loaded at the beginning of the program
GraphicsManager gm; //Required right away
public:
~Session();
void run(); //Actually launches the game after preparation; not further declared in this example
World* getWorld(); //Returns the pointer to the current world
static Session* getSession();
}
Session.cpp
#include "Session.h"
Session::Session(): gm(GraphicsManager()) {}
Session* Session::getSession() { //Return an instance of Session. If no instance exist yet, create one.
if(s == NULL) s = new Session();
return s;
}
World* Session::getWorld() {return w;} //Returns a pointer to the current world
GraphicsManager.h
#pragma once;
class GraphicsManager {
private:
void render();
public:
void run(); //Calls the render method repeatedly; no further declaration in this example
}
GraphicsManager.cpp
#include "GraphicsManager.h"
void GraphicsManger::render() {
World* w = Session::getSession()->getWorld(); //Get pointer to current world so it can be rendered
}
The render method is where I'm stuck. If I put #include "Session.h" into the GraphicsManager.h file, it gives me an error because apparently two header files cannot include each other. If I put a forward declaration at the beginning of GraphicsManager.h or GraphicsManager.cpp, Visual Studio tells me that incomplete types are not permitted.
This has been giving me a headache for weeks. I've made games in Java before and there this pattern was accepted. So how can I do this? If this structure is not possible in C++, do you have other suggestions for it?
In GraphicsManager.cpp, the compiler needs to know about the Session, so you have to #include "Session.h" which by the way includes GraphicsManager as well as World.
A forward definition will not be sufficient, as the compiler would not be able to check types of getSession()->getWorld() expression.
Apparently your GraphicsManager.h doesn't rely itself on the other definitions, so there should'nt be an issue here.
Try to include Session.h to GraphicsManager.cpp:
#include "Session.h"
void GraphicsManger::render() {
World* w = Session::getSession()->getWorld(); //Get pointer to current world so it can be rendered
}
This way Session class defenition will be visible for compiler in GraphicsManager.cpp, so it will not generate incomplite type error. On the other hand, Session.h is not included to GraphicsManager header, so there will no problem that both headers include each other.
This should be a C++ specific.
I have a property m9ReloadAnim in the header file, I can access it from the constructor, but when I try to access it from an other function I get an error like: EXC_BAD_ACCESS or something like: "The address does not contain an object ".
I have a header class like this:
#ifndef __SWAT__Weapon__
#define __SWAT__Weapon__
#include "cocos2d.h"
class Weapon : public cocos2d::CCSprite
{
private:
cocos2d::CCAnimation *m9ReloadAnim = cocos2d::CCAnimation::create();
public:
Weapon();
~Weapon();
void reloadM9();
};
#endif
And a cpp file like this:
enter code here
#include "Weapon.h"
#include "cocos2d.h"
Weapon::Weapon(){
m9ReloadAnim->setDelayPerUnit(1.1f);
}
Weapon::~Weapon(){
}
void Weapon::reloadM9(){
m9ReloadAnim->setDelayPerUnit(1.1f);
}
You could not initialize variable like this:
cocos2d::CCAnimation *m9ReloadAnim = cocos2d::CCAnimation::create();
Only static const int could be init in class declaration.
Move this init to your ctor:
Weapon::Weapon()
: m9ReloadAnim(cocos2d::CCAnimation::create())
{
m9ReloadAnim->setDelayPerUnit(1.1f);
}
or
Weapon::Weapon()
{
m9ReloadAnim = cocos2d::CCAnimation::create();
m9ReloadAnim->setDelayPerUnit(1.1f);
}
Sometimes the becomes corrupted so you can't tell where errors originate. I would suggest to put a breakpoint at the entry point of each method, and step the code line by line to make sure that it's triggering in the reloadM9 method. Check to see the m9ReloadAnim is NULL or if it points to the object created at initialization. Additionally you need to check if you are using the library properly.