Problems with pointers inside actions

Posted By: Florastamine

Problems with pointers inside actions - 04/25/16 03:00

I've created a simple action which displays an arbitrary text whenever the player comes close to the entity, and the string to be displayed is defined in string1. The problem is, whenever I assign the action to 2 or more entities in the level, when displaying the text, it appears to display both the old and the new content, overwriting each other. The action's code has some local pointers initialization at the header, like this:
Code:
action act_notepad()
{
    TEXT *my_text = txt_create(1, 1);
    ...
    // When the player comes close to the text, display it.
    // Hides the text if the distance between the player and me is larger than 242.0.
    // Destroys the text if the level is no longer valid.
}



Here are a few screenshots to make it clearer what I mean. I have two entities in the level, they share the same action, with different string1 on each entity:

First time, the text just do the correct thing:


But when viewing the second text, the problem emerges:


I know the problem lies somewhere in the local TEXT object inside the action definition, but I don't have a solid reason as well as how to solve this as I have no idea how "action" works and how these local objects are stored in Acknex.

Have anybody encountered something like this before? Any insights is appreciated.
Posted By: alibaba

Re: Problems with pointers inside actions - 04/25/16 05:34

I guess having an insight at the actual code would be better in order to help you, instead of just speculating around.
Posted By: Florastamine

Re: Problems with pointers inside actions - 04/25/16 06:40

Thanks, I've tried dumbing down the project to the point that it only contains the relevant code in order to re-produce the issue, but so far no luck, the problem is still there. I can upload the project here if you want to take a look.
Posted By: Florastamine

Re: Problems with pointers inside actions - 04/25/16 07:53

Here's the oversimplified project which only contains some basic code to get the game up and working. The faulty code is located in ./source/game/ai/behavior_static.c Press [0] to unlock the camera and move around. You can see that both the old and the new text objects overwriting each other, although I've hid them in the else {} branch in the code.

https://dl.dropboxusercontent.com/u/26857618/DPFL2.rar
Posted By: alibaba

Re: Problems with pointers inside actions - 04/25/16 10:15

I took a quick look at the code (I have not 3DGS here right now), but in order to use the "hit"-Struct, you need to set the flag "SCAN_TEXTURE". Try it out. I'll take a second look when I'm back home.
Posted By: Florastamine

Re: Problems with pointers inside actions - 04/25/16 12:08

As far as I know, setting SCAN_TEXTURE only fills the hit struct with texture information. The struct itself can still be used (only texture information is unavailable). In my code I only use hit->entity, which points to the target entity.
Posted By: Superku

Re: Problems with pointers inside actions - 04/25/16 13:41

Did not quite read everything but...
I usually only have one global dummy TEXT. When I want to render text, for instance for individual signs, dialog with NPCs and so on, I copy the string(s) to that TEXT object and use draw_obj(...) to draw it.
IMO that's a much cleaner approach as you don't have to manage text creation, delete and set/ reset the SHOW flag (for example on level change).
Posted By: jcl

Re: Problems with pointers inside actions - 04/25/16 14:24

>>TEXT *my_text = txt_create(1, 1);<<

The problem is that this creates a text any time you call that function, and thus will quickly overwhelm your game with lots of TEXTs. I don't know if this is the reason of your problem, but the proper way to create something in a more-than-once called function is:

static TEXT *my_text = NULL;
if(!my_text) my_text = txt_create(...);

Objects that are global to the whole game, such as a TEXT object, should be normally created in the main function and not in a local action.
Posted By: FBL

Re: Problems with pointers inside actions - 04/25/16 20:52

...or make sure to use ptr_remove() on the TEXT* when hiding it again.
Posted By: txesmi

Re: Problems with pointers inside actions - 04/26/16 06:05

I am sure both notes are inside 'my->distance' range and 'ent_get_type(hit->entity) == STATIC_NOTEPAD' gets true no matter wich note camera is looking to, so both texts are shown.

Modifying that comparision to 'hit->entity == me' will solve the problem since you know 'me' is a note already.

Anyway your code performs a c_trace for each note. That is a pretty wasteful way to go. Try to call a single 'c_trace' for every scaneable entity just after camera movement inside cameras function. The use of 'c_ignore' is a good habit too.

Salud!
© 2024 lite-C Forums