NoobImbaPro
You can change this now in User CP.
- Reaction score
- 60
Snippet:
Example:
Change Log
JASS:
library ASCIIConverter /*v.2.50*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// ~|By NoobImbaPro|~ //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//
// What does it do:
// It typecasts an integer of type 'hfoo' into a string with "hfoo" value and the opposite
//
// Where it can be used:
// Many RPG maps hold a great amount of items,abilities,units etc...
// So it can help them categorize and identify their items based on the letters of their object ids
// to add some special properties to them.
//
// API:
// function ObjId2S takes integer ObjectId returns string
// ------------------------------------------------------
// function S2ObjId takes string ObjectId returns integer
//
// Installation:
// Create a new trigger and rename it into ASCII Converter.
// Convert it to custom text and paste this code inside it.
//
// WE Bug:
// You can't use the character "\" inside the integer id. Meaning this: '\abc'.
// You can only type it in your object editor, not in trigger editor.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// ~|By NoobImbaPro|~ //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
globals
private string alphabet = " !\"#$%&039;()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
private integer start = 31 //Variable that skips the first characters that can't be used inside an object id integer
private integer array value //Explained on initialization function
public string array index //Simplify string results by isolating each letter from alphabet
endglobals
//Begining from the first letter from left side, identifies it and adds it into a string
function ObjId2S takes integer ObjectId returns string
local integer i = 0
local integer j = 4
local string result = ""
loop
exitwhen j == 0
set i = R2I(ObjectId/value[j])
set result = result + index[i-start]
set ObjectId = ObjectId - i*value[j]
set j = j - 1
endloop
return result
endfunction
function S2ObjId takes string ObjectId returns integer
local integer length = StringLength(ObjectId)
local integer result = 0
local integer i = 0
local integer j = 0
local string objectPart
if length == 4 then
loop
set objectPart = SubString(ObjectId, j, j + 1)
loop
set i = i + 1
exitwhen index<i> == objectPart
endloop
set result = result + (i+start)*value[4-j]
set j = j + 1
set i = 0
exitwhen j > 4
endloop
return result
else
debug call BJDebugMsg("Attempted to convert an invalid value into integer")
endif
return 0
endfunction
//Runs on Map initialization setting the "values" like the decimal ones.
//Decimal 1 --> 10 --> 100 (10*10) --> 1000 (10*10*10)
//ASCII 1 --> 256 --> 256 * 256 ....
//With these values I can the parse each letter at a loop's repeat
//In decimals the number 5678 is consisted by 5*1000 + 6*100 + 7*10 + 8*1
//In ASCII the letter "a" has a value of 97 so an 'aaaa' number is equal to 97*256*256*256 + 97*256*256 + 97*256 + 97
module ASCII_Module_Initializer
private static method onInit takes nothing returns nothing
local integer i = 0
loop
set i = i + 1
set index<i> = SubString(alphabet, i-1, i)
exitwhen index<i> == ""
endloop
set value[1] = 1
set value[2] = 256
set value[3] = 256 * 256
set value[4] = 256 * 256 * 256
endmethod
endmodule
private struct ASCII_Struct_Module_Initializer
implement ASCII_Module_Initializer
endstruct
endlibrary</i></i></i>
Example:
JASS:
scope SpawnIllegalCharacterItem initializer onInit
// You can't preplace an item with this character "\" inside a 'abcd' type variable.
// So we spawn the item with id '\a$0' by trigger on Map Initialization by converting it from a string witch uses the "\" character
private function onInit takes nothing returns nothing
call CreateItemLoc(S2ObjId("\\a$0"), GetRectCenter(gg_rct_Region_000))
endfunction
endscope
Change Log
Code:
v.1.00: Initial implement of ObjId2S function
v.2.00: Implement of S2ObjId function, faster coding of ObjId2S function
v.2.50: Globals initialization through module initializers