Ok, I just had a large ''ding'' go off in my head and a lightbulb appear: I think I''ve just worked out how a modding a game works. I''m pretty sure I''ve got it right, but just to clarify I wonder if you could tell me if I''m correct or not?
Here goes.
You have a game, and in that game there are different engines such as physics and renderer. Each engine has a set of functions in it for doing different things, so for an example the physics engine may have:
function MovePlayerForwards(PlayerPosition: TPosition): TPosition;
begin
// move player 1 unit forwards
PlayerPosition := PlayerPosition + 1;
// return the new player position
Result := PlayerPosition;
end;
Now this function is stored in a dll called ''originalPhysics.dll''. If a person playing the game thought "That''s a cool game, but I want to make the player move differently and create my own mod", then all they would have to do is make another dll, say ''FasterPhysics.dll'' and make a new movement function thus:
function MovePlayerForwards(PlayerPosition: TPosition): TPosition;
begin
// move player 10 units forwards
PlayerPosition := PlayerPosition + 10;
// return the new player position
Result := PlayerPosition;
end;
As long as they keep the function name, variables and return type exactly the same as the original function then this should work, yes?
Obviously there would have to be a mechanism to load a custom dll for a mod, and people wishing to create mods would have to be able to see all the appropriate function names, variables and return types.
Now if my thinking is correct from above, then my next question has to be that of "How do you create a mod using a Virtual Machine (VM) ?" Before anyone answers this I would like to first make sure that what I consider a VM is in fact a VM.
To me a VM is a class that has public methods that are all derived from a certain type, and handled by a command interpreter, ie:
// declarations
type
TCommandFunction = procedure(CommandLine: String);
TCommandParser = class
...
public
procedure DispatchCommand(CommandLine: String);
procedure Say(CommandLine: String);
procedure Load(CommandLine: String);
...
// implementations
procedure TCommandParser.DispatchCommand(CommandLine: String);
var
CommandName: String;
TempCommand: TCommandFunction;
begin
// copy first word from commandline
CommandName := Copy( CommandLine,1,Pos('' '',CommandLine) );
// remove first word from command line
Delete( CommandLine,Pos('' '',CommandLine),Length(CommandLine) );
// assign address of function with name = commandname to tempcommand variable
// i realise this is a Delphi specific function, but i have no idea what the equivalent is in C
@TempCommand := Self.MethodAddress( CommandName );
// check if function exists
if TempCommand <> nil then
TempCommand( CommandLine ); // function exists, dispatch command with argument
end;
procedure TCommandParser.Say(CommandLine: String);
begin
// update editbox2
EditBox2.Text := CommandLine;
end;
procedure TCommandParser.Load(CommandLine: String);
begin
// load world from disk
MyWorld.Load( CommandLine );
end;
Say a program has: EditBox1, EditBox2 and Button1 on a Form, and an instance of TCommandParser called (novelly) MyCommandParser. Now, a user types "say hello" into EditBox1 and clicks Button2, sending the the text "say hello" from EditBox1 to the ''DispatchCommand'' method of MyCommandParser. DispatchCommand then chops out the first part of its ''CommandLine'' string leaving us with "say" and "hello". DispatchCommand then checks to see whether a function exists within its class called "say" (which it does), it then calls the ''Say'' function with its CommandLine equal to "hello". The Say function recieves this and updates EditBox2 with the text "hello".
My base for the above is from some code/ideas by Tom Nyudgens in
this article.
Now taking the above definition of a VM into account, the best I can think of how a mod is created in VM code would be to have some kind of encrypted text file that stores the commands and then they are somehow loaded from disk and used; I really am a bit lost with this, would anybody care to elabore on how [implementing a VM based mod] it would be done (providing, that is, my definition of a VM is indeed correct) ?
Thanks for taking the time to read this.
--
Cheers,