Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create class from raylib-cpp #198

Open
TokyoSU opened this issue Jul 2, 2022 · 7 comments
Open

Can't create class from raylib-cpp #198

TokyoSU opened this issue Jul 2, 2022 · 7 comments

Comments

@TokyoSU
Copy link
Contributor

TokyoSU commented Jul 2, 2022

Pretty all class of raylib-cpp have no constructor allowing that:

class ObjectName
{
public:
	ObjectName();
	~ObjectName();

	void Load(const char* path);
	void Draw(raylib::Vector3 pos);

private:
	raylib::Model model;
};

So it's impossible to create class like this because a error is thrown saying Model have no default constructor !

@RobLoach
Copy link
Owner

RobLoach commented Jul 2, 2022

A few options here...

  1. Use raylib::Model* model; instead... construct and delete at ObjectName destruction
  2. Pass const char* path in as part of the ObjectName constructor

I'm not sure how I feel about constructing the object without loading the associated data. Could run into Unload errors... Worth considering though!

@TokyoSU
Copy link
Contributor Author

TokyoSU commented Jul 3, 2022

  1. Well creating a pointer need allocating via RL_MALLOC() im using mimalloc so its probably a good option.
  2. Can't, because i wont use pointer for model, its overkill :x im planning to do a sceneManager, ive changed the raylib-cpp model.hpp file with a default constructor and it work fine :) im just calling Load(path) manually since the ObjectName class wont be deleted until the game exit, or if i call it myself.

@RobLoach
Copy link
Owner

RobLoach commented Jul 3, 2022

Mind submitting a Pull Request? Others may be interested in it too. I'd love to see what you're proposing.

@TokyoSU
Copy link
Contributor Author

TokyoSU commented Jul 3, 2022

Okay, just need to test more about it, for now, ive not crashed when using this method :)
The think is Texture use a default constructor that init a default texture, but i want to load it, i will technicaly have 2 texture for 1 define from a class xD

@TokyoSU
Copy link
Contributor Author

TokyoSU commented Jul 3, 2022

For this one, it will need some time since i need to check each class and found a way to move some default constructor to function, also testing it to see if it work :)

@RobLoach
Copy link
Owner

Quick thought... Would an initializer list for this help here?
https://docs.microsoft.com/en-us/cpp/cpp/constructors-cpp?view=msvc-170#member_init_list

class ObjectName
{
public:
	ObjectName(const std:string& modelFile);
	~ObjectName();

	void Load(const char* path);
	void Draw(raylib::Vector3 pos);

private:
	raylib::Model model;
};

ObjectName(const std::string& modelFile) : model(modelFile) {
   // Do other things.
}

A child property will need to know how to construct itself if you're creating the parent.

@TokyoSU
Copy link
Contributor Author

TokyoSU commented Aug 22, 2022

add a default one ObjectName() so it could be used within other class, else it would just throw a error about not being initialised properly with ObjectName(string),
also load the model in the Load() function and use it in ObjectName(string) to avoid problem with it, since you can set the error check only in Load(), also the modder/coder with be able to use Load() manually in custom class.
example:

ObjectName(const std:string& modelFile)
{
    Load(modelFile);
}
Load(path)
{
if (File.Exist(path))
    model.Load(path);
else
    Log("File not exist");
}

or something like that. (maybe the model class already check if file path exist so it would be weird to check 2 time for it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants