| Blockland Forums > Modification Help |
| Scriptgroup, Scriptobject |
| (1/4) > >> |
| heedicalking:
What would be the use of a scriptgroup? How would I go about getting members of each. I plan to make a system where a scriptgroup holds scriptobjects which each hold bricks. I wouldl ike to know how scriptgroups work. --- Code: ---if(!isobject(PRPGChunks)) $PRPG::Chunks = new scriptGroup(PRPGChunks); function Chunkgen(%grid) { if(%grid $= "") return; %chunk = new scriptObject() { class = PRPG_ChunkSO; grid = %grid; creatorBLID = 3291; }; $PRPG::Chunks.add(%chunk); } function PRPG_ChunkSO::onAdd(%this) //minigame initial setup { echo("created"); } function PRPG_ChunkSO::onRemove(%this) { echo("removed"); } --- End code --- How would I go about getting children of the group, removing children from the group, checking if children exist, getting a list of them, and so on. |
| Brian Smithers:
it's easy ;) --- Code: --- if(isObject(exampleGroup)) exampleGroup.delete(); new ScriptGroup(exampleGroup); function exampleGroup::exampleFunction(%this,%exampleNumber) { for(%i=0;%i<%exampleNumber;%i++) { %obj = new ScriptObject(); %obj.number = %exampleNumber; %this.add(%obj); } } function exampleGroup::dumpExampleObjects(%this) { for(%i=0;%i<%this.getCount();%i++) { %obj = %this.getObject(%i); %num = %obj.number; echo("Object: " @ %obj @ " Number: " @ %number); } } --- End code --- if you do for example exampleGroup.exampleFunction(5); it will create 5 example objects and will add it to the example group. then when you do exampleGroup.dumpExampleObjects(); it loops through all the objects inside the exampleGroup then it gets each object, finds the number in it, and echos it. Some quick notes ScriptGroup.add(%object) - will add an object to the group ScriptGroup.getCount() - will list the amount of objects in the script group ScriptGroup.getObject(%count) - it will get the object in the group. %count i think is like, the first object added to the group would be 0, the next 1, the one after that would be 2. etc. ScriptGroup.remove(%object) - removes an object from the group ScriptGroup.isMember(%object) - returns a boolean value, checking to see if the object is a member of the script group. |
| Brian Smithers:
Heres a quick chunk system i made, not tested but feel free to use it. function chunkObject() { return new ScriptObject() { class = "Chunk" }; } function Chunk::addBrick(%this,%brick) { if(isObject(%brick.chunkGroup)) { error("::addBrick(%this,%brick): " @ %brick @ " is in a group!"); return; } %brick.chunkGroup = %this; %this.chunk[%this.chunkCount++] = %brick; } function Chunk::clearBricks(%this) { for(%i=0;%i<%this.chunkCount;%i++) { %brick = %this.chunk[%i]; if(isObject(%brick)) %brick.delete(); } } function Chunk::getBricks(%this) { for(%i=0;%i<%this.chunkCount;%i++) { if(%str $= "") %str = %this.chunk[%i]; else %str = %str SPC %this.chunk[%i]; } return %str; } function ChunkGroup::getBricks(%this) { for(%i=0;%i<%this.getCount();%i++) { %chunk = %this.getObject(%i); if(%str $= "") %str = %chunk.getBricks(); else %str = %str SPC %chunk.getBricks(); } return %str; } function newChunkGroup() { %chunkGroup = new ScriptGroup() { class = "ChunkGroup"; }; return %chunkGroup; } function ChunkGroup::newChunk(%this,%bricks) { %chunkObject = chunkObject(); for(%i=0;%i<getFieldCount(%bricks);%i++) { %brick = getField(%bricks,%i); %chunkObject.addBrick(%brick); } return %chunkObject; } |
| Greek2me:
--- Quote from: Brian Smithers on March 23, 2012, 09:30:26 PM ---it's easy ;) --- Code: ---function exampleGroup::dumpExampleObjects(%this) { for(%i=0;%i<%this.getCount();%i++) { %obj = %this.getObject(%i); %num = %obj.number; echo("Object: " @ %obj @ " Number: " @ %number); } } --- End code --- --- End quote --- Just do %scriptGroup.listObjects(); It's a default method. |
| Brian Smithers:
--- Quote from: Greek2me on March 23, 2012, 09:42:53 PM ---Just do %scriptGroup.listObjects(); It's a default method. --- End quote --- i was showing how to get all objects in script group plus, it was a custom message. |
| Navigation |
| Message Index |
| Next page |