Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

Fantasy computer for making, playing and sharing tiny games. · By Nesbox

String to Code by Lua in Tic-80 [SOLVED]

A topic by alexsilent created Apr 24, 2021 Views: 439 Replies: 3
Viewing posts 1 to 3
(1 edit)

I found a way to turn string to code:

function RUN(_x)
 local _test = load("return " .. _x)
 return _test()
end
MyVar = 5
RUN("trace('WORK! '..MyVar)")

And now I can make my own Language in Tic-80 with "switch", without "then" and with Pause by dialogue and menu for dialogue scripting,and game scripts by string with line parser and [=[  ]=] brackets in Lua for long strings: local MyLongText = [=[ ...  ]=]

But I can't find how to make variable initializations/changes! Because these lines have an error:
RUN("MyVar = 1")
or
RUN("MyVar = MyVar + 1")

Is there a way to change variables by string in Lua?

(2 edits)

I found an almost ideal code for global variables:

function RUN(_x)
 local _test = load("return " .. _x)
 return _test()
end

HeroHP=75

function SetG(_name,_act,_value)
if (_act=="+") then
 _G[_name] = _G[_name] + _value
elseif (_act=="-") then
 _G[_name] = _G[_name] - _value
elseif (_act=="*") then
 _G[_name] = _G[_name] * _value
elseif (_act=="/") then
 _G[_name] = _G[_name] / _value
else -- default
_G[_name] = _value
end
return _G[_name]
end

RUN("SetG('HeroHP','-',20)")
RUN("print('HeroHP: '..HeroHP)")

PS But maybe there's a more easy and/or advanced way.

It's really my fault. I found an easiest way at all:

function RUN(_x)
 local _test = load(_x)
 return _test()
end

RUN("HeroHP = 1")
RUN("print('HeroHP: '..HeroHP)")
RUN("HeroHP = HeroHP + 1")
RUN("print('HeroHP 2: '..HeroHP)")

PS Now it's a time for make my own pseudo script language for Tic-80. :3

There's the final version to turn any string to code.

-- script: lua
function RUN(_x)
 local _test = load("return ".._x) or load(_x)
 return _test()
end

RUN("t=0")
x=96
y=RUN("24")

function TIC()
 if RUN("btn(0)") then y=y-1 end
 if btn(1) then RUN("y=y+1") end
 RUN("if btn(2) then x=x-1 end")
 if btn(3) then x=x+1 end

 RUN("cls(13)")
 spr(1+t%60//30*2,x,y,14,3,0,0,2,2)
 RUN("print('HELLO WORLD!',84,84)")
 t = RUN("t+1")
end

There's the link: https://tic80.com/play?cart=1819