Spherical forums

Sphere Development => Sphere Support => Script Support => Topic started by: Miscreant on August 15, 2017, 05:24:26 am

Title: GetPersonDirection??
Post by: Miscreant on August 15, 2017, 05:24:26 am
I'm trying to get my bombs functioning properly. The function is bound to key B for Bombs (for now). The function is not updating the person direction. No matter which direction the player is facing it returns the value of North. Any ideas why this might be?
Title: Re: GetPersonDirection??
Post by: DaVince on August 15, 2017, 09:06:04 am
The problem lies here:

Code: [Select]
if (PerDir = "North"){
BombX = PerX;
BombY = (PerY - 40);
}else if (PerDir = "South"){
BombX = PerX;
BombY = (PerY + 40);
}else if (PerDir = "East"){
BombX = (PerX + 40);
BombY = PerY;
}else if (PerDir = "West"){
BombX = (PerX - 40);
BombY = PerY;
}

You are using a single = which means assignment, so basically you're setting PerDir to "North" right away in that very first if statement and it skips everything else. You'll want to use == here, which means comparison.

Another thing: person directions are case sensitive, so you'll need to change the capitalization to "north", "south", "east" and "west", unless you also capitalized the directions in the spriteset file (though this will likely break some default functionality in the map engine).

Example:
Code: [Select]
if (PerDir == "north")
Title: Re: GetPersonDirection??
Post by: Miscreant on August 15, 2017, 02:40:11 pm
Example:
Code: [Select]
if (PerDir == "north")

My original line was if (PerDir=="North") etc and it kept returning an invalid integer.

Quote
Another thing: person directions are case sensitive, so you'll need to change the capitalization to "north", "south", "east" and "west", unless you also capitalized the directions in the spriteset file (though this will likely break some default functionality in the map engine).

My spritesets are capitalized. It's the natural flow of language and does not seem right if everything is lowercase. However, I went into the spriteset file and changed all the directionals to lowercase and the bombs are now functioning. The directions update and is no longer returning an invalid integer.

I suppose this means that I will have to go and change all the directionals in all my spritesets to lowercase...
Title: Re: GetPersonDirection??
Post by: Fat Cerberus on August 15, 2017, 03:03:36 pm
The map engine is hardcoded to use default direction names in several places, e.g. here:
https://github.com/fatcerberus/minisphere/blob/v4.8.1/src/minisphere/map_engine.c#L1983-L2006

Programming languages are almost always case-sensitive, with a few exceptions (usually BASIC-derived languages are not case-aware).  And in JS, camelCase is the usual convention, so that's what was chosen for the default direction names.

Whether or not it was wise from a design standpoint to special-case the directional poses at all is a question to ask when the new map engine is being designed. ;)

edit: Your "invalid integer" when using == is probably because GetPersonDirection() returned something other than what your if/elseif tower is checking for, so `BombX` and `BombY` were left undefined.  If you then pass them a function expecting a number, you get an error.  JavaScript doesn't have types like e.g. C++, so that kind of thing isn't checked for you.
Title: Re: GetPersonDirection??
Post by: Fat Cerberus on August 15, 2017, 03:40:52 pm
So, interesting thing: Sphere 1.x does a case-insensitive search when looking for spriteset poses:
https://github.com/sphere-group/sphere/blob/master/sphere/source/common/Spriteset.cpp#L925

miniSphere turns out to do the same:
https://github.com/fatcerberus/minisphere/blob/v4.8.1/src/minisphere/spriteset.c#L642
(strcasecmp() is actually case-insensitive, contrary to what the name suggests)

So I'm actually not sure why capitalizing the direction names in the spriteset doesn't work properly... :-\
Title: Re: GetPersonDirection??
Post by: Rhuan on August 15, 2017, 05:40:01 pm
I suppose this means that I will have to go and change all the directionals in all my spritesets to lowercase...
You can write a script to do that.

Code: [Select]
function convertSprites()
{
  var to_convert = GetFileList("spritesets");
  var temp;

  for(var i = 0, j = 0; i < to_convert.length; ++i)
  {
    if(to_convert[i].slice(to_convert[i].length-5,4)==".rss")
    {
      temp = LoadSpriteset(to_convert[i]);
      for (j = 0; j < temp.directions; ++j)
      {
        temp.directions[j].name =  temp.directions[j].name.toLowerCase();
      }
      temp.save("../other/" + to_convert[i]);
    }
  }
}

Notes:
1. the converted spritesets will be saved in the folder "other" inside your game's folder the originals won't be overwritten.
2. I haven't tested the above but it should work...
Title: Re: GetPersonDirection??
Post by: Miscreant on August 15, 2017, 08:38:18 pm
Notes:
1. the converted spritesets will be saved in the folder "other" inside your game's folder the originals won't be overwritten.
2. I haven't tested the above but it should work...

I created an empty project, copied my spritesets into the spriteset folder and executed your code to test it. It did nothing.
Tried it in both Sphere 1.5 & miniSphere.

I don't have that many spritesets to convert. I'll just go through them and edit them by hand.
Title: Re: GetPersonDirection??
Post by: Miscreant on August 15, 2017, 08:45:46 pm
on a side note, I'm trying to convert the x/y values to something less absolute. I now have a var Bombs=LoadSpriteset("bomb.rss");

I was trying to change the BombX/BombY to something along the lines of (as an example) BombX = PerX + Bombs.width;

Is spriteset_object.width a valid call?
Title: Re: GetPersonDirection??
Post by: Fat Cerberus on August 15, 2017, 09:46:28 pm
No, but spriteset_obj.images[0].width is.  All images in a spriteset are the same size so you just have to know the size of one of them.
Title: Re: GetPersonDirection??
Post by: Rhuan on August 16, 2017, 12:33:45 am
Notes:
1. the converted spritesets will be saved in the folder "other" inside your game's folder the originals won't be overwritten.
2. I haven't tested the above but it should work...

I created an empty project, copied my spritesets into the spriteset folder and executed your code to test it. It did nothing.
Tried it in both Sphere 1.5 & miniSphere.

I don't have that many spritesets to convert. I'll just go through them and edit them by hand.
DId you call the function? I've written a function to do the conversion but I didn't include calling it.
Title: Re: GetPersonDirection??
Post by: Fat Cerberus on August 16, 2017, 12:35:06 am
@Rhuan: Want to see something awesome? (miniSphere only)

Code: (JavaScript) [Select]
let spritesets = from(new DirectoryStream('@/spritesets'))
    .where(it => it.fileName.endsWith('.rss'))
    .select(it => ({ fileName: it.fileName, spriteset: LoadSpriteset(it.fullPath) }));
for (let it of spritesets) {
    var convertedName = FS.fullPath(it.fileName, '@/convertedSpritesets');
    from(it.spriteset.directions)
        .each(it => it.name = it.name.toLowerCase());
    it.spriteset.save(convertedName);
}

:))
Title: Re: GetPersonDirection??
Post by: Miscreant on August 16, 2017, 12:38:20 am
@Rhuan It was the only function so I dropped it right into game and attempted it.
Title: Re: GetPersonDirection??
Post by: Rhuan on August 16, 2017, 03:08:43 am
@Rhuan It was the only function so I dropped it right into game and attempted it.
Did you check the "other" folder afterwards? It will run very quickly and appear to do almost nothing but all the spritsets with updated names will be saved to the "other" folder and there as soon as sphere exits.

Also Sphere 1.5 or miniSphere? I haven't tested spriteset.save in miniSphere (it was added recently) so may be better to use 1.5 for this.
Title: Re: GetPersonDirection??
Post by: Miscreant on August 16, 2017, 03:21:36 am
Quote
Did you check the "other" folder afterwards? It will run very quickly and appear to do almost nothing but all the spritsets with updated names will be saved to the "other" folder and there as soon as sphere exits.

The "other" folder was empty.

Quote
Also Sphere 1.5 or miniSphere?

I tried it in both Sphere 1.5 & miniSphere.
Title: Re: GetPersonDirection??
Post by: Fat Cerberus on August 16, 2017, 03:31:04 am
@Rhuan At a quick glance your code uses slice() when it should use substr().  Maybe that's the problem?
Title: Re: GetPersonDirection??
Post by: Rhuan on August 16, 2017, 03:42:07 am
@Rhuan At a quick glance your code uses slice() when it should use substr().  Maybe that's the problem?
I've rechecked and I had done it wrong. slice should do the job but the parameters were wrong, should be:
to_convert.slice(to_convert.length-5)

The ",4" was not needed and would have stopped it working.
Title: Re: GetPersonDirection??
Post by: Miscreant on August 16, 2017, 04:41:37 pm
The -5 is also incorrect. ".rss" is only 4 characters and the -5 would never equal .rss. However with those changes edited into the code, the function did make a copy of all the spritesets but none of the directional values were changed to lower case.
Title: Re: GetPersonDirection??
Post by: Rhuan on August 16, 2017, 05:10:15 pm
This is why I should never post code I haven't tested...

Fixed and tested:
Code: [Select]
var to_convert = GetFileList("spritesets");
var temp;

for(var i = 0, j = 0; i < to_convert.length; ++i)
{
  if(to_convert[i].slice(to_convert[i].length-4)==".rss")
  {
    temp = LoadSpriteset(to_convert[i]);
    for (j = 0; j < temp.directions.length; ++j)
    {
      temp.directions[j].name =  temp.directions[j].name.toLowerCase();
    }
    temp.save("../other/" + to_convert[i]);
  }
}
Title: Re: GetPersonDirection??
Post by: Miscreant on August 16, 2017, 05:29:08 pm
If anyone else would like to use it. Here it is.

@Rhuan if you look at the settings, I credited you as the author.