String Format Question
#1

Is it better practice to create individual strings to be formatted for different things?, or format a single string hundreds of times?

Probably doesn't make much sense so I'll try to explain a bit better, lets say you have a timer which calls a function for updating things every 1000ms, such as time, textdraws, races, minigames and so on..

So instead of having a big bunch of strings for all the different things, you can use one right?

I don't know I'm only asking this because I was a bit sceptical about how the server would cope formatting a single string hundreds and hundreds of times a second for hundreds of things.. like and I mean literally hundreds..
Reply
#2

Read this thread, it's related to your question https://sampforum.blast.hk/showthread.php?pid=876854#pid876854 and pretty interesting.
Reply
#3

Ahh, I spose it's alright then seeing as though the server is single threaded an can only really do one thing at any one time.. an as a result of that there isn't even the slightest chance that a single string can be used twice at the 'exact' same time.

I knew the server was single threaded, I really should've thought about it a bit more lol.


Thanks tho
Reply
#4

https://sampforum.blast.hk/showthread.php?tid=583960

This thread also has some information about it. Vince says that it's not good to use global strings.
Reply
#5

I'm pretty sure vince means a global string, like declaring a single string at the top for an entire script to use, which is an obvious no no, but I'm more asking about a string which is formatted many many times within a single function. The function in question is 2193 lines long.. 'formatting' occurs approx 56 times every second, I say approx because it can be less or more in some cases.. depending on what is active at the time.

So in short would you use approx 16 separate strings and keep each to their own, or just use one string for the lot?

It's really confused me, Troydere reminded me about the single threaded process and thats got me thinking, I really need someone that knows for sure!, but thanks for the reply Unte99
Reply
#6

It's fine to do so within a single function.

However, I was always taught that a single function shouldn't be longer than approximately 20 to 30 lines to maintain readability. This isn't always evident because SA-MP is mostly event based. The same can be said for a GUI application in .NET (user clicks button, focus changed, input changed, etc.) but the key difference herein is that those functions are usually linked to one particular element. In SA-MP callbacks like OnDialogResponse can become very long very quickly so then it would probably be better to create a new function for each dialog.
Reply
#7

Oh cheers mate!, an yeah it's a custom one second function, I wouldn't do it for dialogs or for native sa:mp callbacks and never have basically for the reasons you mentioned, I'd probably make an exception though for a really short piece of code. But I totally agree about the readability side of things, I find that using logical variable, array and string names also helps, along with correct and indentation and consistant opening/closing brace use.. just good scripting practices really imo. I can't stand these scripts that avoid using braces and just separate everything with a single space.. yuk, at least I can confidently say that those 2193 lines are totally readable to me.

Thankyou very much for clearing that up for me vince, I really appreciate it
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)