GameMonkey Script

GameMonkey Script Forums
It is currently Mon Nov 20, 2017 3:24 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sun Nov 13, 2011 1:38 pm 
Offline

Joined: Mon Feb 12, 2007 12:17 am
Posts: 13
Hello,

Currently, whenever a "type" (Such as vector3, player, etc) is declared to the GM VM, I take the type integer that's returned, and feed into a hashmap that returns my type information in the engine. This is also then "executed" in the VM as "global typename = typeid;".

I then create a function called "new", which takes this typeid. When it is called, it creates and returns an object, based upon the lookup of that typeid.

I was wondering if anyone has actually created new and delete operators, so instead of this:

pl = new(Player);

I could do

pl = new Player;

Thanks in advance!


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 26, 2011 1:37 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
I havn't tried, but it would be possible with a bit of work.

Why not just do

pl = Player();

or even:

pl = Player.New();


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 05, 2011 12:19 pm 
Offline

Joined: Fri Jun 19, 2009 12:39 pm
Posts: 31
I'd like to second what 39ster has said. There's no need to manage what type of object to create, because the VM does this for you! Just can just do the following in script code;

Code:
pl = Player();


I'd also strong advise against sepcifically deleting objects in script code. Doing this could cause issues with variables that reference the user objects in other parts of the script. Instead, just ensure that your variable is no longer referencing the object with :-

Code:
pl = null;


My own code base lets the VM manage the allocation and deletion of objects, which I have to add, makes coding in script very easy and trouble free. I tend to find that it's when I try to replicate any of the existing functionality in the VM that I run into headaches!

As an example, this is some script code from my engine that rotates a vector using user types;

Code:
focusPoint = Vec3("0 0 "0);
eyeVec = Vec3("0 2 -6");

rot = Mat3();
eyeVecRot = rot.setRotation(sVec3("0, 1, 0"), angle);


See? There's no need for anything like new, as the VM knows what type I want to allocate. And although I don't delete the objects after I use them, they do get cleared up eventually by the GC so memory is managed pretty well.


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 06, 2011 12:43 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 698
Just a quick comment on this...

Although the described work around is reasonable eg. pl = Player(); It still requires binding of a same named function to return the new instance. Since types could be created dynamically from script or native code, there is justification to implement something like operator new in future, as a standard way to create a new instance. (It happens to be in our design notes for a possible future version of GM.)


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