Skip to main content
gamedev.net
🔒 Locked

Adding scripting to engine

Started by Shuger 1 reply 650+ views
Original Post
Shuger
Shuger
Hi, I've been working on my game and realised that hardcoding and simple data files are not enough. I tried to avoid hardcoding as much as i could but i found it difficult - if not impossible - to do. At that point i decided to implement some type of scripting into my engine. I decided on Lua. However i'm having some design issues. Example: I want to make main menu screen. The way it's now: Data class loads graphics, update class(it's polymorphic) checks for clicking and if any of the buttons is clicked it calls respective functions. code:
if( intersect(Data->Button1,Data->Mouse_Cursor) && Input->Mouse_click())
{
     Manager->PushState(new cGame);
}
It's really bad becaouse Data has Button1 pointer and such, instead of some general entity collection. How can it be solved using scripts? How far should scritping go, should it just be runned when winning conditions are met or maybe it should control whole game flow? If you have any links to tutorials or articles about a design part of incorporating scripts into engine i would really aprreciate it. I've searched web for a long time but i just can't find any decent tutorials on this topic.
Red Ghost
Red Ghost
Hi,

Reading from your example, I am wondering if you need scripting. Let me show a non scripted solution one based on your User Interface(UI) example:

you could have a file holding a description of graphical representation of any kind of widgets you have (only describes the general look of the UI):
- buttons use this bitmap
- dialogs use that bitmap
- etc.

You also could have a file describing your dialog box (or menu entry) with a unique ID for each widget composing your dialog box (like a windows resource script). For each widget, it also describes the message to be sent when the user selects the widget. The format of the file could be:
WIDGET_TYPE, WIDGET_ID, WIDGET_POSITION_AND_SIZE, MESSAGE_ACTION, MESSAGE_DESTINATION, MESSAGE_DATA_1, MESSAGE_DATA_2

Let's imagine you have a main system class identified in the messenger system by the unique ID 0. This system class has an Execute method which will load and execute a game state according to its ID:
- 100 begin new game state
- 200 reload game state
- 300 quit game state
Now, when you want to create a main menu screen, your widget file would be:
MENU_ENTRY, 1, position and size of this widget, Execute, 0, 100, 0
MENU_ENTRY, 2, position and size of this widget, Execute, 0, 200, 0
MENU_ENTRY, 3, position and size of this widget, Execute, 0, 300, 0

Imagine you click on the first menu entry widget: this widget will send a message to the main system class (MESSAGE_DEST = 0) with the action Execute and the ID 100. The main system class receives this message, calls the execute method with 100 as argument. This loads up the begin new game state.

If you need a scripted UI, there isn't much difference. Instead of mentionning the message elements in your widget file, you just mention the script to be executed. It is interesting to use a scripted effect when clicking on a widget whenever the reaction of the UI is complex and non standard. Let's say you have an adventure game where you must select between two options (two menu entries here). Clicking on the menu entry will provoke a change of music, a start of a particle system and a change of the player characteristics.

Hope that helps.
Ghostly yours,
Red.
Ghostly yours,Red.
Shuger
Shuger
Thanks!
It was really helpful. The biggest problem i had was about how can object A refer to object B without hardcoding this dependency. Using Id's solves this problem in simple manner, And allows to simplify loading of dependencies.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.