Dev Blog / Script Follow Up – Breaking Victor’s Scripts

“Breaking” isn’t really the right word, because Victor’s script will still work as intended. Rather, what I’m doing here is hooking into it such that I don’t need to use a comment call.

As explained a few weeks ago, Victor’s scripts all use a “comment call” function to work – so you write a comment about lights to create lights. Great for people who know all of the information to make the light ahead of time. Awful for people who want to generate a light using variables instead of magic numbers. Fortunately, there’s an easy fix.

Torches and Lanterns in Vidar

Lanterns follow the same pattern as torches, no need to worry about the different terms!

Lanterns follow the same pattern as torches, no need to worry about the different terms!

After the Ice Cave (which I feel like I’ve been iterating to death), the player will enter the Dark Cave, filled with puzzles which are completely different in kind. You’ll find torches and lanterns which only stay lit for a short period of time.  Here, lights do a few things:

  • Actually light the map. The “Dark Cave” isn’t a misnomer, you can barely see the screen. So lighting a torch will let you see the puzzle better.
  • Reveal hidden puzzle elements. In the Dark Cave, switches are invisible until a torch within 2 tiles is lit – and will go dark again once the torch expires. Similarly, “light bridges” are only shown if a torch nearby is lit.
  • Open doors. Some puzzles rely on having all of the torches lit simultaneously for a door to open, Zelda-style.

The position of torches changes depending on which puzzle option has been loaded.

The torch pierces the shade even when the player isn't right next to it, allowing for better puzzle solving.

The torch pierces the shade even when the player isn’t right next to it, allowing for better puzzle solving.

Hooking Into Victor

Torches are a Class in Vidar. For our purposes, they have attributes lightindex and id. Remember that each light created using Victor’s script needs a unique number. It doesn’t have to be sequential, it just has to be unique. Once I figure that out, I’ll store it in the correct instance of torch as lightindex. And id is the event’s id when the object is first spawned.

My code to create a light in Victor’s script looks like this:

lightid= $game_map.screen.lights.keys.sort.last + 1
text = "<event light> id: #{lightid} index: #{self.id} name: 'light' zoom: #{zoom} var: 5 speed: 5 </event light>"
$game_map.setup_map_lights(:event, text)
self.lightindex = lightid

So the first line, I get all of the light ids on the map, sort them, and then add one to the highest. This will always be unique. And that’s the lightid I’ll use.

The second line is my comment call! Funny, huh? In Ruby, you can use #{}, and anything in the curly brackets will be evaluated as text. So lightid will be replaced with our unique number, and self.id will be replaced with our event id. Finally, zoom will be passed in when we create the light (we might want to make it bigger or smaller in certain circumstances. In actuality, this is what the text might look like:

<event light>
id: 50
index: 3
name: 'light'
zoom: 100
var: 5
speed: 4
</event light>

Look familiar? It’s exactly what you’d place in a comment call, except in a comment call, #{} won’t evaluate properly.

The third line of code calls Victor’s script, telling it’s an event light with :event, and passing in our comment call as text.

Finally, we store the lightid in the torch so that it can remember which light to turn off when it expires.

And that’s it – this will automatically create the light we need without using a comment call! How fancy!

Deleting Lights

With all that, it’s superbly easy to turn the torch off. The torch already knows which light it created, so all we do is call:

$game_map.screen.remove_light.push(self.lightindex)

And we’re done. This step is far less exciting, sorry.

We can talk about how all these light bridges show up some other time!

We can talk about how all these light bridges show up some other time!

 

 

Advertisements

5 thoughts on “Dev Blog / Script Follow Up – Breaking Victor’s Scripts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s