# Unity Truth Table Data Structure

## Recommended Posts

Doggan    528
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
Nypyren    12074
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[i]->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
Zahlman    1682
... 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
Doggan    528
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.

## Create an account

Register a new account

• ### Similar Content

•
Hi, I would like to have an opinion on what I can improve and change in my first published game.
The game was made on Unity3D.
Thank you.

• You are a US Army soldier, and one day an unknown enemy attacked your base. Now your task is to survive and protect the base. There are a lot of weapons in your arsenal. Survive this nightmare at any cost.

• Hi,
I am composer and sound designer that is trying to learn some codeing  Could You help me and tell me what should i add to change volume to 1.0F when enter trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioPlay : MonoBehaviour {

public AudioClip strings_1;
AudioSource audioSource;

// Use this for initialization
void Start () {
audioSource = GetComponent<AudioSource> ();
audioSource.PlayOneShot (strings_1, 0.0F );   - when game starts i want it to start but with 0 volume
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if(other.gameObject.tag == "slotviolin")
{
Debug.Log ("coilder works");

// how can i change volume to 1.0F and if it's possible how to make it smooth ?

}
}
}

Thanks

• Hold out as long as possible in the "Arena of Death." Only your skill and the correct tactics of combat will help you survive in the Arena. Nine types of weapons are at your disposal, but remember that cartridges are limited and they need to be spent wisely. You must prioritize the position and the type of weapons in order to achieve victory.   Awaiting you:   - 9 types of weapons - Dynamic gameplay - Different types of opponents, with their own characteristics and weaknesses - Increasing complexity   Download https://falcoware.com/DeadArena.php

• Hello guys
We are introducing our game Jump Jump And Fall
In this game you are help the cute box to jump every plain and collecting coin.
Every 5 coin collected, there is a new challenging plain.
Don't let the cute box fall down !
=: Gameplay video :=