Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How can I make a callable function for a C++ class without initiation?

Hey all,

I am trying to create an AInventoryItem that will have a virtual function its children will call and override. The end goal is that each item made for the game will have its own unique UseItem() function but it can be called without creating an instance. I understand that functions can be made static, which makes them callable without an instance, but static functions cannot be virtual.

I am trying to avoid spawning an item to use its function, turning off collision and visibility, and then destroying it when its effect is complete. I know UE4 automatically deletes objects that go so far out of range. I want to avoid littering the game with a ton of invisible items and I don't want anything to be auto-deleted before completion (Say an elixir that lasts for an hour).

Does anyone know a simple way to reconcile this issue?

Thanks in advance.

Product Version: UE 4.13
more ▼

asked Dec 08 '16 at 03:22 PM in C++ Programming

avatar image

11 3 4 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I think encapsulating the item in a struct or UObject is the way to go. Have everything that needs to be available at "inventory time" be available. Only spawn the item when you really need it.

That is how I implement my inventory stuff most of the time.

more ▼

answered Dec 08 '16 at 03:56 PM

avatar image

1.1k 31 12 47

avatar image Cetaurefie Dec 08 '16 at 05:47 PM

All of the item data (icon, quantity, stack size, class) is in a struct. I just don't know how to use the item ( AInventoryItem::UseItem() ) without spawning it first

avatar image DennyR Dec 08 '16 at 05:52 PM

There is no real way, you can make UObject for that though, so instead of spawning an actor, just use NewObject() if you need call functions. You can also use a spawn UObject instead of a UStruct to give the DataContainer some real logic and override it for each item.

avatar image Cetaurefie Dec 08 '16 at 06:14 PM

Does it specifically need to be a UObject or can it be a child of a UObject such as AActor? I would assume AActor still can call NewObject() since it is a UObject but I just wanted to make sure.

avatar image DennyR Dec 08 '16 at 07:04 PM

It should be a UObject, since Actors has to be spawned, you can use AActors just fine though. Just no need to use AActor for something that doesn't "live" in the world.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question