Displaying text when player goes near object [solved]

dacharya64

  • Posts: 6
I'm still new to Stencyl and trying to figure out how to do everything I need for this RPG. One thing I want to do is when the player actor walks up to an object (I guess this would have to be another actor too) text will display above that second object. For instance, if the player walks over to a bookshelf, above the bookshelf text would appear that says "Read the book" and if the player walks away the text will disappear.

I thought I was halfway there when I modified the "Die on Collision w/ Actor Type" behavior so that instead of the actor dying, it'd display the second actor (the text). But when the player moves away the second actor is still there.

I then tried to make it so the second actor appears only when the player is within a certain region, but couldn't really figure out how to do it.

Any help would be much appreciated!

« Last Edit: July 02, 2012, 05:00:08 pm by dacharya64 »

hansbe

  • Posts: 262
If you set some boolean attribute to true after the player enters a regioin or collides with the bookshelf, you can use a draw text block. Drawing blocks are only available in "when drawing" events

dacharya64

  • Posts: 6
Okay, so specifically about the boolean thing:

I created a boolean called "in_contact" and created a "when drawing" to say if in_contact is true, draw some text at 0,0. In that same behavior, I said if player hits box, then set if_contact to true. But this doesn't seem to have any perceivable effect. What's wrong with this code? Also, will this make the text disappear when the player is no longer colliding with the box?

Kajitii

  • Posts: 184
I haven't messed with fonts outside of Crash Course 2, so I might be wrong...

You might need to set a font for the text to display, or else it won't display anything.  The text will also never turn off, since you never set the boolean to false.  Since the text will display when the boolean is true, and it will always be true when you trigger your event, it's going to stay there.

hansbe

  • Posts: 262
Kajiti might be right.
And here are some other things to worry about:
- The text will be drawn relatively to the actor on which you placed the drawing event on, so if the actor is in the bottom right corner it won't show. (If you are using it in a scene behaviour it might draw it relative to the scene, in the top left of it, which might be outside the screen. (You can translate coordinates to the screen with a block)).
- You should check the collision groups, so that the groups which player and box are members of get tested against each other.
Maybe try drawing text outside the "if", to see if you can see i first.

dacharya64

  • Posts: 6
I double checked and the actor behavior wasn't working at all. I changed it to a scene behavior, and now the text does appear when I hit to box! (I didn't have to set a specific font to display, though).

Now for rules setting the boolean to false: is there a way to continuously check if the player is colliding with the box? Is that the best thing to do in this situation?

Thanks so much for your help thus far.

froz

  • Posts: 250
Everything always is drawn at x/y relative to the actor to which the behaviour is attached. So for example if you draw at 100,100 and your actor is at 100,100, the drawing/text will appear at 200,200. That's probably why you didn't see the text before. You can change it by using "switch to screen space" block. So you can keep the behaviour attached to the actor and don't have to move it to the scene.

I haven't done anything like this really, but instead using collisions, I would create a region in the item (f.e. bookshelf) actor behaviour, a region little bigger then the actor itself. Then I would use "when actor enters/exits region" to show/hide text.

This way it will also be much simpler to create different texts for different items.

dacharya64

  • Posts: 6
I wanted to to it by region control at first, but couldn't really figure out how to do so. I'm pretty sure its done through the "when actor enters/exits region" but when I try to use that, I can either choose for the region space "last created region" or a region attribute. Choosing "last created region" doesn't seem to do anything, whereas I know how to create a region attribute, but not how to correlate that attribute with the region in the scene. For instance, if I have a region called "Region 1" how can I set the rule to apply when the player enters region 1?

Sift

  • Posts: 47
You could try a behavior for the item that says

When created:
set [text attribute] to alt + 255 (so it's blank)

when drawing
draw [text attribute] at x, y
if x of [target attribute(player)] > x of self - number
if x of [target attribute(player)] < x of self + number
set [text attribute] to whatever you want
otherwise
set [text attribute] to alt+255

Edit: You can also add
if y of [target attribute(player)] > y of self - number
if y of [target attribute(player)] < y of self + number

if it's a bird's eye view type of game

might work?

« Last Edit: June 08, 2012, 01:19:55 pm by Sift »

solleader

  • Posts: 44
oK i just done the same thing on my game: SIMPLE.
So u need to:
1- make on a graphic software an actor that is the thing you want to display (with text).
2-Put that in the scene where u want to see that
3- make a region where the actor need to be to trigger the popo up.
4- In the scene behaviour made the actor pop-up text to hide on always, and "when an actor enter in the region" set to show the actor.

froz

  • Posts: 250
Quote
I wanted to to it by region control at first, but couldn't really figure out how to do so. I'm pretty sure its done through the "when actor enters/exits region" but when I try to use that, I can either choose for the region space "last created region" or a region attribute. Choosing "last created region" doesn't seem to do anything, whereas I know how to create a region attribute, but not how to correlate that attribute with the region in the scene. For instance, if I have a region called "Region 1" how can I set the rule to apply when the player enters region 1?
Make an attribute of region type and set it to the last created region in the event where you create that region, just after that. Later use that attribute in the when actor enters/exits region (or better "when actor of type enters exits region", so you can choose your player's type). It should work.

Method posted solleader would require separate graphic for every text, I wouldn't do it.

Sift's method can be good too, although I would change a bit:

Code: [Select]
if ((absolute value of (x of self - x of player) < distance to show text) and (absolute value of (y of self - y of player) < distance to show text))
set show text? to true

and in the draw event
if show text?
switch to screen space
draw text blabla at x something y something

However, this would require you to let the item somehow know which actor is your player.
You should also consider what will happen if you have 2 items close to each other and player would trigger behaviours to show text for both items in the same time.

dacharya64

  • Posts: 6
I tried out what you said, Froz, and it worked! I'll have to test it out with multiple objects now, but for now my problem is solved! Thanks so much to all of you.

« Last Edit: June 09, 2012, 08:49:31 am by dacharya64 »

captaincomic

  • *
  • Posts: 6108
Just a little note, in the "enter/exit region" events you should probably use Region 1 instead of "Last Created Region", because if you were to create another region later "Last Created Region" would change.


dacharya64

  • Posts: 6
The earlier game I was working on was basically an edited form of the sample rpg game. I figured I'd start a new blank game and would be able to implement the same features. I've done all the same things, but I can't get the text to show up when the player walks into any specific region. I'm going to go ahead and attach the whole game file.

(the "nome" that appears when the game is loaded is only there to make certain that text /can/ show up).


EDIT: Okay, I fixed this. It had to do with setting a certain attribute as text instead of a boolean.

« Last Edit: July 02, 2012, 04:59:44 pm by dacharya64 »