Author Topic: Strings || Bricks  (Read 2523 times)

Hi guys! I made the inventory system for my RPG a while ago, in which if you picked up a stick your %client.stick variable would += 1. I deemed this inefficient, and made it so there is just one variable; %client.backpack. It'd be the names of everything you have on you, tied into a string. For example, if you had two logs and one string, it'd be "Log Log String". I'd search through it to find if they have a item, when needed. I was wondering if this is actually more efficient, or if I'm just a dumbass and wasted a lot of hours trying to get said system to work. If it is actually more efficient, another question is what is the character limit on a string? If I pick up a forgetton of items, eventually when I call talk(%client.backpack);, it stops abruptly. If there is an actual character limit, what would be a good way to shorten it so "Log Log String" gets shortened to "Log(2) String" or something similar.

Also, what does the <br> do, in a string?
« Last Edit: July 06, 2015, 06:56:36 PM by Johnny Blockhead »

it's easier to just use variables to store the data, instead of one large string variable
if it makes more sense for you to keep everything in a list in a string, do it that way, if you're just doing it because you can but it doesn't make sense, don't do it that way

<br> should act like a new line when displaying it

I'd probably prefer to do the %client.sticks method, but something in my brain told me the string would be more efficient. I'd switch over, if it didn't mean I'd have to rewrite a large chunk of my code. Thanks.

the limitation you discovered is a limitation on sending messages to players, not inherently tied to the length of strings

strings might have an upper length, but it isn't just 1024 bytes or whatever talk's limitation is

Maybe try something like:

client picks up a log.

%client.backPack["logs"]++;

echo(%client.backpack["logs"]);

Output ==> 1

Edit: Also, check this out: listSupport.cs
« Last Edit: July 06, 2015, 02:10:37 AM by elm »

Using a single variable is much less efficient because you have to loop through them all. If you have 200 sticks, then pick up a rock, and a piece of code wants to see if the player has a rock, then you'll have to loop through 200 iterations, using getWord (and probably more functions) each iteration.

Generally, its a bad idea to store data in strings like this. It is sometimes necessary though, if you are returning it.

Generally, its a bad idea to store data in strings like this. It is sometimes necessary though, if you are returning it.
NO! It is never necessary to use a long string list. Just use an array of global variables or store the array onto the client itself.

Thanks, that makes a lot of sense. I'll end up rewriting it eventually.

There seems to be a bit of an obsession with efficiency lately, which is very strange... Unless your inventory management methods are going to be called hundreds or thousands of times per second, it is highly unlikely that there will be any noticeable return on time spent optimizing your code. If efficiency was as important as some people think, high-level languages such as Python would never flourish, and everyone would program in assembly. However, programming in Python is quick, and programming in assembly is slow and tedious. The difference in execution time, 9999 times out of 10,000, is not worth the difference in programming time.

tl;dr: The time you spend worrying about efficiency would be better spent coding

There seems to be a bit of an obsession with efficiency lately, which is very strange... Unless your inventory management methods are going to be called hundreds or thousands of times per second, it is highly unlikely that there will be any noticeable return on time spent optimizing your code. If efficiency was as important as some people think, high-level languages such as Python would never flourish, and everyone would program in assembly. However, programming in Python is quick, and programming in assembly is slow and tedious. The difference in execution time, 9999 times out of 10,000, is not worth the difference in programming time.

tl;dr: The time you spend worrying about efficiency would be better spent coding
In this case, the way he wrote it was not only inefficient in execution time, but also in writing time and ease of reading

Efficiency of execution should be a concern, but in most cases is not the most important.

people just need to choose the correct data structure for the problem

Okay, sorry, I have a new question.
In my RPG, you can collect certain types of bricks and put them in your inventory. I'm trying to make a system where you can plant the said bricks later on. I latched onto the light command, fired a raycast, and then I'll plant a brick there. The only problem is, is there a way to make it snap to the brick grid? All of the search results were really confusing. If it's an essential part of the answer, I'm trying to do this with 2x2 plates and 2x2 regular bricks.
Edit: I'm dumb. I should've just tried to do it first, I guess.
Bricks automatically align themselves to the brickgrid. If I told torque to plant a 4x cube at "440.564 525.64 467.367", a new 4x cube would appear at "440.5 525.5 467.4"
« Last Edit: July 06, 2015, 05:21:06 PM by Johnny Blockhead »

That totally didn't work.

All of the raycasts seem to go through the trees
Also I can't seem to place the 2x2 plates. Ignore the colors.
//BEEP BEEP, BUILDMODE COMING THROUGH
if(%client.buildmode == 1 && %client.bmstock !$= "")
{
   %result = containerRaycast(%start, %end, $Typemasks::ALL, %client.player); //I have the other parts in before this
   %mat = getWord(%client.bmstock, 1); //%client.bmstock is a string with something like 20 String in it. It specifies how many they can
   %amt = getWord(%client.bmstock, 0); //place. %mat is the material type (String) and %amt is the amount (20)
   %pos = posFromRaycast(%result);
   if(%pos $= "")
      return;
   //Ignore this if-statement chain
   if(%mat $= "Silver")
   {
      %db = SilverBrickData;
      %color = 1;
   }
   else if(%mat $= "Iron")
   {
      %db = IronBrickData;
      %color = 1;
   }
   else if(%mat $= "Steel")
   {
      %db = SteelBrickData;
      %color = %client.currentColor;
   }
   else if(%mat $= "String")
   {
      %db = StringBrickData;
      %color = 1;
   }
   else if(%mat $= "WoodPlank")
   {
      %db = WoodPlankBrickData;
      %color = %client.currentColor;
   }
   %brick = new fxDTSBrick()
   {
      datablock = %db;
      position = %pos;
      isPlanted = 1;
      colorID = %color;
   };
   %client.bmstock = %amt - 1 SPC %mat;
   return;
}

The code is a bit messy right now, sorry.
Edit: I fixed the wooden planks not planting, it was an if-statement i set up that would return;, and it was going off when it wasn't supposed to. Bricks placed through this method have really wonky collision, it's a bumpy ride when you walk on them. All tools (hammer, wrench, printer) seem to ignore them. I also made it so the bricks are added to the clients brickgroup.

I found this snippet of code:
%posToGrid = mFloatLength(getWord(%pos,0)/2,0)*2 SPC mFloatLength(getWord(%pos,1)/2,0)*2 SPC 1+mFloatLength(getWord(%pos,2)/2,0)*2;
Practically no idea what it does, but could I use it? It's said to align it to the 4x4 cube grid. Can I use that for my 2x2 plates and 4x4 plates?

Edit 2: Oh wait, I'm dumb. They are actually aligning to the grid. Or, at least the plates are. I'm not sure what was up with trees. I should probably re-test that. It's just that they can't be tool'd and their collision is really weird. /getID raycasts completely ignore them.


If I stand still on top of them, my feet just sink through.
« Last Edit: July 06, 2015, 06:56:04 PM by Johnny Blockhead »

You need to do %brick.plant(); after creating the brick.
You also probably shouldn't use $Typemasks::ALL because it'll also collide with players, items, fake killed bricks, etc.
« Last Edit: July 07, 2015, 06:33:10 AM by jes00 »

You need to do %brick.plant(); after creating the brick.
You also probably should use $Typemasks::ALL because it'll also collide with players, items, fake killed bricks, etc.
Thanks!
I did use $Typemasks::All? Or did you make a typo and are you saying I shouldn't? I guess I'll use the brick typemask.
Edit: The collision and raycast problems are gone, but the bricks still go inside other bricks. Any fix? Collision/Raycasts forget up for bricks placed inside other bricks. Could this be because of planting errors? If so, how do I detect when the error happens so I can stop the entire thing from happening?

Edit2: Thanks to a huge help from Visolator, I got it fixed! I did %error = %brick.plant(); and checked if it existed. Then, I deleted the brick when there was an error.
« Last Edit: July 06, 2015, 07:55:20 PM by Johnny Blockhead »