# Unity Truth Table Data Structure

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am throwing some ideas around my head, but I want some feedback from the GameDev community. How would you go about implementing a 'truth table' in C++? i.e. what data structure would you use? Basically if I have some graph (nodes and directed edges), each node in the graph will have an associated truth table. The number of entries in the node's table is a function of the # of parents of the node (entries = 2^numParents). For example: 0 parents = 1 entry = t 1 parents = 2 entries = t / f 2 parents = 4 entries = tt / tf / ft / ff .. etc. Given the node, you will of course know the values of it's parents. These parent values are used to index into the current node's truth table. It gets tricky, though, because the # of indices used to index into the truth table equals the number of parents of that node. Because of this uniqueness to the problem, you can't use a simple associative array. This is essentially a Bayesian Network. It's so common I'm wondering how most applications go about implementing this. Thanks~

##### Share on other sites
In this case you could use a single dimensional array of size 2^N.

T *truthTable = new T[1 << numPredecessors];

You should then be able to use the predecessors' true/false as bits of the index into the array:

int index = 0;for (int i=0; i<numPredecessors; ++i)  index |= (predecessors->IsTrue() ? 1 : 0) << i;

This should also account for the "no predecessors" case where your index is always 0 (the first index).

Is that sort of what you wanted? The major downside of this approach is that it will use an astronomical amount of memory if you get a lot of predecessors.

If you have a large number of predecessors but only a couple entries in the truth table are true (or only a couple are false) then you would be better off with a set of bit-patterns that represent the "rare" state and assume everything else is the "default" state.

##### Share on other sites
... I started trying to write this and it wasn't much fun. But probably something with boost::multi_array<bool, N> and type erasure techniques (so that you don't have to know the number of dimensions ahead of time).

Edit: Of course, that's assuming you demand a pretty, iterative-subscripting interface. If you're willing to pack bits together, then just indexing into 2^N bit field is just fine. (Consider using a std::vector<std::bitset<32> > for example, and doing some shifting to figure out a vector index and a position within the bitset.)

##### Share on other sites
Thanks for the help!

Quote:
 Original post by NypyrenIs that sort of what you wanted? The major downside of this approach is that it will use an astronomical amount of memory if you get a lot of predecessors.If you have a large number of predecessors but only a couple entries in the truth table are true (or only a couple are false) then you would be better off with a set of bit-patterns that represent the "rare" state and assume everything else is the "default" state.

The one thing I wasn't clear on, I guess, was that each entry in the truth table holds some float value (not a bool). So I guess I should have just called it a Bayesian Network instead of a truth table :) There is no way around the fact that a node with a lot of parents will use up a lot of memory storing these table entries.

Based on the above posts, I have come up with a solution, using an array indexing scheme. I can allocate the correct # of required entries for the table, and then use an indexing algorithm to index into the array.

Thanks again.

• 47
• 11
• 17
• 11
• 13
• ### Similar Content

• I'm working on a space game, and I suck at art. I would love to get some help from someone who is more skilled than me. Things I need include modular space ship parts and GUI elements. Nothing too fancy, just functional so I can get a prototype put together. This could potentially become a serious project, but for now this is just a hobby project.

In this video, you can see a few things I already completed
:2018-02-24 20-08-13.flv2018-02-24 20-08-13.flv

• If you want to incorporate noise into your shaders, the Turbulance Library has you covered. Using code I gathered from this library, I made a cginc file that contains all you need to easily implement noise into your unity shaders. Who knows how this stuff works, but man, does it work well!

https://pastebin.com/LLCUpJut

Here is an example of what you can create using these noise functions.

• I'll be buying a new laptop as my workstation for building games, Mostly 3D but not hard core.

I'm stuck at choosing between these 2 specs below. Does this really matter and if so, can some one tell my how and why it matters.
Choice1:
Intel core i5-8250U (8th gen Kabylake refresh)(6 MB Smart Cache, 1.6 GHz Base with Turbo Boost up to 3.4 GHz) 4 cores 8 threads
RAM 8 GB DDR4 (2400 MHz)
GPU 2 GB DDR5 Nvidia MX150 256 bit
SSD: yes
Choice2:
Intel core i7-7500U 2.70GHz Base Processor (4M Cache, up to 3.50 GHz Boost) 2 Cores, 4 Threads
RAM 4 GB DDR4 (1800 MHz)
GPU 2 GB DDR5 Nvidia GeForce 940MX 256 bit
SSD: No

• Hello fellow devs!
Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

In Pseudo-Code:
angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
index = (int) (angle / (PI / 5));
PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

I can't get my head around it. Any suggestions? Is the whole approach doomed?

Thank you in advance for any input!

• By devbyskc
Hi Everyone,
Like most here, I'm a newbie but have been dabbling with game development for a few years. I am currently working full-time overseas and learning the craft in my spare time. It's been a long but highly rewarding adventure. Much of my time has been spent working through tutorials. In all of them, as well as my own attempts at development, I used the audio files supplied by the tutorial author, or obtained from one of the numerous sites online. I am working solo, and will be for a while, so I don't want to get too wrapped up with any one skill set. Regarding audio, the files I've found and used are good for what I was doing at the time. However I would now like to try my hand at customizing the audio more. My game engine of choice is Unity and it has an audio mixer built in that I have experimented with following their tutorials. I have obtained a great book called Game Audio Development with Unity 5.x that I am working through. Half way through the book it introduces using FMOD to supplement the Unity Audio Mixer. Later in the book, the author introduces Reaper (a very popular DAW) as an external program to compose and mix music to be integrated with Unity. I did some research on DAWs and quickly became overwhelmed. Much of what I found was geared toward professional sound engineers and sound designers. I am in no way trying or even thinking about getting to that level. All I want to be able to do is take a music file, and tweak it some to get the sound I want for my game. I've played with Audacity as well, but it didn't seem to fit the bill. So that is why I am looking at a better quality DAW. Since being solo, I am also under a budget contraint. So of all the DAW software out there, I am considering Reaper or Presonus Studio One due to their pricing. My question is, is investing the time to learn about using a DAW to tweak a sound file worth it? Are there any solo developers currently using a DAW as part of their overall workflow? If so, which one? I've also come across Fabric which is a Unity plug-in that enhances the built-in audio mixer. Would that be a better alternative?
I know this is long, and maybe I haven't communicated well in trying to be brief. But any advice from the gurus/vets would be greatly appreciated. I've leaned so much and had a lot of fun in the process. BTW, I am also a senior citizen (I cut my programming teeth back using punch cards and Structured Basic when it first came out). If anyone needs more clarification of what I am trying to accomplish please let me know.  Thanks in advance for any assistance/advice.