prologprolog-directive-dynamic

Prolog getting dynamic fact values


While learning Prolog, I am writing a text-based game, this is some of it:

NewHealth is Health - Damage,
retract(stat(Target, health, Health)),
assert(stat(Target, health, NewHealth)),

I got an error about static procedures but a simple search fixed it, when running the game I would just quickly say dynamic stat/3. After that however, I get an error further up in the file where I declare the Health variable:

stat(Target, health, Health),

I narrowed down the problem to this (prolog console):

| ?- assert(test(a)).
yes
| ?- listing
test(a).
yes
| ?- dynamic test/1
yes
| ?- listing
yes

After being declared dynamic, it isn't in the static database anymore. But with a google search a couldn't find a case that has the same problem as mine. I just need to query and retract/assert a single database.


Solution

  • dynamic/1 is a directive. Place it near to top of your file, with this syntax

    :- dynamic stat/3.