4 Replies - 754 Views - Last Post: 25 June 2016 - 01:20 PM Rate Topic: -----

#1 Fraysa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 12

Which approach to use for a scripting interface?

Posted 25 June 2016 - 12:10 AM

I'm developing a game server for a MMORPG in C#.

The game has many NPCs which you can interact with by clicking on them, and then initiating a conversation using dialogs with several buttons (ok, next, yes/no, etcetera).

I decided to use MoonSharp as my scripting interface for this matter.

Now - I have to decide which approach I'd like to use for it. Basically, when you click on a NPC the client sends it's ID and then the server knows which script to initiate. Some NPCs require more than one dialog, so you have to send a few. Once you send a dialog (which is through a network packet) the client displays a window to the client (like a MessageBox) and waits for the client's input. Once the input is received, the server processes the input (0 or 1, depending on what button the user pressed) and proceeds to display the next dialog. So, I have two options to make this work:

1. Run the main function of a script every time with the current state. For example - when you click on a NPC it runs the main function with state '0' and finishes execution. Then, when the user sends the input, run it again with state '1', and so on. Here's an example of a script that would work with that approach:

function main(state)
	if state == 1 then send("Press next to continue!")
	elseif state == 2 then send("Great!")
	end
end



That approach seems to be the safest and easiest, however it's a pain to write and handle the states.

2. Halt the code execution by blocking the execution thread and resume it once the user has given input. Here's an example of a script that would work with that approach:

send("Press next to continue!");
send("Great!");



Basically, the send method returns a boolean indicating if the user has pressed next or no. When the method executes, it blocks the thread and when the server processes the user's input it resumes it.

So - the second approach seems like the best for me but the server is supposed to run over 10,000 concurrent users - so it seems a bit risky running so many blocked threads in potential.

Which one is a better approach and perhaps there's another one I'm missing?

If you require more information let me know!

Thank you and have a great day.

Is This A Good Question/Topic? 0
  • +

Replies To: Which approach to use for a scripting interface?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Which approach to use for a scripting interface?

Posted 25 June 2016 - 06:41 AM

With that second approach, also consider what happens when the server gets rebooted. How will you figure out which threads were in a paused state and which were running?

The way I would approach this is by having state machines that can be persisted and rehydrated from a data store. As inputs/events come in to change states, the state machine is either found in a in a cache or rehydrated from the store and the updated.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Which approach to use for a scripting interface?

Posted 25 June 2016 - 10:41 AM

I think you are looking for a behavior tree. Try and search for "Game AI behavior tree" and see what you can find.
They work very similar to a state machine (they sorta are). Try and find a game library/framework where it is already implemented.

I came around one once, and they had great docs with examples, but sadly I can't remember where :(
Was This Post Helpful? 0
  • +
  • -

#4 Fraysa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 12

Re: Which approach to use for a scripting interface?

Posted 25 June 2016 - 01:01 PM

Can you come up wit han example that demonstrates that using my example?

This post has been edited by andrewsw: 25 June 2016 - 01:21 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,569
  • Joined: 12-December 12

Re: Which approach to use for a scripting interface?

Posted 25 June 2016 - 01:20 PM

You've been given some good advice and direction, it is now up to you to search, and research, and come up with your own example, or perhaps to ask a follow-up question. Do not ask someone to write code for you nor to do your searching, and work, for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1