[Request/Info] String to Code?

Ghostwind

o________o
Reaction score
172
I was wondering if there is a function, or the possibility to create a function, that turns a string into code?

The purpose of this is to allow the user to call jass functions in game, which would be used in a potential RP map I would create if such a function were to exist.

Example: Player 1 types "call CreateUnit(Player(1), 'hfoo', 0.00, 0.00, 0.00)" in game, a footman spawns at 0.00,0.00 for Player 1.

Is this possible to be made? I cannot tell you how useful this would be for me.
 
Reaction score
341
I doubt it, though you can maybe use the ExecuteFunc function, because it takes an argument as string.

so you could have

JASS:
function createunit takes nothing returns nothing
    call CreateUnit(Player(1), 'hfoo', 0.00, 0.00, 0.00)
endfunction


Then use call ExecuteFunc(EnteredChatString()) in your chat trigger.

I have never tested this though.
 

Ghostwind

o________o
Reaction score
172
ExecuteFunc takes a code not a string, no? That's what I want anyways, is to use ExecuteFunc(StringToCode(........))
 

Tom_Kazansky

--- wraith it ! ---
Reaction score
157
I think of a way:
JASS:
scope Chatter initializer Init

function ChatCreateUnit takes integer playerid, integer uid, real x, real y, real face returns unit
    set bj_lastCreatedUnit = CreateUnit(Player(playerid),uid,x,y,face)
    return bj_lastCreatedUnit
endfunction

private function FindString takes string st, string char, integer start returns integer
    local integer i
    local integer im = StringLength(st)
    local integer l = StringLength(char)
    set i = start
    loop
        exitwhen i > im-(l-1)
        if SubString(st,i-1,i+l-1) == char then
        return i-1
        endif
        set i = i + 1
    endloop
    if i == im then
        return i
    endif
    return i-1
endfunction

private function ChatterCond takes nothing returns boolean
    return SubString(GetEventPlayerChatString(), 0, 5) == "call "
endfunction

private function ChatterAct takes nothing returns nothing
    local string s = GetEventPlayerChatString()
    local integer c1 = 0
    local integer c2 = 0
    local integer pid
    local integer uid
    local real x
    local real y
    local real face
    set s = SubString(s, 5,StringLength(s))
    
    set c2 = FindString( s, "_", 1 )
    if SubString(s,c1,c2) == "ChatCreateUnit" then
        set c1 = c2 + 2
        set c2 = FindString( s, "_", c1 )
        set pid = S2I(SubString(s,c1-1,c2))
        set c1 = c2 + 2
        set c2 = FindString( s, "_", c1 )
        set uid = UnitId(SubString(s,c1-1,c2))
        set c1 = c2 + 2
        set c2 = FindString( s, "_", c1 )
        set x = S2R(SubString(s,c1-1,c2))
        set c1 = c2 + 2
        set c2 = FindString( s, "_", c1 )
        set y = S2R(SubString(s,c1-1,c2))
        set c1 = c2 + 2
        set c2 = FindString( s, "_", c1 )
        set face = S2R(SubString(s,c1-1,c2))
        call ChatCreateUnit( pid, uid, x, y ,face )
    endif
    
endfunction

//===========================================================================
public function Init takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    local integer i = 0
    loop
        exitwhen i > 11
        call TriggerRegisterPlayerChatEvent( t, Player(i), "call ", false )
        set i = i + 1
    endloop
    call TriggerAddCondition( t, Condition( function ChatterCond ) )
    call TriggerAddAction( t, function ChatterAct )
    set t = null
endfunction

endscope



When a player chat: call ChatCreateUnit_0_Footman_0_0_270
it creates a Footman for Player 1 at ( 0, 0 ) and face 270 degrees
about the unit id, I only can convert name -> id -> create with this id, but it works.

Ghostwind, if you are interested, I will explain more
anyways, see the demo map. ;)
 

Attachments

  • Chatter.w3x
    17.3 KB · Views: 130

Ghostwind

o________o
Reaction score
172
Hmm. I recall a tutorial at wc3jass on that func. I'll have a look at that tomorrow.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top