Skip to main content

News

Topic: newbie scripting pt.I (createperson) (Read 1854 times) previous topic - next topic

newbie scripting pt.I (createperson)
I'm gonna be that guy who asks a question without having the program even open but I am at work and digress.  I've been fiddling with scripting entities lately to create a bit of foundation for a little project. Anyway everytime a try to create a new test "person" [using createperson()], it ends up on top of the main character and reducing movement. Am I supposed to not be using the same sprite animation for two characters? Is there a better way to call npcs and enemies into the fray besides the gui method?

Thanks in advance.
  • Last Edit: June 11, 2014, 05:56:36 pm by pancakes4ever

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: newbie scripting pt.I (createperson)
Reply #1
CreatePerson by default sets all entities to the default starting spot on a map. What you need to do manually is to move them through code via SetPersonX/Y() functions.

So, generally speaking adding an entity to map may involve writing a piece of code like this:
Code: (javascript) [Select]

function CreateMapPerson(props) {
    CreatePerson(props.name, props.spriteset, "persist" in props ? !props.persist : true);
    if ("x" in props) SetPersonX(props.name, props.x * GetTileWidth());
    if ("y" in props) SetPersonY(props.name, props.y * GetTileHeight());
    if ("layer" in props) SetPersonLayer(props.name, props.layer);
}

// Create some entities:
CreateMapPerson({ name: "bob", spriteset: "bob.rss", x: 5, y: 10 }); // place bob at tile position 5,10 on default layer
CreateMapPerson({ name: "sam", spriteset: "sam.rss", x: 10, y: 12, layer: 2 }); // place sam at tile position 10,12 on layer 2


Now, there is one exception, you cannot place enemies before the map engine loads (I have no clue why, there is no reason for this). So in the case of starting up a new game, you'll have to get tricky:

Code: (javascript) [Select]

function game() {
    CreatePerson("player", "player.rss", false);
    AttachInput("player");
    AttachCamera("player");
    QueuePersonScript("player", "CreateMapPerson({ name: 'bob', spriteset: 'bob.rss', x: 10, y: 5 })", true);
    MapEngine("map.rmp", 60);
}


It looks hackish, but that's like one of three methods of trying to place entities as the map engine loads. This is the easiest solution, equivalent solutions would require you to use SetDelayScript, or SetDefaultMapEngineScript. But this is the safest what I showed above since it doesn't overwrite any existing scripts.

The function CreateMapPerson is a bit advanced, it takes properties in it's input rather than the standard style of input. This is so you can pick and choose what parameters you want to use. In this way you can decide to only choose an x position, or only a layer, or decide whether or not to persist the created entity. (Persisting an entity means: will he live to see a map change? Since changing maps kills all entities. Setting persist to true keeps the entity).
  • Last Edit: June 11, 2014, 10:17:09 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: newbie scripting pt.I (createperson)
Reply #2
Awesome! This makes a lot more sense and is lot easier than what I thought I was going to have to do.
I figured it kind of make sense you can't set enemy positions pre-map, the map doesn't exist before you set it as the engine, so if one plots the X,Y before hand, its really plotting in a negative space. if this is the case,  then the best way to set a location is to do it when the map does exist .  But since the map engine is basically a loop, any location commands placed in Main class after MapEngine() isn't executed (which is what I stupidly did. I spent like 20 minutes shaking my head in shame). I thought I was going to have to make an update script for setting enemies then one for actual gameplay.
Thank you!   :D

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: newbie scripting pt.I (createperson)
Reply #3

But since the map engine is basically a loop, any location commands placed in Main class after MapEngine() isn't executed (which is what I stupidly did.


We all stupidly do that in Sphere! :P Even though the map hasn't loaded yet, the reason why you would want to set people before it loads is because you know where they'll be ahead of time. Sure the map hasn't loaded yet, but neither have they. But having the ability set up what the map will look like before it loads is always nice. I mean you can do this perfectly fine with ChangeMap() It's just that MapEngine() as you are aware, goes into an infinite map-loop, and so is no good to put code before or after it.

Setting a person script, a delay or map script just means we are running that code while the map engine runs. The person script is the best choice since we know it runs only once and since it's the first script attached to the entity, it'll happen right as the map loads. The last parameter 'true' means the code will happen immediately. It's better than any other method, even though a person scripts were added not to necessarily do that (lol).

Sphere should have an eventing system like in RPGMaker, the closest we have is LordEnglish's Scenario script, but it lacks from the fact it's not tightly integrated to the map engine, so it must piggy-back off of it to do work and some bootstrapping means it's still not ideal for all situations. I think he resorts to a delay script to get it up and running. Having an easy to script, in-map-engine eventing system would really provide fresh air to Sphere, and solve all of these problems of writing your own event functionality (such as the spawning of other entities, etc).
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: newbie scripting pt.I (createperson)
Reply #4
Quote
Awesome! This makes a lot more sense and is lot easier than what I thought I was going to have to do.
I figured it kind of make sense you can't set enemy positions pre-map, the map doesn't exist before you set it as the engine, so if one plots the X,Y before hand, its really plotting in a negative space. if this is the case,  then the best way to set a location is to do it when the map does exist .  But since the map engine is basically a loop, any location commands placed in Main class after MapEngine() isn't executed (which is what I stupidly did. I spent like 20 minutes shaking my head in shame). I thought I was going to have to make an update script for setting enemies then one for actual gameplay.
Thank you!  


... yup... I did this too a few years back... even now still on occasion :)

But alas, thats why we are here to help. Sometimes it just requires a "different" set of eyes