help checking if substring exists

Started by Memor-X, January 20, 2013, 06:08:17 pm

Previous topic - Next topic

Memor-X

i finally got my XGrid's hud working in my game (after almost 3 months dam it!), so far i just got strings but i plan to change them to use a function which would return the string, the function itself will return the % completion of each grid for each character in a 2 dimensional table (each character's % completion of each of the grids)

each grid is an instance of a class which really just hold the data which is created in the Actor Class so that way the same grid will have different data for each of the characters, i id these using a string (eg. "Aria-01",  "Cain-01",  "Kaze-05",  "Sain-CCC")

now, in order to get my % completion i need to go into every grid for the actor and get the sum of the nodes activated, the sum of the total nodes for the grid, divide the sum activated by the sum total and then multiple by 100, %'s 101.

the problem i have is that i have no idea how i'm going to do the search, the only idea i have is to call a substring function that return's true or false if the substring is found and search for the first part of each of the grid's string id and i put this in a for each loop, but i don't know if Ruby or RGSS has a function like that, what's it's name or if it has a better why, this is an example of the code i wrote down (for reference, it probably doesn't work anyway)


totalActivated = 0
totalInGrid = 0
comRate = 0.00

foreach(XGrid.Actors.Grids as currentGrid)
{
     if(subString(currentGrid.name,"Aria"))
     {
          totalActivated += currentGrid.activated
          totalInGrid += currentGrid.total
     }
}

comRate = totalActivated / totalInGrid

return toString(comRate * 100) . "%"


KK20

In all honesty, I got lost into what you are trying to accomplish. :wacko:
Anyways, I do this

str1 = "Hello"
str2 = "Hellow"
a = str1[/low/]
b = str2[/low/]
print a #-> nil
print b #-> "low"

If the string doesn't return nil, it contains the substring.

There are other ways to do this as well, some of which you can easily find by searching "substring" in the RMXP help file.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Blizzard

January 21, 2013, 03:49:00 am #2 Last Edit: January 21, 2013, 03:51:11 am by Blizzard
Alternatively you can use:

str.clone.sub!(/low/) {}


It also returns nil if the substring doesn't exist. Though, this is when you need regular expressions. For pure strings using String#include? is enough.

['Aria-01', 'Cain-01', 'Kaze-05', 'Sain-CCC'].any? {|str| string.include?(str) }


Or for your purpose of counting stuff:

count = 0
['Aria-01', 'Cain-01', 'Kaze-05', 'Sain-CCC'].each {|str| count += 1 if string.include?(str) }
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Memor-X

thanks both of you

Quote from: KK20 on January 20, 2013, 10:24:28 pm
In all honesty, I got lost into what you are trying to accomplish. :wacko:


which is why i provided that code example trying to explain what i was planning, ofcause, in all honesty, i should never be allowed to explain my work and/or code, should be a law of Gia and Alaya enforceable by a Counter Force, but in the end everything i say can be simplified like these

http://somethingofthatilk.com/index.php?id=325

my friend said that i was the combination of the top 2 (at least the last frame of each), otherwise, i would be

Frame 1: My original Post
Frame 2: i want to search for a word in a series of words in a collection of sentences
Frame 3: SELECT activated, total FROM grid_collection WHERE id LIKE "%argument%"
Special Frame: :facepalm:

KK20

I work better with images. Plus, you only said 'nodes' and not really what this grid contains or anything. Or what it even does.

btw that comic is gold. I have a friend who wants to be a historian and he hated me for showing him that.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Memor-X

Quote from: KK20 on January 21, 2013, 09:41:49 pm
you only said 'nodes' and not really what this grid contains or anything. Or what it even does.


:facepalm: did i forget that, the XGrid is kinda like the Sphere Grid from Final Fantasy X with the Nodes being events that you activate to increase your stats by spending points and using particular items, each map is a separate Grid filled with these events which range from increasing your stats to learning new skills, swapping out your summons and just block your advancement (to prevent people just jumping to get the most powerful nodes first off, i have blocked areas off with Nodes which requires either the character being at a certain Level or reaching a certain state in the game), the difference btween the 2 is that the XGrid is not linear, you could skip all MP increasing Nodes to make a warrior without any repercussions, in the Sphere Grid, if you skipped a Node and later learned you needed it you would have to grind your S.lv to backtrack and Grid it back to get back to where you was (i think you can backtrack like 4 or 5 Nodes along an activated path but that still costs S.lv which you would want for advancing forward, reason why when i play, if i can't activate a node i will sit and wait until i get the sphere (minus lock nodes))

so far what my script does is store the information of which nodes have been activated, now that i've got that working i'm trying to get a nice HUD to show up, there are 2 different HUDs, one is the Hub's HUD which shows the totals of all the Grids for each character, the other shows when you enter a character's Grid where it shows what their status are, completion of that Grid and how many Nodes of each type, all of which are easy to display once i got the HUD designed and displaying with strings, problem was that to reduce potential Lag i had to break the Grids into different maps which made displaying totals hardder

KK20

Ah that makes much more sense now. Even though I never played FFX (or really any of the series) I can just relate it to a type of complex skill tree system. I'd like to see what it all looks like when its done.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!