🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Hello, in need of some guidance for using a value that increments as a parameter

Started by
16 comments, last by DoomOperator 4 years, 4 months ago

Hey there, I appreciate all the guidance i can get i value your input.

I am trying to make it so that in my game when my character picks up a weapon that the weapon will initially go to the hands but then picking up another weapon will place the newly picked up weapon into the inventory aka back socket. I have 3 sockets other than the hands for the inventory of the weapons. I am using a float called StorageSlotIndication, then i increment it like so: p.s in my game weapons are called “Artifacts”

My thinking is that everytime the OnEnterInventory Function is played out StorageSlotInfication will increment it's value by 1.
but i am finding that it doesn't store the information like i want it to as seen below.
The effect i am having is that i pick up 1 weapon and it goes to my hands, the next weapon goes to my back inventory slot, But the 3rd weapon overlaps with the back inventory slot, and this is not what i want.

Please help, What changes can i make to have this effect work for me???????
I've spent nearly 3days trying to figure this out.

Should i try and make a function which has “StorageSlotIndication++;" I'm still very much learning C++ and would appreciate the help! ???

What should i do??

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;

Advertisement

By having your hands and inventory in an array you can easily solve this problem. Assume your hands are empty and inventory is empty and the int value is set to 0 for all.

bool pickupWeapon(int weaponID, player *playerOne)
{
	for (int a = 0; a < 4; a++)
	{
		if (playerOne->handsPlusInventory[a] == 0)
		{
			playerOne->handsPlusInventory[a] = weaponID;

			return true;
		}
	}

	return false;
}

Something like the above will always make sure you check by Hands, then 1st, 2nd, and 3rd inventory slot. By having a bool return you can see if the pickup was successful or not as well.

There are many ways to solve this problem but this is just a quick solution for you.

Programmer and 3D Artist

@Rutin I really appreciate this Rutin, I will test this code and see what i can do. I'm still very much learning and consider myself an intermediate C++ Programmer with the unreal engine.

I'm wondering how that would work with the current code i have? With the bool pickupWeapon(); do i run this function when the weapon is picked up? I'm reverse engineering the code from “Survival Game” by Tom Looman found on github.

I made need some more clarification on how i would implement this? Is it possible for you to give me some example code where this boolean is being used?

I do wish to learn and understand what you are sharing with me, if you'd be so kind.

Again i appreciate your reply ?

When you say “back inventory slot”, do you mean StorageSlot3?

Your StorageSlotIndication is a float (a decimal number), but the way you are using it, it should be an int (integer, whole number). When you do arithmetic with float numbers, and then compare them with integers, sometimes the results aren't as you'd expect. Look up “floating point arithmetic” for more information.

I suggest you change StorageSlotIndication to int and see if that helps.

@1024

Thankyou 1024, i will be sure to give that a go. I knew integers were whole numbers but i assumed that an increment ++ would always add 1 to whatever type it would be.

I think this should solve my problem, and yes socket 3 is another inventory slot, i think i have it listed as “ArmSocket”

DoomOperator said:

@Rutin I really appreciate this Rutin, I will test this code and see what i can do. I'm still very much learning and consider myself an intermediate C++ Programmer with the unreal engine.

I'm wondering how that would work with the current code i have? With the bool pickupWeapon(); do i run this function when the weapon is picked up? I'm reverse engineering the code from “Survival Game” by Tom Looman found on github.

I made need some more clarification on how i would implement this? Is it possible for you to give me some example code where this boolean is being used?

I do wish to learn and understand what you are sharing with me, if you'd be so kind.

Again i appreciate your reply ?

Yes, you would do something similar on your pickup trigger.

I only used a boolean so you can tell if your pickup failed as your inventory and hands are full. This gives you an opportunity to display that to the player in some way (message, alert, on screen prompt, ect…). Of course it's not needed and you can just use void instead.

Again, there are several ways to do this. This is just one way that works.

I also just noticed in your original post, are you writing logic code in your header files (.h)???

Artifact.h

FORCEINLINE EInventorySlot GetStorageSlot()

{

 if (StorageSlotIndication == 1)

  return StorageSlot1;

 else if (StorageSlotIndication == 2)

  return StorageSlot2;

 else  

  return StorageSlot3;

}

float StorageSlotIndication;

Programmer and 3D Artist

DoomOperator said:

@1024

Thankyou 1024, i will be sure to give that a go. I knew integers were whole numbers but i assumed that an increment ++ would always add 1 to whatever type it would be.

I think this should solve my problem, and yes socket 3 is another inventory slot, i think i have it listed as “ArmSocket”

Adding one is not the problem, comparing a float and an int is the problem ?

I think that the reason why the weapon goes to slot 3 is that “if (StorageSlotIndication == 2)” fails, because your value may not be exactly 2 (again, because of the way floating point arithmetic works, it is 2, but not equal to integer 2), so when you compare it with 2 (which is an integer), it evaluates as false, and skips to the else branch (slot 3).

@Rutin

I truly appreciate your help Rutin. I commend you for it.

Yes this code is in the header file, should i transfer it to the .cpp?

If you check out Survival Game on GitHub you'll see that it is this way.

I've never used a boolean as a function type like you have listed above, i'm interested in understanding it though.

I also don't understand the player playerOne Reference in your parameters? is this like (class CharacteBase *playerOne)

i'm a bit puzzled, as in Unreal i only ever see the pointer the other way around like (class CharacterBase* playerOne)

I quickly wrote this code below to show you how i understand this would work for me, but i feel like i am setting it up wrongly. Would you be able to give me some Example code of this bool/void function working in action to determine what place an item or weapon should take?

hahaha, yes i know my code below is probably way off.

Artifact.h

void pickupweapon(int WeaponID, ACharacterBase* PlayerOne);

FORCEINLINE EInventorySlot GetStorageSlot()
{
	if(pickupweapon(true))
	return StorageSlot1;
	else if (pickupweapon(false))
	return StorageSlot2;
	else 					← presuming there is a nullptr option as well as true or false
	return StorageSlot3;
{

@1024

I really appreciate your replies, this is all good stuff.

I changed it to an int, and it didn't work. Rutin pointed out my code is in the header file, this may be why it isn't working properly. It seems to register the first increment so it takes me to StorageSlot1, but it seems to not want to increment anymore after that even though the OnEnterInventory() Function is played everytime an item goes to inventory. I will try and move this to the .cpp file and see if things change.

Please not i am using an increment so that i have decrements on the StorageSlotIndication; when the weapon is dropped into the world and out of the inventory also i have more than 2 Storage slots so i can't use a boolean to determine what slot should be used especially since i plan to expand this to more slots like 4 or 5 and upwards.

I appreciate your input, and i believe i learnt something valuable about integers and floats from you ??
p.s, I originally started with int32 but that didn't work either, it might just be code doesn't work the way i want because it is in the header file.

thankyou 1024

Nope, in c++ a variable is space for a value instead of a reference to a value. That is a quite fundamental difference. As such a boolean has exactly 2 values, true and false. Pointers are an independent and completely othogonal concept in c and c++.

This topic is closed to new replies.

Advertisement