playMaker

Author Topic: Generate FSM Variables at Runtime by Parsing a .TXT File?  (Read 1667 times)

westingtyler

  • Sr. Member
  • ****
  • Posts: 277
    • My Video Game Projects Web Site
Generate FSM Variables at Runtime by Parsing a .TXT File?
« on: November 29, 2017, 08:53:01 AM »
I am stumped how to do this.

I can do a raw load into a string of a whole text file using EZ Save 2.

Then I can parse it using indicators like "if next character is a /" then save the last snipped as a new string entry in an array." adding a new string entry for each new /entry, up to infinity?

Okay, so:
My goal is to be able to write all my npc attributes code in a text file using like Notepad++ or Sublime, then load that file during runtime on a "blank" NPC character prefab to set all its properties, from its model and texture to its bark voice, to its walk speed, to its conversation topics and responses.

Basically

The problem is that my system is broader and more abstract than that; I don't have NPCs in my game, I have "entities" which can range from a potted plant to a blacksmith to a door, all of which can have wildly varying traits.

Some entities will be able to move and will therefore need variables for speed, frequency, footstep sounds, etc.

Some entities will be able to converse and will therefore need variable lists for an undefined number of possible text responses and query words.

I COULD make a single "Entity Blank" FSM on the prefab that contains 1 billion variables to describe everything from potted plants to knights and dragons, but in most cases, if I just have an FSM with all possible traits on every single entity, there will be a LOT of bloated FSMs with blank variables that are never used.

Therefore it would be nice if I could scan the entity's profile text file for 'CAN MOVE' then, if that trait type exists here, then create a speed variable and set it to what the text file says, and do the same for a footstep sound, a speed frequency.

I COULD have different fsm prefabs with pre-created blank variables representing every possible attribute for a given trait, (a "Can Walk" FSM with three variables for it)... but what about cases where, say, the entity can only walk over certain terrain types that I need to write out a list of, in its text file ahead of time? what if there are ten blocked terrain types? what if there are 20? what if most moving entities only have 3? do I just make an FSM for blocked terrain types, with 100 blank variables, just in case I need that many someday for some random special npc? Should I use an array if a potential trait list could be 0 to infinite?


Thank you for reading this if you did. I am not sure the best way to approach this issue or if I've been clear. And since I have so many characters, doing it wrong from the beginning could massively multiply my workload down the road.

What do you recommend?
« Last Edit: November 29, 2017, 08:56:32 AM by westingtyler »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Generate FSM Variables at Runtime by Parsing a .TXT File?
« Reply #1 on: November 29, 2017, 01:48:57 PM »
Hi,

 Interesting. Seems to me you need XmlMaker.

- infinity... maybe not, but indeed on IOS, a 36Mb text file holding xml is parsed and accessible in memory in a split of a second ( If I recall, 0.2s to load up) so you can go for it. However you should always make sure it's worth it and how to optimize as much as possible.

- attribute loading is a good idea.

- Each entities will likely be prefab, but you could have your prefab made out of smaller prefabs and the main prefab act as a stub that will instantiate more features each in their own GameObject parented to it. Then you can really go wild.

- The main thing is not complexity but maintaining a strict convention for naming, access to data and event. For example I alwasy resort to a "MetaData" Fsm for these kind of situation where that fsm is a convention for certain types of object that others know and are sure to find so that they can query it, same with events, make sure you have well written and explicit events all around.


So yeah, take a "Divide and Conquer" approach, it always works better than big fsm that does-it-all... and build it modular, think like Lego bricks, you can't go wrong. Small Fsm, very clear task. And don't hesitate to just have as many as required.

I am working on a complex Nintendo Mii type of sample at the moment that I think will help you along this to see how I approach these, it's not exactly the same goal, but I think it will help you figuring it out.
 I'll share very soon that sample, then I think it will be good you study it and come back with questions possibly.

Twitter link to MeFace Project

 Bye,

 Jean

westingtyler

  • Sr. Member
  • ****
  • Posts: 277
    • My Video Game Projects Web Site
Re: Generate FSM Variables at Runtime by Parsing a .TXT File?
« Reply #2 on: November 29, 2017, 03:02:06 PM »
Hey, that's great. I look forward to checking out the example, and in the meantime I'll be working out the details using the multiple prefabs option!

westingtyler

  • Sr. Member
  • ****
  • Posts: 277
    • My Video Game Projects Web Site
Re: Generate FSM Variables at Runtime by Parsing a .TXT File?
« Reply #3 on: November 29, 2017, 06:57:14 PM »
I have another related question. How do i speed up the load time when loading and parsing the text block I've ripped from the text file?

The process takes characters from the beginning of the text file and builds them into strings until they hit a carriage return or a /, for example, which means there are a small set of events that loop many, many times. i can let this happen, and it's fast, but then it breaks and tells me the loop count is exceeded.

the only way around this I know of is to add a next frame event at the start of each loop, but this slows down this loading process substantially. what is the best practice for this? is it safe to just tell the fsm to override the loop count? or what?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Generate FSM Variables at Runtime by Parsing a .TXT File?
« Reply #4 on: November 30, 2017, 01:06:19 AM »
Hi,

 Time and break loop exceeded warning are different and do not affect one another.

you should go with a proper parsing system using csv, json or xml and rely on this to parse your data. Using DataMaker is likely the goto choice here.

 Can you give us a sample of the text content?

you can increase the loop count limit in your fsm using the "Max Loop override" in your fsm inspector.

Bye,

 Jean