display text in response to two properties

How would i display text if both property strings have the same string in them?Here is an image of what
I mean.

Attachments


Have two property changed sensors. Connect them both to two expression controllers (so you have four wires; each controller is connected to the two sensors). The first expression:


prop1 = prop

will trigger the show actuator.

The other expression:


prop1 != prop

will trigger the hide actuator.

The property names must be valid as python identifiers, meaning they can’t have dots or spaces in, like you have in your example.

How would it do it python?I forgot to ask that.I have been trying to make the text show up in the bge in response to the two propertiesin python.But I could only get it to display in the console.

You need to be more precise with your questions. It seems you are actually asking to draw text in the screen if some condition (irrelevant) is met.

To draw text, you can use the blf module (which is a little complicated for a beginner), or just use a text object parented to the camera for now (I recommend).

To test if the two properties are equal, you need to understand how to check if two variables are equal, how to access Game Object properties, and put it all together.

I usually use in python for that.That is for checking if a string is in a sentence.

I usually use in python for that.That is for checking if a string is in a sentence.Which I want to do the same thing with two properties.Then have a text object display a message.

Yes, where are you getting stuck?

Displaying the text in a text object instead the console.That is the part I am having trouble with.

No I did not look at that page.

The program only compares one set of two properties.Not the first set.why does it do that?

from bge import logic


cont = logic.getCurrentController()
own = cont.owner


dialogFile = own["dialogFile"]
dialogFile = logic.expandPath("//"+dialogFile)


with open(dialogFile) as d:
             dialog = d.readlines()
             if own['2name'] in own['text']:
                own.text = dialog[0]
             if own['fathers_name'] in own['text']:
                own.text = dialog[2]
             else:
                own.text = dialog[7]
print(lines)
d.close()

Try reading the code as an instruction manual:

  1. You perform one test for the value of 2name in the text property. If it contains the value, you set the display text to dialogue[0].
  2. You then perform one test for the value of fathers_name in the text property. If it contains the value, you set the display text to dialogue[2]
  3. Otherwise, if 2 didn’t find the value in the property, you set the text to dialogue[7]

Any ideas?

Hint, with programming, you must anticipate potential bugs before you encounter them, by thinking about what data you expect to input into the program, and how it responds. The best tactic is to always ensure you’re asking the computer to do exactly what you want, rather than roughly what you want, e.g instead of checking if the player is dead inside the movement script (so it can’t move when dead), you instead want the movement script to only run when the player is alive. These are not the same things. A better way of introducing this behaviour is to have a generic way of disabling movement so that later, if you add paralysis spells, you don’t need to change existing code.

I tried classes but it did not work.

It has nothing to do with classes… did you fully read my reply? Think about what your data is going to be, and then pretend you’re the Python interpreter running the code with that data.

In this case, the data is the values of the fathers_name, 2name, text

I finally made my sophisticated dialogue system.I had to use dictionaries.
Here is the code.

from bge import logic


cont = logic.getCurrentController()
own = cont.owner



dialogue = {"Choice1" : "Hello!",
       "Choice2" : "How are you?",
       "Choice3" : "His name is bill fye."}
if own['2name'] in own['text']:
   own.text=dialogue['Choice2']
if own['fathers_name'] in own['text']:
   own.text=dialogue['Choice3']



text_object["text"] = dialogue[own.name]

Why doesn’t elif plus else work in a program.It seems that the bge is restricting what can be used from python.

Your observation is incorrect :wink:

It did not work when i did it.It spit out a error.

What’s this nonsense? elifs and elses work perfectly fine.

That is what i mean’t.
Here is the error i get and the script.

Python script error - object 'Text', controller 'Python':
Traceback (most recent call last):
  File "Text.001", line 33, in <module>
KeyError: 'Text'


from bge import logic



cont = logic.getCurrentController()
own = cont.owner






dialogue = {"Choice1" : "Hello!",
       "Choice2" : "My name is Carnun.",
       "Choice3" : "His name is bill fye.",
       "Choice4" : "Her name is cris.",    
       "Choice5" : "I live in brimstone.I love to eat at a great restaurant named fireback there.",
       "Choice6" : "I prefer cloudy rainy days instead of sunny days.", 
       "Choice7" : "I do not understand you."} 



if own['2name'] in own['text']:
   own.text=dialogue['Choice2']
if own['fathers_name'] in own['text']:
   own.text=dialogue['Choice3']
if own['wifes'] in own['text']:
   own.text=dialogue['Choice4']
if own['lives'] in own['text']:
   own.text=dialogue['Choice5']
if own['sun'] in own['text']:
   own.text=dialogue['Choice6']
else:
   own.text=dialogue['Choice7']  






text_object["text"] = dialogue[own.name]

Someday the general population on this forum will know how to properly use Python. Today is not that day.

By the way, what kind of extension is .001, you should use .py for python scripts.