DoomOperator said: Yes this code is in the header file, should i transfer it to the .cpp?
You should be using your Header files for function declarations. I personally among many others consider it very bad practice to write any logic in your .H files, these go in your .CPP files. This is a different story with Templates but that is well beyond the scope.
DoomOperator said: If you check out Survival Game on GitHub you'll see that it is this way.
I'm going to put this out there that using someone's game on GitHub is a horrible way to learn C++ and Game Programming. You shouldn't even be learning C++ while using UE4 to begin with either.
DoomOperator said: I've never used a boolean as a function type like you have listed above, i'm interested in understanding it though.
It can be done because we want to know if the function executed as intended or didn't. In my example it returns TRUE if the inventory item changes to the weaponID which means pickup was successful, otherwise FALSE which means the player's hand slot and inventory slots are full resulting in a failed pickup.
DoomOperator said: I also don't understand the player playerOne Reference in your parameters? is this like (class CharacteBase *playerOne)
This is because we're not dealing with copies, or global variables. We want to change playerOne directly in this function.
DoomOperator said: i'm a bit puzzled, as in Unreal i only ever see the pointer the other way around like (class CharacterBase* playerOne)
Writing:
myclass* something;
is the same as writing:
myclass *something;
It comes down to personal preference and people will debate which is “better” for readability.
DoomOperator said: I'm still very much learning and consider myself an intermediate C++ Programmer with the unreal engine.
I feel it is needed to really suggest that you go back and learn C++ outside of UE4 and by not using the method of dissecting someone's code from GitHub. Based on the questions you've asked I don't believe you're at an intermediate level yet, and I would be careful to refer as such because you can give off the impression you know more than you really do. This will just lead to more and more confusion and problems the further you dive deeper into C++ and UE4, as well as game programming in general.
@Alberth Thankyou for the clarification. I've been coding for 5months or so, i have most of my terminology correct. You are right Alberth i am referring to a variable that has a value.
Rutin said: You should be using your Header files for function declarations. I personally among many others consider it very bad practice to write any logic in your .H files, these go in your .CPP files. This is a different story with Templates but that is well beyond the scope.
I agree with you.
Rutin said: I'm going to put this out there that using someone's game on GitHub is a horrible way to learn C++ and Game Programming. You shouldn't even be learning C++ while using UE4 to begin with either.
I have been learning C++ in Unreal from other resources mainly Udemy, Youtube, I do need to brush up on my understanding of C++ away from Unreal. I began by learning C++ concepts in code blocks and moved onto unreal shortly after. There are a few things i need to understand more, like i i still don't completely understand how to utilize () - Arguments. I am using this game from GitHub as it is an Official Epic Games UE4 project, and it is one of the very few references i could find to have a Inventory system that is completely in C++ without the use of blueprints. I have no desire to use blueprints because i am dedicated to becoming a very good games programmer with C++.
Rutin said: It can be done because we want to know if the function executed as intended or didn't. In my example it returns TRUE if the inventory item changes to the weaponID which means pickup was successful, otherwise FALSE which means the player's hand slot and inventory slots are full resulting in a failed pickup.
I'd love to see some example code of this, i find it best to read the code line by line as to understand it. I'm not sure if the example code i wrote before would work or not. If you could show me an example of this it would be handy and save me alot of time. I understand if you don't, i don't wish for you to do my work for me. Just trying to learn ?
Rutin said: This is because we're not dealing with copies, or global variables. We want to change playerOne directly in this function.
It comes down to personal preference and people will debate which is “better” for readability.
Thankyou a tonne, i will remember these things for life. ? I did not know this, though i did figure that the class in the arguments was affected.
Rutin said: I feel it is needed to really suggest that you go back and learn C++ outside of UE4 and by not using the method of dissecting someone's code from GitHub. Based on the questions you've asked I don't believe you're at an intermediate level yet, and I would be careful to refer as such because you can give off the impression you know more than you really do. This will just lead to more and more confusion and problems the further you dive deeper into C++ and UE4, as well as game programming in general.
I have some C++ courses on Udemy, i have been planning to just go through all my courses despite how boring hahaha cause yes there is alot more for me to learn.
Maybe, between Beginner-Intermediate best describes my coding experience and knowledge, i am on that threshold where i understand quite a bit at this stage. i'm about 5-6months into coding with unreal and have been doing so on a near fulltime basis. I said intermediate mainly referring to UE4.24.1 because some of the concets in the cpp file reference boilerplate code like FTimerHandles, FNames, CreateDefaultSubobject, so on. I don't know but i believe these sorts of things are reserved for the unreal engine and not other programs that deal with C++.
I really really appreciate this Rutin.
May i ask 2 things? could you please show me an some example code of how i would use this bool function that you showed me?
and is there any reference you could give me to some great C++ learning material? like is there a website with a great teacher with many videos? a Textbook? would it be worth while befriending a more senior level C++ programmer?
DoomOperator said: because i am dedicated to becoming a very good games programmer with C++
Actually, being a good programmer has nothing to do with C++ or any other language. The real problem you're solving as a programmer is not writing code, it is structuring a given problem such that a computer can solve it, preferably very efficient. To make that happen, you have to know a lot about data structures and algorithms. The solution at this stage is just an idea in your mind, or a drawing on a white board.
A language like C++, but also any other language (Java, Python, C#, etc etc), is just writing down the found solution such that it is readable for everybody. Computers can also read it (although they don't care for readability at all), and then do what you wrote.
I get what your saying. Being able to use words and descriptions that help others understand is a big part of it. I am specializing in C++ within the Unreal Engine and it's API. I agree with you. I honestly don't understand why my integer won't work in the code i listed above. I haven't done much with increments so i think that is part to it.
I get what your saying. Being able to use words and descriptions that help others understand is a big part of it. I am specializing in C++ within the Unreal Engine and it's API. I agree with you. I honestly don't understand why my integer won't work in the code i listed above. I haven't done much with increments so i think that is part to it.
I'll try to go over a few things but I don't have too much time. The main issue I've seen with newer programmers and inexperienced programmers is that they focus more on how things in the language work and syntax and not on the logic behind using the language as a tool to solve problems. What I mean by this is that you cannot assume you know how to program well just because you understand what a if statement is, or what a method is, or how to pass a variable, ect… All you would have to do is walk into an interview for a programming job and the second you're in front of the whiteboard you would fail because you're not focusing on how to solve problems logically by using the tools provided.
As @Alberth pointed out, you need to learn data structures and algorithms. For example, if I told you to use BFS (breadth first search) or even DFS (depth first search) to solve a maze problem without referencing a tutorial could you do it?
Copying code line by line and reading it isn't enough. You need to focus on learning how to use the language, then how to apply a variety of solutions to solving problems by taking algorithms and coding them in your chosen language. Also, if you cannot solve the problem by pseudo code then you're not going to solve it by programming in any given language assuming that language with or without additional libraries supports the requirements needed to solve the problem. This all comes with time, and by making your OWN programs while studying the methods people use.
Let me break down your original code here:
Artifact.cpp
void AArtifact::OnEnterInventory(ACharacterBase* NewOwner)
{
StorageSlotIndication++;
SetOwningPawn(NewOwner);
AttachMeshToPawn(GetStorageSlot());
}
This feeds back to a function in the header file .
Artifact.h
FORCEINLINE EInventorySlot GetStorageSlot()
{
if (StorageSlotIndication == 1)
return StorageSlot1;
else if (StorageSlotIndication == 2)
return StorageSlot2;
else
return StorageSlot3;
}
float StorageSlotIndication;
Despite what I said prior about how you're doing things… From what I can see you're tracking StorageSlotIndication as a float which for one isn't needed, an int will do. Another thing is when anything enters your inventory you're always increasing this value, but what if you run into 10+ items? Are you making sure that StorageSlotIndication cannot increase beyond the max number which represents all inventory is full?
The logic behind this is as follows:
Pick up event triggered (this can be done by either collision between the player and a ground item, or if the player attempts to pick up loot for example)
Can the player pick up the item?
Check if the player has open hand slots if not continue below.
Check if the first inventory slot is empty, if not cycle to the next slot until all slots have been checked, if not continue below otherwise pick up the item.
The player does not pick up the item a notice is triggered to let the player know their inventory is full (this is done by using a boolean like in my prior example)
You take the above and code it, but you have to understand the problem and the solution because knowing how to use C++ in the sense of loops, variables, functions, is not enough.
With the above we can see if an item can be picked up using a basic console application. I used a struct for this just to keep it all simple but pay attention to what is going on.
#include <iostream>
using namespace std;
struct player
{
int handSlot = 0;
int inventorySlots[3] = { 0, 0, 0 };
};
bool pickupWeapon(int weaponID, player *playerOne)
{
// Check Hands first
if (playerOne->handSlot == 0)
{
playerOne->handSlot = weaponID;
return true;
}
// Check Inventory next if hands are full
for (int a = 0; a < 3; a++)
{
if (playerOne->inventorySlots[a] == 0)
{
playerOne->inventorySlots[a] = weaponID;
return true;
}
}
return false;
}
void printSlots(player *playerOne)
{
// Print hand slots and Inventory slots
cout << endl << "Hand Slot = " << playerOne->handSlot;
cout << endl << "Inventory Slots = { ";
for (int a = 0; a < 3; a++)
{
if (a == 2)
{
cout << playerOne->inventorySlots[a] << " }";
}
else
{
cout << playerOne->inventorySlots[a] << " , ";
}
}
}
int main()
{
player playerOne;
player *playerOnePtr = &playerOne;
// TO SET VALUES FOR SLOTS PRIOR TO EXAMPLE BY USER
cout << "Enter four numbers, press enter after each number to set the slot ID. The first will be for hands, then the next three per inventory slot: ";
cin >> playerOne.handSlot >> playerOne.inventorySlots[0] >> playerOne.inventorySlots[1] >> playerOne.inventorySlots[2];
printSlots(playerOnePtr);
// ////////////////////////////////////////////////
int interactionCode = 0;
cout << endl << endl << "You're standing next to a sword, press 1 to pick it up: ";
cin >> interactionCode;
if (interactionCode == 1)
{
if (pickupWeapon(1, playerOnePtr) == true)
{
cout << endl << "Sword picked up!";
}
else
{
cout << endl << "Inventory is full!";
}
}
// Print hand slots and Inventory slots
printSlots(playerOnePtr);
cin.get(); // Waits
return 0;
}
Here is an example:
Now if I set everything to full but the 2nd inventory slot:
Your entire goal is to learn how to logically solve problems and use the language as a tool to write solutions in which the machine can understand and process.
The above also answers your question:
DoomOperator said: I'd love to see some example code of this, i find it best to read the code line by line as to understand it. I'm not sure if the example code i wrote before would work or not. If you could show me an example of this it would be handy and save me alot of time. I understand if you don't, i don't wish for you to do my work for me. Just trying to learn ?
@Rutin I really appreciate this man, i will go through this very slowly to comprehend it properly. I am very good at problem solving and i have found myself quite successful at figuring out what language to use to solve certain problems in unreal. I haven't really learnt much about loops, and how to use them. I see how your referencing things in the code, this is incredibly useful for me i thankyou Rutin. As to your question before about “breadth first search” and “depth first search” no i am yet to learn what this is about. I am dedicated to my art, and learning how to create a game in C++. I don't mean to flaunt my skill level or anything, i was trying to provide an insight to where i am. I know i have the ability to learn difficult concepts, the one thing i love about learning code is that is is based on logic, if it were irrational I'd be lost haha.
I may ask follow up questions when i begin to use this code but that won't be for a few days as my gf is visiting me.
Most of the week i am coding, trying to learn and such.
To say again, I really appreciate this Rutin, Super helpful, i hope to return the favor one day. ?