GameMonkey Script

GameMonkey Script Forums
It is currently Thu Jul 19, 2018 3:21 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Dec 08, 2015 2:32 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Hello,

I made this new type to make classes possible (with inheritance and what not). This is suited better for an event oriented system. I will try explain better but here are the sources i've attached. One thing, you need to add this function to "gmThread" before you can use this lib:

Code:
bool gmThread::VerifyParameters(int paramCount, ...)
{
    va_list args;
    va_start(args, paramCount);
    if(m_numParameters >= paramCount)
    {
        for(int i = 0; i < paramCount; ++i)
        {
            gmType type = va_arg(args, gmType);
            if(type > GM_FLOAT || (m_stack + m_base)[i].m_type > GM_FLOAT) {
                if((m_stack + m_base)[i].m_type != type)
                {
                    va_end(args);

                    return false;
                }
            }
        }
        va_end(args);
        return true;
    }
    va_end(args);
    return false;
}


Anyways, so use simply include the file "ObjectLib.h" and register like this:

ObjectLib::Register(machine);

So here is an example script (obviously some non related libraries are being used in this example):

Code:
global obj_base_obj =
{
   X = 0.0f,
   Y = 0.0f
};

global obj_character =
{
   Parent = obj_base_obj,
   MoveX = 0.0f,
   MoveY = 0.0f,
   Image = null,
   
   Events = {
      OnStep = function() {
         this.X += this.MoveX;
         this.Y += this.MoveY;
      },
      
      //Draw the character image
      OnDraw = function() {
         if(this.Image) {
            Renderer.DrawImage(this.Image, this.X, this.Y);
         }
      }
   }
};

global obj_player =
{
   Parent = obj_character,
   Image = Image.LoadFile("player.png"),
   Events =
   {
      OnStep = function() {
         //Maybe can add more stuff here before calling the parent event..
         //Console.WriteLine("TEST");
         //Call the parent
         Object.CallInheritedEvent(this, obj_character, "OnStep");
      },
      
      //Events with sub keys call be called like this
      //Object.CallInstanceEvent(this, "OnKeyDown", "KeyRight");
      OnKeyDown = {
         KeyRight = function() {
            this.MoveX = 1.0f;
            
            //Can also call parent
            //Object.CallInheritedEvent(this, obj_character, "OnKeyDown");
         },
         
         KeyLeft = function() {
            this.MoveX = -1.0f;
         },
         
         KeyUp = function() {
            this.MoveY = -1.0f;
         },
         
         KeyDown = function() {
            this.MoveY = 1.0f;
         }
      },

      
      OnDraw = function() {
         //Maybe can add more draw functions here.
         
         //Call the parents OnDraw event
         Object.CallInheritedEvent(this, obj_character, "OnDraw");
      }
   }
};

//Create a new instance of obj_player
playerInstance = Object.CreateInstance(obj_player);
playerInstance.X = 64.0;
playerInstance.Y = 64.0;

//some back end stuff
while(true) {
  Object.CallInstanceEvent(playerInstance, "OnStep");
 
  Renderer.Clear();
  Object.CallInstanceEvent(playerInstance, "OnDraw");
 
  Renderer.Refresh();
}




Here's a better example and the output is below the code:

Code:
 global obj_parent = {
   Events = {
      OnCreate = function() {
         Console.WriteLine("Calling obj_parent.Events.OnCreate");
      },
      
      OnTests = {
         Test1 = function() {
            Console.WriteLine("Calling obj_parent.Events.OnTests.Test1");
         }
      }
   }
 };
 
 global obj_childA =
 {
   Parent = obj_parent,
   MyVariable = "Hello",   //Unlike functions and events, variables will be copied over into the instance itself
   Events = {
      OnCreate = function() {
         Console.WriteLine("Calling obj_childA.Events.OnCreate");
      },
      OnTests = {
         Test2 = function() {
            Console.WriteLine("Calling obj_childA.Events.OnTests.Test2");
         }
      }
   },
   
   DoSomething = function() {
      Console.WriteLine("Calling obj_childA:DoSomething");
   },


 };
 
 global obj_childB =
 {
   Parent = obj_childA,
   Events = {
      OnCreate = function() {
         Console.WriteLine("Calling obj_childB.Events.OnCreate");
         
         Object.CallInheritedEvent(this, obj_childA, "OnCreate");
      }
   }
 };
 
 instance = Object.CreateInstance(obj_childB);
 Object.CallInstanceEvent(instance, "OnCreate");
 Object.CallInstanceEvent(instance, "OnTests", "Test1");
 Object.CallInstanceEvent(instance, "OnTests", "Test2");
 instance.DoSomething();
 
 //Change DoSomething to a user variable just for this single instance
 instance.DoSomething = function() {
   Console.WriteLine("Calling instance DoSomething");
 };
 
 instance.DoSomething();
 


Quote:
Calling obj_childB.Events.OnCreate
Calling obj_childA.Events.OnCreate
Calling obj_parent.Events.OnTests.Test1
Calling obj_childA.Events.OnTests.Test2
Calling obj_childA:DoSomething
Calling instance DoSomething


Attachments:
ObjectLib.zip [2.7 KiB]
Downloaded 102 times


Last edited by 39ster on Tue Dec 08, 2015 4:57 pm, edited 6 times in total.
Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 08, 2015 2:39 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
So here is a better explanation.

I havnt tested it too well and will make updates if there are issues you guys find but it will call the correct events, and member functions. If the class hasn't defined an event/script, it will look for it in the Parent class, and also the Parents parent, etc

To create an instance of a class you do this:

Code:
instance = Object.CreateInstance(obj_player);
instance.X = 32.0f;
instance.Y = 32.0f;

//When you call a member function, first it will check the class, then the parents (and parents parent) to find this function.
instance.DoSomething();

//You may also override a member function per instance like this:
instance.DoSomething = function() {
  Console.WriteLine("BLAH");
};


To call an event do this:
Code:
Object.CallInstanceEvent(instance, "OnDraw");

//Or a sub-event
Object.CallInstanceEvent(instance, "OnKeyDown", "KeyRight");


Again with the events, it will first look for the event defined in the class, then it will look in the parent, and the parents parent


Last edited by 39ster on Tue Dec 08, 2015 2:54 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 08, 2015 2:44 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
It will also NOT copy each member function/event for every instance. Rather it will only store the class table and parent class, and then find the member function/event from the table stored. It will however copy over member variables (non-functions) per instance.

Also for each Instance created, there is an internal table that can store private members that can be accessed by whatever backed.

Code:

global obj_player = {

  Name = "Luke", //This non-function variable will be copied per-instance

  //These events, and functions will not be stored per instance
  Events = {
    OnAlarm = {
      Alarm1 = function() {
        Console.WriteLine("Alarm triggered");
      }
    }
  },

  //This function will not be copied per instance, BUT you can override it per instance simply do this:
  //instance.DoSomething = function() {}
  //And it will correctly use the overided function rather than the function stored in this table
  DoSomething = function(blah) {
    Console.WriteLine(blah);
  }
};

instance = Object.CreateInstance(obj_player);
instance.Name = "A different name for each instance";

//Some back end management of the alarms/timers


privateMembers = Object.GetInternalVariables(instance); //Get the table with the internal variables

//Create a table of alarms in the internal variables (these variable cannot be access using "get dot" on the instance)
privateMembers.Timers = {};

//Set an alarm
privateMembers.Timers["Alarm1"] = 1000; //1000 millisecs


//Iterate through each alarm and trigger the alarm event when the alarm reaches 0
foreach(key and value in privateMembers.Timers)
{
  value -= 10; //Subtract some value from the timer each update
  if(value <= 0)
  {
    Object.CallInstanceEvent(instance, "OnAlarm", key);
  }
}


This is poorly explained i know, but this class can be very useful for event oriented games. I will try explain it better later.


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 08, 2015 3:08 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Code:
 global obj_base_parent =
 {
   Events = {
      OnCreate = function() {
         
         internals = Object.GetInternalVariables(this);
         internals.Timers = {};
      }
   },
   
   SetAlarm = function(alarmKey, millisecs)
   {
      internals = Object.GetInternalVariables(this);
      internals.Timers[alarmKey] = millisecs;
   },
   
   UpdateAlarms = function(deltaTime)
   {
      internals = Object.GetInternalVariables(this);
      foreach(key and value in internals.Timers)
      {
         value -= deltaTime;
         
         internals.Timers[key] = value;
         if(value <= 0) {
            Object.CallInstanceEvent(this, "OnAlarm", key);
            
            if(internals.Timers[key] <= 0) {
               internals.Timers[key] = null;
            }
         }
      }
   }
 };
 
 global obj_derp = {
   Parent = obj_base_parent,
   
   Events = {
      OnCreate = function() {
         //Call the parent on create
         Object.CallInheritedEvent(this, obj_base_parent, "OnCreate");
         
         this.Timers = "blahh";  //This will not modify the Internal variable named "Timers" as those can only be accessed with Object.GetInternalVariables();
         this.SetAlarm("Alarm1", 1000);
      },
      
      OnAlarm = {
         Alarm1 = function() {
            Console.WriteLine("Alarm triggered");
            
            this.SetAlarm("Alarm1", 1000);
         }
      }
   }
 };
 
 instance = Object.CreateInstance(obj_derp);
 Object.CallInstanceEvent(instance, "OnCreate");
 
 while(true) {
   instance.UpdateAlarms(10);
 }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group