In this post I will document a few handy scripts and tips that my son and I have figured out while using the fun game development tool RPG In A Box. I strive to create scripts that are reusable and read values from the context they were called from.
Damage/Harvest An Entity And Give a Resource
- First, add an object to your map, in this example an asteroid.
- Set a property on the object called “hp” with a numeric value set to how strong the object is. For example, 3. Make sure it has a “death” animation.
- Ensure the player character has a “gather” animation created to animate them destroying the object and gathering resources.
- In the Scripts section for the object, triggered when “Character interacts”, attach the following
damage_asteroid
script:
// Destroy an object and collect resources from it.
play_animation(player, "gather");
wait(0.25);
// Deduct HP from the object.
if( self != null ) then
$currentHp = self.property["hp"];
$newHp = $currentHp - 1;
set_entity_property(self, "hp", $newHp);
// If HP is 0, destroy it and increase resources.
if( self.property["hp"] <= 0 ) then
// Destroyed!
play_sound("explode1.wav");
play_animation(self, "death");
wait(0.3);
if( self != null) then
remove_entity(self);
end;
execute_script("give_resources", true);
end;
end;
Because this script refers to self
, it is generic and can be attached to any destroyable object that gives resources. Several times we check if self is null because if the player is attacking quickly, the script can be triggered on an entity that was already removed and no longer exists.
If the asteroid’s entire HP is used up, then along with destroying it, we give the player some kind of resource (money, points, ores, etc) by executing the give_resources
script (note that in the Stats editor we add a new character stat called “resources”.
// Update the player stat 'resources' variable by 1. Show a message if threshold exceeded.
wait(0.1); // Bug workaround.
$currentResources = global.property["resources"];
$newResources = $currentResources + 1;
global.property["resources"] = $newResources;
// Check if first resource:
if( $newResources == 1) then
display_message("You found a resource! Keep blasting!");
end;
// Check if final resource:
if ($newResources == 5) then
complete_quest("QUEST_0001");
display_message("Quest is completed.");
end;
Load a Map And Switch Character Model
In our game we wanted to land on a planet and ‘exit’ our ship and have the player now look like an astronaut character instead of the ship character. To do this, we first create the second map and under the map properties we set the Player Override property to “astronaut”.
In the map we are coming from, we place an object to interact with (ex a planet), or tag a tile which can be stepped on, and give it properties for the map to load, the X, Y, Z coordinates to load the player on, and the sound effect to play when switching maps (example a landing sound effect).
Then when the player Interacts with the object or steps on the tile, it executes this simple script:
// Load a new map by reading destination info pulled from the triggering object.
wait(0.10); // Bug workaround.
// Read destination target info from triggering object.
$mapTarget = self.property["mapTarget"];
$x = self.property["landX"];
$y = self.property["landY"];
$z = self.property["landZ"];
$travelSound = self.property["travelSound"];
// Play the travel sound effect and change map.
play_sound($travelSound);
load_map($mapTarget, coord[$x, $y, $z], SOUTH);
We can then travel back to the the orignal space map the same way, and ensuring we set the Player Override property, we will be back in our ship model once in space.
I hope these are helpful! I’m planning to add more as I learn myself.